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 de.smartics.properties.impl.config.cache;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.is;
20  import static org.hamcrest.Matchers.nullValue;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.times;
23  import static org.mockito.Mockito.verify;
24  import static org.mockito.Mockito.when;
25  import help.de.smartics.properties.core.PropertyDescriptorBuilder;
26  import help.de.smartics.properties.core.PropertyKeyBuilder;
27  
28  import java.util.Properties;
29  
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  import de.smartics.properties.api.config.domain.ConfigurationProperties;
35  import de.smartics.properties.api.config.domain.DescribedProperty;
36  import de.smartics.properties.api.config.domain.Property;
37  import de.smartics.properties.api.config.domain.PropertyLocation;
38  import de.smartics.properties.api.config.domain.ValidatedProperty;
39  import de.smartics.properties.api.config.domain.key.ApplicationId;
40  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
41  import de.smartics.properties.api.config.domain.key.EnvironmentId;
42  import de.smartics.properties.api.core.domain.PropertyDescriptor;
43  import de.smartics.properties.api.core.domain.PropertyKey;
44  import de.smartics.properties.impl.config.domain.key.rtaware.TenantId;
45  import de.smartics.properties.impl.config.domain.key.rtaware.TenantUserConfigurationKey;
46  import de.smartics.properties.impl.config.domain.key.rtaware.UserId;
47  import de.smartics.properties.spi.config.cache.CacheManagerFactory;
48  import de.smartics.testdoc.annotations.Uut;
49  
50  /**
51   * Tests {@link DependencyTrackingCache}.
52   */
53  public class DependencyTrackingCacheTest
54  { // NOPMD
55    // ********************************* Fields *********************************
56  
57    // --- constants ------------------------------------------------------------
58  
59    private static final PropertyLocation TEST_SOURCE = new PropertyLocation(
60        "test");
61  
62    // ... values for caching tests
63    private static final String KEY_MISS = "miss";
64  
65    private static final String KEY_HIT = "hit";
66  
67    private static final String VALUE_HIT = "hitValue";
68  
69    private static final String NO_DEFAULT = null;
70  
71    private static final String NO_VALUE = null;
72  
73    private static final String NO_DEFAULT_EXPRESSION = null;
74  
75    // ... values for tests on resolving place holder
76  
77    private static final String DOMAIN_KEY = "domain";
78  
79    private static final String DOMAIN_VALUE = "example.com";
80  
81    private static final String NEW_DOMAIN_VALUE = "new.example.com";
82  
83    private static final String SERVER_KEY = "server";
84  
85    private static final String SERVER_VALUE = "www.${domain}";
86  
87    private static final String RESOLVED_SERVER_VALUE = "www.example.com";
88  
89    private static final String NEW_RESOLVED_SERVER_VALUE = "www.new.example.com";
90  
91    private static final String MAIL_SERVER_KEY = "mail.server";
92  
93    private static final String MAIL_SERVER_VALUE = null;
94  
95    private static final String MAIL_SERVER_EXPRESSION = "mail-${server}";
96  
97    private static final String RESOLVED_MAIL_SERVER_VALUE =
98        "mail-www.example.com";
99  
100   private static final String NEW_RESOLVED_MAIL_SERVER_VALUE =
101       "mail-www.new.example.com";
102 
103   // --- members --------------------------------------------------------------
104 
105   @Uut
106   private DependencyTrackingCache uut;
107 
108   // ****************************** Inner Classes *****************************
109 
110   // ********************************* Methods ********************************
111 
112   // --- prepare --------------------------------------------------------------
113 
114   @Before
115   public void setUp()
116   {
117     final ConfigurationKey<?> key =
118         new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
119             ApplicationId.ANY_APP, TenantId.ANY_TENANT, UserId.ANY_USER);
120     uut = new DependencyTrackingCache(key);
121   }
122 
123   @After
124   public void shutDown()
125   {
126     CacheManagerFactory.getInstance().createManager().clearAll();
127   }
128 
129   // --- helper ---------------------------------------------------------------
130 
131   private void initCache()
132   {
133     final ConfigurationProperties propertySource =
134         mock(ConfigurationProperties.class);
135 
136     when(propertySource.getValidatedProperty(DOMAIN_KEY, null)).thenReturn(
137         create(TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE, NO_DEFAULT_EXPRESSION,
138             DOMAIN_VALUE),
139         create(TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE, NO_DEFAULT_EXPRESSION,
140             NEW_DOMAIN_VALUE));
141     when(propertySource.getValidatedProperty(SERVER_KEY, null)).thenReturn(
142         create(TEST_SOURCE, SERVER_KEY, SERVER_VALUE, NO_DEFAULT_EXPRESSION,
143             RESOLVED_SERVER_VALUE),
144         create(TEST_SOURCE, SERVER_KEY, SERVER_VALUE, NO_DEFAULT_EXPRESSION,
145             NEW_RESOLVED_SERVER_VALUE));
146     when(propertySource.getValidatedProperty(MAIL_SERVER_KEY, null))
147         .thenReturn(
148             create(TEST_SOURCE, MAIL_SERVER_KEY, MAIL_SERVER_VALUE,
149                 MAIL_SERVER_EXPRESSION, RESOLVED_MAIL_SERVER_VALUE),
150             create(TEST_SOURCE, MAIL_SERVER_KEY, null, MAIL_SERVER_EXPRESSION,
151                 NEW_RESOLVED_MAIL_SERVER_VALUE));
152 
153     uut.getValidatedProperty(propertySource, createDescriptor(DOMAIN_KEY),
154         NO_DEFAULT);
155     uut.getValidatedProperty(propertySource, createDescriptor(SERVER_KEY),
156         NO_DEFAULT);
157     uut.getValidatedProperty(propertySource, createDescriptor(MAIL_SERVER_KEY),
158         NO_DEFAULT);
159   }
160 
161   private static ValidatedProperty create(final PropertyLocation source,
162       final String name, final String value, final String expression,
163       final Object validatedValue) throws IllegalArgumentException
164   {
165     final ConfigurationKey<?> configurationKey =
166         new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
167             ApplicationId.ANY_APP, TenantId.ANY_TENANT, UserId.ANY_USER);
168     final PropertyDescriptor descriptor =
169         PropertyDescriptorBuilder.defaultPropertyDescriptor();
170 
171     final Property property = new Property(source, name, value);
172     final DescribedProperty descProperty =
173         new DescribedProperty(configurationKey, descriptor, property);
174     final ValidatedProperty validatedProperty =
175         new ValidatedProperty(descProperty, expression, validatedValue);
176     return validatedProperty;
177   }
178 
179   // --- tests ----------------------------------------------------------------
180 
181   private static PropertyDescriptor createDescriptor(final String keyName)
182   {
183     final PropertyDescriptorBuilder builder = PropertyDescriptorBuilder.a();
184     final PropertyKeyBuilder keyBuilder = PropertyKeyBuilder.a();
185     keyBuilder.withName(keyName).withPropertySet(null);
186     final PropertyKey key = keyBuilder.build();
187     builder.with(key);
188 
189     return builder.build();
190   }
191 
192   @Test
193   public void storesAMissInTheCache()
194   {
195     final ConfigurationProperties propertySource =
196         mock(ConfigurationProperties.class);
197     final ValidatedProperty validatedProperty =
198         create(TEST_SOURCE, KEY_MISS, NO_VALUE, NO_DEFAULT_EXPRESSION, null);
199     when(propertySource.getValidatedProperty(KEY_MISS, NO_DEFAULT)).thenReturn(
200         validatedProperty);
201 
202     final PropertyDescriptor descriptor = createDescriptor(KEY_MISS);
203     final ValidatedProperty property =
204         uut.getValidatedProperty(propertySource, descriptor, NO_DEFAULT);
205     assertThat(property, is(validatedProperty));
206 
207     uut.getValidatedProperty(propertySource, descriptor, NO_DEFAULT);
208 
209   }
210 
211   @Test
212   public void storesAHitInTheCache()
213   {
214     final ConfigurationProperties propertySource =
215         mock(ConfigurationProperties.class);
216     final ValidatedProperty validatedProperty =
217         create(TEST_SOURCE, KEY_HIT, VALUE_HIT, NO_DEFAULT_EXPRESSION, null);
218     when(propertySource.getValidatedProperty(KEY_HIT, NO_DEFAULT)).thenReturn(
219         validatedProperty);
220 
221     final PropertyDescriptor descriptor = createDescriptor(KEY_HIT);
222     final ValidatedProperty property =
223         uut.getValidatedProperty(propertySource, descriptor, NO_DEFAULT);
224     assertThat(property, is(validatedProperty));
225 
226     uut.getValidatedProperty(propertySource, descriptor, NO_DEFAULT);
227 
228     verify(propertySource, times(1)).getValidatedProperty(KEY_HIT, NO_DEFAULT);
229   }
230 
231   @Test
232   public void removesAValueFromTheCache()
233   {
234     final ConfigurationProperties propertySource =
235         mock(ConfigurationProperties.class);
236     final ValidatedProperty validatedProperty =
237         create(TEST_SOURCE, KEY_HIT, VALUE_HIT, NO_DEFAULT_EXPRESSION, null);
238     when(propertySource.getValidatedProperty(KEY_HIT, NO_DEFAULT)).thenReturn(
239         validatedProperty);
240     final PropertyDescriptor descriptor = createDescriptor(KEY_HIT);
241     uut.getValidatedProperty(propertySource, descriptor, NO_DEFAULT);
242 
243     final Property property = uut.removeFromCache(KEY_HIT);
244     assertThat(property, is((Property) validatedProperty));
245 
246   }
247 
248   @Test
249   public void allowsToRemoveAValueFromTheCacheThatIsNotThereGracefully()
250   {
251     final Property property = uut.removeFromCache(KEY_HIT);
252     assertThat(property, is(nullValue()));
253   }
254 
255   @Test
256   public void removedDependentPropertiesRoot()
257   {
258     initCache();
259 
260     uut.removeFromCache(DOMAIN_KEY);
261   }
262 
263   @Test
264   public void removedDependentPropertiesMiddle()
265   {
266     initCache();
267 
268     uut.removeFromCache(SERVER_KEY);
269   }
270 
271   @Test
272   public void removedDependentPropertiesLeaf()
273   {
274     initCache();
275 
276     uut.removeFromCache(MAIL_SERVER_KEY);
277   }
278 
279   @Test
280   public void removeMulipleRoot()
281   {
282     initCache();
283 
284     final Properties properties = new Properties();
285     properties.setProperty(DOMAIN_KEY, DOMAIN_VALUE);
286     properties.setProperty(MAIL_SERVER_KEY, "non-null");
287 
288     uut.removeFromCache(properties);
289   }
290 
291   @Test
292   public void removeMulipleMiddle()
293   {
294     initCache();
295 
296     final Properties properties = new Properties();
297     properties.setProperty(SERVER_KEY, SERVER_VALUE);
298 
299     uut.removeFromCache(properties);
300   }
301 
302   @Test
303   public void removeMulipleLeaf()
304   {
305     initCache();
306 
307     final Properties properties = new Properties();
308     properties.setProperty(SERVER_KEY, SERVER_VALUE);
309     properties.setProperty(MAIL_SERVER_KEY, "non-null");
310 
311     uut.removeFromCache(properties);
312   }
313 
314   @Test
315   public void allowsEmptyPropertiesGracefully()
316   {
317     initCache();
318 
319     final Properties properties = new Properties();
320 
321     uut.removeFromCache(properties);
322   }
323 }