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.impl.config.domain.key.envapp.EnvAppConfigurationKey;
32  import de.smartics.properties.impl.config.domain.key.envapp.EnvAppDefinitionKeyHelper;
33  import de.smartics.properties.impl.config.domain.key.envapp.EnvAppPropertiesDefinitionContext;
34  import de.smartics.testdoc.annotations.Uut;
35  import de.smartics.util.lang.NullArgumentException;
36  
37  /**
38   * Tests {@link EnvAppDefinitionKeyHelper} with registered groups, nodes and
39   * environments.
40   */
41  @Uut(type = EnvAppDefinitionKeyHelper.class)
42  public class DefinitionKeyHelperRegisteredTest
43  { // NOPMD
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    private static final String COM_GROUP = "com.smartics.test.one";
49  
50    private static final String DEFAULT_GROUP = "de.smartics.test.one";
51  
52    private static final String NON_STANDARD_GROUP = "nonstandard";
53  
54    private static final String NON_STANDARD_TLD = "xxx";
55  
56    private static final String NON_STANDARD_GROUP_TLD = NON_STANDARD_TLD
57                                                         + ".example.test";
58  
59    // ... environments .........................................................
60  
61    private static final EnvironmentId ENV_PRODUCTION = new EnvironmentId(
62        "production");
63  
64    private static final EnvironmentId ENV_PRODUCTION_A = new EnvironmentId(
65        "production", DEFAULT_GROUP);
66  
67    private static final EnvironmentId ENV_PRODUCTION_XX = new EnvironmentId(
68        DEFAULT_GROUP);
69  
70    // ... applications .........................................................
71  
72    private static final ApplicationId APP_ANY_IN_GROUP_ONE = new ApplicationId(
73        COM_GROUP, null, null);
74  
75    private static final ApplicationId APP_WITH_NON_STANDARD_GROUP =
76        new ApplicationId(NON_STANDARD_GROUP, null, null);
77  
78    private static final ApplicationId APP_WITH_NON_STANDARD_TLD =
79        new ApplicationId(NON_STANDARD_GROUP_TLD, null, null);
80  
81    // --- members --------------------------------------------------------------
82  
83    // ****************************** Inner Classes *****************************
84  
85    // ********************************* Methods ********************************
86  
87    // --- prepare --------------------------------------------------------------
88  
89    // --- helper ---------------------------------------------------------------
90  
91    private void runTest(final EnvAppDefinitionKeyHelper uut,
92        final EnvironmentId envId, final ApplicationId appId)
93      throws IllegalArgumentException, NullArgumentException
94    {
95      final String path =
96          envId.toPath() + (envId != ANY_ENV ? "/" : "") + appId.toPath();
97      runTest(uut, path, envId, appId);
98    }
99  
100   @SuppressWarnings("rawtypes")
101   private void runTest(final EnvAppDefinitionKeyHelper uut, final String path,
102       final EnvironmentId envId, final ApplicationId appId)
103     throws IllegalArgumentException, NullArgumentException
104   {
105     final ConfigurationKey key = uut.parse(path);
106 
107     final ConfigurationKey expectedKey =
108         new EnvAppConfigurationKey(envId, appId);
109     assertThat(key, is(equalTo(expectedKey)));
110   }
111 
112   // --- tests ----------------------------------------------------------------
113 
114   @Test
115   public void identifiesRegisteredEnvironments()
116   {
117     final Set<String> envs = new HashSet<String>();
118     envs.add(DEFAULT_GROUP);
119 
120     final EnvAppPropertiesDefinitionContext context =
121         new EnvAppPropertiesDefinitionContext(envs, null, null);
122 
123     final EnvAppDefinitionKeyHelper uut =
124         new EnvAppDefinitionKeyHelper(context);
125     runTest(uut, ENV_PRODUCTION_XX, APP_ANY_IN_GROUP_ONE);
126   }
127 
128   @Test
129   public void identifiesRegisteredNodes()
130   {
131     final Set<String> nodes = new HashSet<String>();
132     nodes.add(DEFAULT_GROUP);
133 
134     final EnvAppPropertiesDefinitionContext context =
135         new EnvAppPropertiesDefinitionContext(null, nodes, null);
136 
137     final EnvAppDefinitionKeyHelper uut =
138         new EnvAppDefinitionKeyHelper(context);
139     runTest(uut, ENV_PRODUCTION_A, APP_ANY_IN_GROUP_ONE);
140   }
141 
142   @Test
143   public void identifiesRegisteredGroups()
144   {
145     final Set<String> groups = new HashSet<String>();
146     groups.add(NON_STANDARD_GROUP);
147 
148     final EnvAppPropertiesDefinitionContext context =
149         new EnvAppPropertiesDefinitionContext(null, null, groups);
150 
151     final EnvAppDefinitionKeyHelper uut =
152         new EnvAppDefinitionKeyHelper(context);
153     runTest(uut, ENV_PRODUCTION, APP_WITH_NON_STANDARD_GROUP);
154   }
155 
156   @Test
157   public void identifiesGroupByRegisteredTlds()
158   {
159     final Set<String> tlds = new HashSet<String>();
160     tlds.add(NON_STANDARD_TLD);
161 
162     final EnvAppPropertiesDefinitionContext context =
163         new EnvAppPropertiesDefinitionContext(tlds, null, null, null, null);
164 
165     final EnvAppDefinitionKeyHelper uut =
166         new EnvAppDefinitionKeyHelper(context);
167     runTest(uut, ENV_PRODUCTION, APP_WITH_NON_STANDARD_TLD);
168   }
169 }