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