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.ApplicationId.ANY_APP;
19  import static de.smartics.properties.api.config.domain.key.EnvironmentId.ANY_ENV;
20  import static org.hamcrest.MatcherAssert.assertThat;
21  import static org.hamcrest.Matchers.equalTo;
22  import static org.hamcrest.Matchers.is;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import de.smartics.properties.api.config.domain.key.ApplicationId;
28  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29  import de.smartics.properties.api.config.domain.key.EnvironmentId;
30  import de.smartics.properties.spi.config.definition.DefinitionKeyHelper;
31  import de.smartics.testdoc.annotations.Uut;
32  import de.smartics.util.lang.NullArgumentException;
33  
34  /**
35   * Tests {@link DefinitionKeyHelper}.
36   */
37  public class DefinitionKeyHelperTest
38  { // NOPMD
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    // ... environments .........................................................
44  
45    private static final EnvironmentId ENV_PRODUCTION = new EnvironmentId(
46        "production");
47  
48    private static final EnvironmentId ENV_PRODUCTION_A = new EnvironmentId(
49        "production", "a");
50  
51    // ... applications .........................................................
52  
53    private static final String DEFAULT_GROUP = "de.smartics.test.one";
54  
55    private static final String TLD_COM_GROUP = "com.smartics.test.one";
56  
57    private static final String DEFAULT_ARTIFACT = "app-1";
58  
59    private static final ApplicationId APP_ANY_IN_GROUP_ONE = new ApplicationId(
60        DEFAULT_GROUP, null, null);
61  
62    private static final ApplicationId APP_ANY_VERSION_OF_APP_1 =
63        new ApplicationId(DEFAULT_GROUP, DEFAULT_ARTIFACT, null);
64  
65    private static final ApplicationId APP_APP_1_V1 = new ApplicationId(
66        DEFAULT_GROUP, DEFAULT_ARTIFACT, "1.0");
67  
68    private static final ApplicationId APP_ANY_IN_GROUP_COM = new ApplicationId(
69        TLD_COM_GROUP, null, null);
70  
71    // --- members --------------------------------------------------------------
72  
73    @Uut
74    private DefinitionKeyHelper uut;
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- prepare --------------------------------------------------------------
81  
82    @Before
83    public void setUp()
84    {
85      uut = new DefinitionKeyHelper();
86    }
87  
88    // --- helper ---------------------------------------------------------------
89  
90    private void runTest(final EnvironmentId envId, final ApplicationId appId)
91      throws IllegalArgumentException, NullArgumentException
92    {
93      final String path =
94          envId.toString() + (envId != ANY_ENV ? "/" : "") + appId.toPath();
95      runTest(path, envId, appId);
96    }
97  
98    private void runTest(final String path, final EnvironmentId envId,
99        final ApplicationId appId) throws IllegalArgumentException,
100     NullArgumentException
101   {
102     final ConfigurationKey key = uut.parse(path);
103 
104     final ConfigurationKey expectedKey = new ConfigurationKey(envId, appId);
105     assertThat(key, is(equalTo(expectedKey)));
106   }
107 
108   // --- tests ----------------------------------------------------------------
109 
110   @Test
111   public void identifiesAnEnvironmentOnly()
112   {
113     runTest(ENV_PRODUCTION, ANY_APP);
114   }
115 
116   @Test
117   public void identifiesAnEnvironmentWithoutNode()
118   {
119     runTest(ENV_PRODUCTION, APP_ANY_IN_GROUP_ONE);
120   }
121 
122   @Test
123   public void identifiesAnEnvironmentWithNodeButNotApplication()
124   {
125     runTest(ENV_PRODUCTION_A, ANY_APP);
126   }
127 
128   @Test
129   public void identifiesAnEnvironmentWithNodeAndApplicationGroup()
130   {
131     runTest(ENV_PRODUCTION_A, APP_ANY_IN_GROUP_ONE);
132   }
133 
134   @Test
135   public void identifiesAnEnvironmentWithNodeAndApplicationGroupAndArtifact()
136   {
137     runTest(ENV_PRODUCTION_A, APP_ANY_VERSION_OF_APP_1);
138   }
139 
140   @Test
141   public void identifiesAnEnvironmentWithNodeAndApplicationGroupAndArtifactAndVersion()
142   {
143     runTest(ENV_PRODUCTION_A, APP_APP_1_V1);
144   }
145 
146   @Test
147   public void identifiesApplicationGroupAndArtifactAndVersion()
148   {
149     runTest(ANY_ENV, APP_APP_1_V1);
150   }
151 
152   @Test
153   public void startingSlashAllowedWithEnvironment()
154   {
155     final String path = '/' + ENV_PRODUCTION.toString();
156     runTest(path, ENV_PRODUCTION, ANY_APP);
157   }
158 
159   @Test
160   public void startingSlashAllowedWithApplicationOnly()
161   {
162     final String path = '/' + APP_APP_1_V1.toPath();
163     runTest(path, ANY_ENV, APP_APP_1_V1);
164   }
165 
166   @Test
167   public void trailingSlashAllowedWithEnvironment()
168   {
169     final String path = ENV_PRODUCTION.toString() + '/';
170     runTest(path, ENV_PRODUCTION, ANY_APP);
171   }
172 
173   @Test
174   public void trailingSlashAllowedWithApplicationOnly()
175   {
176     final String path = APP_APP_1_V1.toPath() + '/';
177     runTest(path, ANY_ENV, APP_APP_1_V1);
178   }
179 
180   @Test
181   public void identifiesAnEnvironmentWithNodeAndApplicationGroupCom()
182   {
183     runTest(ENV_PRODUCTION_A, APP_ANY_IN_GROUP_COM);
184   }
185 
186   @Test
187   public void returnsAnyEnvAnyAppKeyIfNoPathIsGiven()
188   {
189     final ConfigurationKey key = uut.parse("");
190     final ConfigurationKey expectedKey =
191         new ConfigurationKey(EnvironmentId.ANY_ENV, ApplicationId.ANY_APP);
192     assertThat(key, is(equalTo(expectedKey)));
193   }
194 
195   @Test
196   public void skipsPropertiesFile()
197   {
198     final String path = ENV_PRODUCTION.toString() + "/my-module.properties";
199     runTest(path, ENV_PRODUCTION, ANY_APP);
200   }
201 }