View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package test.de.smartics.properties.spi.config.definition;
17  
18  import static de.smartics.properties.api.config.domain.key.EnvironmentId.ANY_ENV;
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.equalTo;
21  import static org.hamcrest.Matchers.is;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.junit.Test;
27  
28  import de.smartics.properties.api.config.domain.key.ApplicationId;
29  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
30  import de.smartics.properties.api.config.domain.key.EnvironmentId;
31  import de.smartics.properties.spi.config.definition.DefinitionKeyHelper;
32  import de.smartics.properties.spi.config.definition.PropertiesDefinitionContext;
33  import de.smartics.testdoc.annotations.Uut;
34  import de.smartics.util.lang.NullArgumentException;
35  
36  /**
37   * Tests {@link DefinitionKeyHelper} with registered groups, nodes and
38   * environments.
39   */
40  @Uut(type = DefinitionKeyHelper.class)
41  public class DefinitionKeyHelperRegisteredTest
42  { // NOPMD
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    private static final String COM_GROUP = "com.smartics.test.one";
48  
49    private static final String DEFAULT_GROUP = "de.smartics.test.one";
50  
51    private static final String NON_STANDARD_GROUP = "nonstandard";
52  
53    private static final String NON_STANDARD_TLD = "xxx";
54  
55    private static final String NON_STANDARD_GROUP_TLD = NON_STANDARD_TLD
56                                                         + ".example.test";
57  
58    // ... environments .........................................................
59  
60    private static final EnvironmentId ENV_PRODUCTION = new EnvironmentId(
61        "production");
62  
63    private static final EnvironmentId ENV_PRODUCTION_A = new EnvironmentId(
64        "production", DEFAULT_GROUP);
65  
66    private static final EnvironmentId ENV_PRODUCTION_XX = new EnvironmentId(
67        DEFAULT_GROUP);
68  
69    // ... applications .........................................................
70  
71    private static final ApplicationId APP_ANY_IN_GROUP_ONE = new ApplicationId(
72        COM_GROUP, null, null);
73  
74    private static final ApplicationId APP_WITH_NON_STANDARD_GROUP =
75        new ApplicationId(NON_STANDARD_GROUP, null, null);
76  
77    private static final ApplicationId APP_WITH_NON_STANDARD_TLD =
78        new ApplicationId(NON_STANDARD_GROUP_TLD, null, null);
79  
80    // --- members --------------------------------------------------------------
81  
82    // ****************************** Inner Classes *****************************
83  
84    // ********************************* Methods ********************************
85  
86    // --- prepare --------------------------------------------------------------
87  
88    // --- helper ---------------------------------------------------------------
89  
90    private void runTest(final DefinitionKeyHelper uut,
91        final EnvironmentId envId, final ApplicationId appId)
92      throws IllegalArgumentException, NullArgumentException
93    {
94      final String path =
95          envId.toString() + (envId != ANY_ENV ? "/" : "") + appId.toPath();
96      runTest(uut, path, envId, appId);
97    }
98  
99    private void runTest(final DefinitionKeyHelper uut, final String path,
100       final EnvironmentId envId, final ApplicationId appId)
101     throws IllegalArgumentException, NullArgumentException
102   {
103     final ConfigurationKey key = uut.parse(path);
104 
105     final ConfigurationKey expectedKey = new ConfigurationKey(envId, appId);
106     assertThat(key, is(equalTo(expectedKey)));
107   }
108 
109   // --- tests ----------------------------------------------------------------
110 
111   @Test
112   public void identifiesRegisteredEnvironments()
113   {
114     final Set<String> envs = new HashSet<String>();
115     envs.add(DEFAULT_GROUP);
116 
117     final PropertiesDefinitionContext context =
118         new PropertiesDefinitionContext(envs, null, null);
119 
120     final DefinitionKeyHelper uut = new DefinitionKeyHelper(context);
121     runTest(uut, ENV_PRODUCTION_XX, APP_ANY_IN_GROUP_ONE);
122   }
123 
124   @Test
125   public void identifiesRegisteredNodes()
126   {
127     final Set<String> nodes = new HashSet<String>();
128     nodes.add(DEFAULT_GROUP);
129 
130     final PropertiesDefinitionContext context =
131         new PropertiesDefinitionContext(null, nodes, null);
132 
133     final DefinitionKeyHelper uut = new DefinitionKeyHelper(context);
134     runTest(uut, ENV_PRODUCTION_A, APP_ANY_IN_GROUP_ONE);
135   }
136 
137   @Test
138   public void identifiesRegisteredGroups()
139   {
140     final Set<String> groups = new HashSet<String>();
141     groups.add(NON_STANDARD_GROUP);
142 
143     final PropertiesDefinitionContext context =
144         new PropertiesDefinitionContext(null, null, groups);
145 
146     final DefinitionKeyHelper uut = new DefinitionKeyHelper(context);
147     runTest(uut, ENV_PRODUCTION, APP_WITH_NON_STANDARD_GROUP);
148   }
149 
150   @Test
151   public void identifiesGroupByRegisteredTlds()
152   {
153     final Set<String> tlds = new HashSet<String>();
154     tlds.add(NON_STANDARD_TLD);
155 
156     final PropertiesDefinitionContext context =
157         new PropertiesDefinitionContext(tlds, null, null, null, null);
158 
159     final DefinitionKeyHelper uut = new DefinitionKeyHelper(context);
160     runTest(uut, ENV_PRODUCTION, APP_WITH_NON_STANDARD_TLD);
161   }
162 }