PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/compute/src/test/java/org/jclouds/compute/internal/FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeatTest.java

http://github.com/jclouds/jclouds
Java | 176 lines | 122 code | 33 blank | 21 comment | 0 complexity | 01fd59abd2bf6f78ed517deec672fc6c MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.compute.internal;
  18. import static org.jclouds.compute.config.ComputeServiceProperties.RESOURCENAME_DELIMITER;
  19. import static org.jclouds.compute.config.ComputeServiceProperties.RESOURCENAME_PREFIX;
  20. import static org.testng.Assert.assertEquals;
  21. import static org.testng.Assert.assertFalse;
  22. import static org.testng.Assert.assertTrue;
  23. import org.jclouds.compute.functions.GroupNamingConvention;
  24. import org.jclouds.predicates.Validator;
  25. import org.testng.annotations.Test;
  26. import com.google.common.base.Supplier;
  27. import com.google.common.base.Suppliers;
  28. import com.google.inject.AbstractModule;
  29. import com.google.inject.Guice;
  30. import com.google.inject.TypeLiteral;
  31. import com.google.inject.name.Names;
  32. @Test(testName = "FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeatTest")
  33. public class FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeatTest {
  34. Validator<String> okValidator = new Validator<String>() {
  35. @Override
  36. public void validate(String t) throws IllegalArgumentException {
  37. }
  38. };
  39. public void testSharedName() {
  40. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  41. "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
  42. assertEquals(fn.sharedNameForGroup("cluster"), "jclouds_cluster");
  43. assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster"), "cluster");
  44. assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster"), null);
  45. assertTrue(fn.containsGroup("cluster").apply("jclouds_cluster"));
  46. }
  47. public void testOkToHaveDelimiterInGroupOnUniqueName() {
  48. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  49. "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
  50. assertEquals(fn.sharedNameForGroup("cluster_"), "jclouds_cluster_");
  51. assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster_"), "cluster_");
  52. assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster_"), null);
  53. assertTrue(fn.containsGroup("cluster_").apply("jclouds_cluster_"));
  54. }
  55. public void testSharedNameWithHyphenInGroup() {
  56. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  57. "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
  58. assertEquals(fn.sharedNameForGroup("cluster-"), "jclouds_cluster-");
  59. assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster-"), "cluster-");
  60. assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster-"), null);
  61. assertTrue(fn.containsGroup("cluster-").apply("jclouds_cluster-"));
  62. }
  63. public void testNextName() {
  64. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  65. "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
  66. assertEquals(fn.uniqueNameForGroup("cluster"), "jclouds_cluster_123");
  67. // note accidental treatment of a unique node as a shared one can lead to
  68. // incorrect group names, as long as we permit delimiter to be in group name
  69. assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster_123"), "cluster_123");
  70. assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster_123"), "cluster");
  71. assertTrue(fn.containsGroup("cluster").apply("jclouds_cluster_123"));
  72. }
  73. public void testCannotFindSharedNameWhenDelimiterWrong() {
  74. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  75. "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
  76. assertEquals(fn.groupInSharedNameOrNull("jclouds#cluster"), null);
  77. assertEquals(fn.groupInUniqueNameOrNull("jclouds#cluster"), null);
  78. assertFalse(fn.containsGroup("cluster").apply("jclouds#cluster"));
  79. }
  80. public void testCannotFindNextNameWhenDelimiterWrong() {
  81. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  82. "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
  83. assertEquals(fn.groupInSharedNameOrNull("jclouds#cluster#123"), null);
  84. assertEquals(fn.groupInUniqueNameOrNull("jclouds#cluster#123"), null);
  85. assertFalse(fn.containsGroup("cluster").apply("jclouds#cluster#123"));
  86. }
  87. public void testPropertyChangesDelimiter() {
  88. GroupNamingConvention fn = Guice.createInjector(new AbstractModule() {
  89. @Override
  90. protected void configure() {
  91. bindConstant().annotatedWith(Names.named(RESOURCENAME_DELIMITER)).to('#');
  92. }
  93. }).getInstance(GroupNamingConvention.Factory.class).create();
  94. assertEquals(fn.sharedNameForGroup("cluster"), "jclouds#cluster");
  95. assertEquals(fn.groupInSharedNameOrNull("jclouds#cluster"), "cluster");
  96. assertEquals(fn.groupInUniqueNameOrNull("jclouds#cluster"), null);
  97. assertTrue(fn.containsGroup("cluster").apply("jclouds#cluster"));
  98. }
  99. public void testPropertyChangesPrefix() {
  100. GroupNamingConvention fn = Guice.createInjector(new AbstractModule() {
  101. @Override
  102. protected void configure() {
  103. bindConstant().annotatedWith(Names.named(RESOURCENAME_PREFIX)).to("kclouds");
  104. }
  105. }).getInstance(GroupNamingConvention.Factory.class).create();
  106. assertEquals(fn.sharedNameForGroup("cluster"), "kclouds-cluster");
  107. assertEquals(fn.groupInSharedNameOrNull("kclouds-cluster"), "cluster");
  108. assertEquals(fn.groupInUniqueNameOrNull("kclouds-cluster"), null);
  109. assertTrue(fn.containsGroup("cluster").apply("kclouds-cluster"));
  110. }
  111. public void testCanChangeSuffixSupplier() {
  112. GroupNamingConvention fn = Guice.createInjector(new AbstractModule() {
  113. @Override
  114. protected void configure() {
  115. bind(new TypeLiteral<Supplier<String>>() {
  116. }).toInstance(Suppliers.ofInstance("foo"));
  117. }
  118. }).getInstance(GroupNamingConvention.Factory.class).create();
  119. assertEquals(fn.uniqueNameForGroup("cluster"), "jclouds-cluster-foo");
  120. // note accidental treatment of a unique node as a shared one can lead to
  121. // incorrect group names, as long as we permit delimiter to be in group name
  122. assertEquals(fn.groupInSharedNameOrNull("jclouds-cluster-foo"), "cluster-foo");
  123. assertEquals(fn.groupInUniqueNameOrNull("jclouds-cluster-foo"), "cluster");
  124. assertTrue(fn.containsGroup("cluster").apply("jclouds-cluster-foo"));
  125. }
  126. // ///
  127. public void testSharedNameNoPrefix() {
  128. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  129. "", '_', Suppliers.ofInstance("123"), okValidator);
  130. assertEquals(fn.sharedNameForGroup("cluster"), "cluster");
  131. assertEquals(fn.groupInSharedNameOrNull("cluster"), "cluster");
  132. assertEquals(fn.groupInUniqueNameOrNull("cluster"), null);
  133. assertTrue(fn.containsGroup("cluster").apply("cluster"));
  134. }
  135. public void testNextNameNoPrefix() {
  136. FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
  137. "", '_', Suppliers.ofInstance("123"), okValidator);
  138. assertEquals(fn.uniqueNameForGroup("cluster"), "cluster_123");
  139. assertEquals(fn.groupInSharedNameOrNull("cluster_123"), "cluster_123");
  140. assertEquals(fn.groupInUniqueNameOrNull("cluster_123"), "cluster");
  141. assertTrue(fn.containsGroup("cluster").apply("cluster_123"));
  142. }
  143. }