PageRenderTime 31ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/andreisavu/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 */
 19package org.jclouds.compute.internal;
 20
 21import static org.jclouds.compute.config.ComputeServiceProperties.RESOURCENAME_DELIMITER;
 22import static org.jclouds.compute.config.ComputeServiceProperties.RESOURCENAME_PREFIX;
 23import static org.testng.Assert.assertEquals;
 24import static org.testng.Assert.assertFalse;
 25import static org.testng.Assert.assertTrue;
 26
 27import org.jclouds.compute.functions.GroupNamingConvention;
 28import org.jclouds.predicates.Validator;
 29import org.testng.annotations.Test;
 30
 31import com.google.common.base.Supplier;
 32import com.google.common.base.Suppliers;
 33import com.google.inject.AbstractModule;
 34import com.google.inject.Guice;
 35import com.google.inject.TypeLiteral;
 36import com.google.inject.name.Names;
 37
 38/**
 39 * 
 40 * @author Adrian Cole
 41 */
 42@Test(testName = "FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeatTest")
 43public class FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeatTest {
 44   Validator<String> okValidator = new Validator<String>() {
 45
 46      @Override
 47      public void validate(String t) throws IllegalArgumentException {
 48
 49      }
 50
 51   };
 52
 53   public void testSharedName() {
 54      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
 55            "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
 56
 57      assertEquals(fn.sharedNameForGroup("cluster"), "jclouds_cluster");
 58      assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster"), "cluster");
 59      assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster"), null);
 60      assertTrue(fn.containsGroup("cluster").apply("jclouds_cluster"));
 61   }
 62
 63   public void testOkToHaveDelimiterInGroupOnUniqueName() {
 64      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
 65            "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
 66
 67      assertEquals(fn.sharedNameForGroup("cluster_"), "jclouds_cluster_");
 68      assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster_"), "cluster_");
 69      assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster_"), null);
 70      assertTrue(fn.containsGroup("cluster_").apply("jclouds_cluster_"));
 71   }
 72
 73   public void testSharedNameWithHyphenInGroup() {
 74      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
 75            "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
 76
 77      assertEquals(fn.sharedNameForGroup("cluster-"), "jclouds_cluster-");
 78      assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster-"), "cluster-");
 79      assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster-"), null);
 80      assertTrue(fn.containsGroup("cluster-").apply("jclouds_cluster-"));
 81   }
 82
 83   public void testNextName() {
 84      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
 85            "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
 86
 87      assertEquals(fn.uniqueNameForGroup("cluster"), "jclouds_cluster_123");
 88      // note accidental treatment of a unique node as a shared one can lead to 
 89      // incorrect group names, as long as we permit delimiter to be in group name
 90      assertEquals(fn.groupInSharedNameOrNull("jclouds_cluster_123"), "cluster_123");
 91      assertEquals(fn.groupInUniqueNameOrNull("jclouds_cluster_123"), "cluster");
 92      assertTrue(fn.containsGroup("cluster").apply("jclouds_cluster_123"));
 93   }
 94
 95   public void testCannotFindSharedNameWhenDelimiterWrong() {
 96      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
 97            "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
 98      assertEquals(fn.groupInSharedNameOrNull("jclouds#cluster"), null);
 99      assertEquals(fn.groupInUniqueNameOrNull("jclouds#cluster"), null);
100      assertFalse(fn.containsGroup("cluster").apply("jclouds#cluster"));
101   }
102
103   public void testCannotFindNextNameWhenDelimiterWrong() {
104      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
105            "jclouds", '_', Suppliers.ofInstance("123"), okValidator);
106      assertEquals(fn.groupInSharedNameOrNull("jclouds#cluster#123"), null);
107      assertEquals(fn.groupInUniqueNameOrNull("jclouds#cluster#123"), null);
108      assertFalse(fn.containsGroup("cluster").apply("jclouds#cluster#123"));
109   }
110
111   public void testPropertyChangesDelimiter() {
112      GroupNamingConvention fn = Guice.createInjector(new AbstractModule() {
113
114         @Override
115         protected void configure() {
116            bindConstant().annotatedWith(Names.named(RESOURCENAME_DELIMITER)).to('#');
117         }
118      }).getInstance(GroupNamingConvention.Factory.class).create();
119
120      assertEquals(fn.sharedNameForGroup("cluster"), "jclouds#cluster");
121      assertEquals(fn.groupInSharedNameOrNull("jclouds#cluster"), "cluster");
122      assertEquals(fn.groupInUniqueNameOrNull("jclouds#cluster"), null);
123      assertTrue(fn.containsGroup("cluster").apply("jclouds#cluster"));
124   }
125
126   public void testPropertyChangesPrefix() {
127      GroupNamingConvention fn = Guice.createInjector(new AbstractModule() {
128
129         @Override
130         protected void configure() {
131            bindConstant().annotatedWith(Names.named(RESOURCENAME_PREFIX)).to("kclouds");
132         }
133      }).getInstance(GroupNamingConvention.Factory.class).create();
134
135      assertEquals(fn.sharedNameForGroup("cluster"), "kclouds-cluster");
136      assertEquals(fn.groupInSharedNameOrNull("kclouds-cluster"), "cluster");
137      assertEquals(fn.groupInUniqueNameOrNull("kclouds-cluster"), null);
138      assertTrue(fn.containsGroup("cluster").apply("kclouds-cluster"));
139
140   }
141
142   public void testCanChangeSuffixSupplier() {
143      GroupNamingConvention fn = Guice.createInjector(new AbstractModule() {
144
145         @Override
146         protected void configure() {
147            bind(new TypeLiteral<Supplier<String>>() {
148            }).toInstance(Suppliers.ofInstance("foo"));
149         }
150      }).getInstance(GroupNamingConvention.Factory.class).create();
151
152      assertEquals(fn.uniqueNameForGroup("cluster"), "jclouds-cluster-foo");
153      // note accidental treatment of a unique node as a shared one can lead to 
154      // incorrect group names, as long as we permit delimiter to be in group name
155      assertEquals(fn.groupInSharedNameOrNull("jclouds-cluster-foo"), "cluster-foo");
156      assertEquals(fn.groupInUniqueNameOrNull("jclouds-cluster-foo"), "cluster");
157      assertTrue(fn.containsGroup("cluster").apply("jclouds-cluster-foo"));
158
159   }
160
161   // ///
162
163   public void testSharedNameNoPrefix() {
164      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
165            "", '_', Suppliers.ofInstance("123"), okValidator);
166
167      assertEquals(fn.sharedNameForGroup("cluster"), "cluster");
168      assertEquals(fn.groupInSharedNameOrNull("cluster"), "cluster");
169      assertEquals(fn.groupInUniqueNameOrNull("cluster"), null);
170      assertTrue(fn.containsGroup("cluster").apply("cluster"));
171   }
172
173   public void testNextNameNoPrefix() {
174      FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat fn = new FormatSharedNamesAndAppendUniqueStringToThoseWhichRepeat(
175            "", '_', Suppliers.ofInstance("123"), okValidator);
176
177      assertEquals(fn.uniqueNameForGroup("cluster"), "cluster_123");
178      assertEquals(fn.groupInSharedNameOrNull("cluster_123"), "cluster_123");
179      assertEquals(fn.groupInUniqueNameOrNull("cluster_123"), "cluster");
180      assertTrue(fn.containsGroup("cluster").apply("cluster_123"));
181   }
182}