PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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