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.impl.config.cache;
17  
18  import static org.hamcrest.MatcherAssert.assertThat;
19  import static org.hamcrest.Matchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.when;
23  import help.de.smartics.properties.core.PropertyDescriptorBuilder;
24  import help.de.smartics.properties.core.PropertyKeyBuilder;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import de.smartics.properties.api.config.domain.DescribedProperty;
30  import de.smartics.properties.api.config.domain.Property;
31  import de.smartics.properties.api.config.domain.PropertyLocation;
32  import de.smartics.properties.api.config.domain.ValidatedProperty;
33  import de.smartics.properties.api.config.domain.key.ApplicationId;
34  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
35  import de.smartics.properties.api.config.domain.key.EnvironmentId;
36  import de.smartics.properties.api.core.domain.PropertyDescriptor;
37  import de.smartics.properties.api.core.domain.PropertyKey;
38  import de.smartics.properties.impl.config.cache.CacheConfigurationPropertiesManagement;
39  import de.smartics.properties.impl.config.domain.key.rtaware.TenantId;
40  import de.smartics.properties.impl.config.domain.key.rtaware.TenantUserConfigurationKey;
41  import de.smartics.properties.impl.config.domain.key.rtaware.UserId;
42  import de.smartics.properties.spi.config.support.ConfigurationPropertiesManagementSpi;
43  import de.smartics.testdoc.annotations.Uut;
44  
45  /**
46   * Tests {@link CacheConfigurationPropertiesManagement}.
47   */
48  public class CacheConfigurationPropertiesManagementTest
49  {
50    // ********************************* Fields *********************************
51  
52    // --- constants ------------------------------------------------------------
53  
54    private static final String NO_DEFAULT_EXPRESSION = null;
55  
56    private static final PropertyLocation TEST_SOURCE = new PropertyLocation(
57        "test");
58  
59    private static final String DOMAIN_KEY = "domain";
60  
61    private static final String DOMAIN_VALUE = "example.com";
62  
63    private static final String NEW_DOMAIN_VALUE = "new.example.com";
64  
65    private static final String SERVER_KEY = "server";
66  
67    private static final String SERVER_VALUE = "www.${domain}";
68  
69    private static final String RESOLVED_SERVER_VALUE = "www.example.com";
70  
71    private static final String NEW_RESOLVED_SERVER_VALUE = "www.new.example.com";
72  
73    private static final String MAIL_SERVER_KEY = "mail.server";
74  
75    private static final String MAIL_SERVER_VALUE = null;
76  
77    private static final String MAIL_SERVER_EXPRESSION = "mail-${server}";
78  
79    private static final String RESOLVED_MAIL_SERVER_VALUE =
80        "mail-www.example.com";
81  
82    private static final String NEW_RESOLVED_MAIL_SERVER_VALUE =
83        "mail-www.new.example.com";
84  
85    // --- members --------------------------------------------------------------
86  
87    @Uut
88    private CacheConfigurationPropertiesManagement uut;
89  
90    // ****************************** Inner Classes *****************************
91  
92    // ********************************* Methods ********************************
93  
94    // --- prepare --------------------------------------------------------------
95  
96    @Before
97    @SuppressWarnings({ "rawtypes", "unchecked" })
98    public void setUp()
99    {
100     final ConfigurationPropertiesManagementSpi delegate =
101         mock(ConfigurationPropertiesManagementSpi.class);
102 
103     final ConfigurationKey configurationKey =
104         new TenantUserConfigurationKey(EnvironmentId.ANY_ENV,
105             ApplicationId.ANY_APP, TenantId.ANY_TENANT, UserId.ANY_USER);
106 
107     when(delegate.getValidatedProperty(DOMAIN_KEY, null)).thenReturn(
108         create(configurationKey, TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE,
109             NO_DEFAULT_EXPRESSION, DOMAIN_VALUE),
110         create(configurationKey, TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE,
111             NO_DEFAULT_EXPRESSION, NEW_DOMAIN_VALUE));
112     when(delegate.getValidatedProperty(SERVER_KEY, null)).thenReturn(
113         create(configurationKey, TEST_SOURCE, SERVER_KEY, SERVER_VALUE,
114             NO_DEFAULT_EXPRESSION, RESOLVED_SERVER_VALUE),
115         create(configurationKey, TEST_SOURCE, SERVER_KEY, SERVER_VALUE,
116             NO_DEFAULT_EXPRESSION, NEW_RESOLVED_SERVER_VALUE));
117     when(delegate.getValidatedProperty(MAIL_SERVER_KEY, null)).thenReturn(
118         create(configurationKey, TEST_SOURCE, MAIL_SERVER_KEY,
119             MAIL_SERVER_VALUE, MAIL_SERVER_EXPRESSION,
120             RESOLVED_MAIL_SERVER_VALUE),
121         create(configurationKey, TEST_SOURCE, MAIL_SERVER_KEY, null,
122             MAIL_SERVER_EXPRESSION, NEW_RESOLVED_MAIL_SERVER_VALUE));
123 
124     when(delegate.getKey()).thenReturn(configurationKey);
125 
126     uut = new CacheConfigurationPropertiesManagement(delegate);
127 
128   }
129 
130   // CHECKSTYLE:OFF
131   private static ValidatedProperty create(
132       final ConfigurationKey<?> configurationKey,
133       final PropertyLocation source, final String name, final String value,
134       final String expression, final Object validatedValue)
135     throws IllegalArgumentException
136     // CHECKSTYLE:ON
137   {
138     final PropertyDescriptor descriptor =
139         PropertyDescriptorBuilder.defaultPropertyDescriptor();
140 
141     final Property property = new Property(source, name, value);
142     final DescribedProperty descProperty =
143         new DescribedProperty(configurationKey, descriptor, property);
144     final ValidatedProperty validatedProperty =
145         new ValidatedProperty(descProperty, expression, validatedValue);
146     return validatedProperty;
147   }
148 
149   // --- helper ---------------------------------------------------------------
150 
151   private void fillCache()
152   {
153     final String actualDomainValue =
154         (String) uut.getPropertyValue(createDescriptor(DOMAIN_KEY));
155     final String actualServerValue =
156         (String) uut.getPropertyValue(createDescriptor(SERVER_KEY));
157     final String actualMailServerValue =
158         (String) uut.getPropertyValue(createDescriptor(MAIL_SERVER_KEY));
159 
160     assertThat(actualDomainValue, is(equalTo(DOMAIN_VALUE)));
161     assertThat(actualServerValue, is(equalTo(RESOLVED_SERVER_VALUE)));
162     assertThat(actualMailServerValue, is(equalTo(RESOLVED_MAIL_SERVER_VALUE)));
163   }
164 
165   private static PropertyDescriptor createDescriptor(final String keyName)
166   {
167     final PropertyDescriptorBuilder builder = PropertyDescriptorBuilder.a();
168     final PropertyKeyBuilder keyBuilder = PropertyKeyBuilder.a();
169     keyBuilder.withName(keyName).withPropertySet(null);
170     final PropertyKey key = keyBuilder.build();
171     builder.with(key);
172 
173     return builder.build();
174   }
175 
176   // --- tests ----------------------------------------------------------------
177 
178   @Test
179   public void settingPropertyClearesDependentProperty()
180   {
181     fillCache();
182 
183     final PropertyKey key = PropertyKey.create(null, DOMAIN_KEY);
184     uut.setProperty(key, NEW_DOMAIN_VALUE);
185 
186     final String actualDomainValue =
187         (String) uut.getPropertyValue(createDescriptor(DOMAIN_KEY));
188     final String actualServerValue =
189         (String) uut.getPropertyValue(createDescriptor(SERVER_KEY));
190     final String actualMailServerValue =
191         (String) uut.getPropertyValue(createDescriptor(MAIL_SERVER_KEY));
192 
193     assertThat(actualDomainValue, is(equalTo(NEW_DOMAIN_VALUE)));
194     assertThat(actualServerValue, is(equalTo(NEW_RESOLVED_SERVER_VALUE)));
195     assertThat(actualMailServerValue,
196         is(equalTo(NEW_RESOLVED_MAIL_SERVER_VALUE)));
197   }
198 }