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  
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import de.smartics.properties.api.config.domain.Property;
28  import de.smartics.properties.api.config.domain.PropertyLocation;
29  import de.smartics.properties.api.config.domain.ResolvedProperty;
30  import de.smartics.properties.api.core.domain.PropertyKey;
31  import de.smartics.properties.impl.config.cache.CacheConfigurationPropertiesManagement;
32  import de.smartics.properties.spi.config.support.ConfigurationPropertiesManagementSpi;
33  import de.smartics.testdoc.annotations.Uut;
34  
35  /**
36   * Tests {@link CacheConfigurationPropertiesManagement}.
37   */
38  public class CacheConfigurationPropertiesManagementTest
39  {
40  
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    private static final String NO_DEFAULT_EXPRESSION = null;
46  
47    private static final PropertyLocation TEST_SOURCE = new PropertyLocation(
48        "test");
49  
50    private static final String DOMAIN_KEY = "domain";
51  
52    private static final String DOMAIN_VALUE = "example.com";
53  
54    private static final String NEW_DOMAIN_VALUE = "new.example.com";
55  
56    private static final String SERVER_KEY = "server";
57  
58    private static final String SERVER_VALUE = "www.${domain}";
59  
60    private static final String RESOLVED_SERVER_VALUE = "www.example.com";
61  
62    private static final String NEW_RESOLVED_SERVER_VALUE = "www.new.example.com";
63  
64    private static final String MAIL_SERVER_KEY = "mail.server";
65  
66    private static final String MAIL_SERVER_VALUE = null;
67  
68    private static final String MAIL_SERVER_EXPRESSION = "mail-${server}";
69  
70    private static final String RESOLVED_MAIL_SERVER_VALUE =
71        "mail-www.example.com";
72  
73    private static final String NEW_RESOLVED_MAIL_SERVER_VALUE =
74        "mail-www.new.example.com";
75  
76    // --- members --------------------------------------------------------------
77  
78    @Uut
79    private CacheConfigurationPropertiesManagement uut;
80  
81    // ****************************** Inner Classes *****************************
82  
83    // ********************************* Methods ********************************
84  
85    // --- prepare --------------------------------------------------------------
86  
87    @Before
88    public void setUp()
89    {
90      final ConfigurationPropertiesManagementSpi delegate =
91          mock(ConfigurationPropertiesManagementSpi.class);
92  
93      when(delegate.getResolvedProperty(DOMAIN_KEY, null)).thenReturn(
94          new ResolvedProperty(
95              new Property(TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE),
96              NO_DEFAULT_EXPRESSION, DOMAIN_VALUE),
97          new ResolvedProperty(
98              new Property(TEST_SOURCE, DOMAIN_KEY, DOMAIN_VALUE),
99              NO_DEFAULT_EXPRESSION, NEW_DOMAIN_VALUE));
100     when(delegate.getResolvedProperty(SERVER_KEY, null)).thenReturn(
101         new ResolvedProperty(
102             new Property(TEST_SOURCE, SERVER_KEY, SERVER_VALUE),
103             NO_DEFAULT_EXPRESSION, RESOLVED_SERVER_VALUE),
104         new ResolvedProperty(
105             new Property(TEST_SOURCE, SERVER_KEY, SERVER_VALUE),
106             NO_DEFAULT_EXPRESSION, NEW_RESOLVED_SERVER_VALUE));
107     when(delegate.getResolvedProperty(MAIL_SERVER_KEY, null)).thenReturn(
108         new ResolvedProperty(new Property(TEST_SOURCE, MAIL_SERVER_KEY,
109             MAIL_SERVER_VALUE), MAIL_SERVER_EXPRESSION,
110             RESOLVED_MAIL_SERVER_VALUE),
111         new ResolvedProperty(new Property(TEST_SOURCE, MAIL_SERVER_KEY, null),
112             MAIL_SERVER_EXPRESSION, NEW_RESOLVED_MAIL_SERVER_VALUE));
113 
114     uut = new CacheConfigurationPropertiesManagement(delegate);
115 
116   }
117 
118   // --- helper ---------------------------------------------------------------
119 
120   private void fillCache()
121   {
122     final String actualDomainValue = (String) uut.getPropertyValue(DOMAIN_KEY);
123     final String actualServerValue = (String) uut.getPropertyValue(SERVER_KEY);
124     final String actualMailServerValue =
125         (String) uut.getPropertyValue(MAIL_SERVER_KEY);
126 
127     assertThat(actualDomainValue, is(equalTo(DOMAIN_VALUE)));
128     assertThat(actualServerValue, is(equalTo(RESOLVED_SERVER_VALUE)));
129     assertThat(actualMailServerValue, is(equalTo(RESOLVED_MAIL_SERVER_VALUE)));
130   }
131 
132   // --- tests ----------------------------------------------------------------
133 
134   @Test
135   public void settingPropertyClearesDependentProperty()
136   {
137     fillCache();
138 
139     final PropertyKey key = PropertyKey.create(null, DOMAIN_KEY);
140     uut.setProperty(key, NEW_DOMAIN_VALUE);
141 
142     final String actualDomainValue = (String) uut.getPropertyValue(DOMAIN_KEY);
143     final String actualServerValue = (String) uut.getPropertyValue(SERVER_KEY);
144     final String actualMailServerValue =
145         (String) uut.getPropertyValue(MAIL_SERVER_KEY);
146 
147     assertThat(actualDomainValue, is(equalTo(NEW_DOMAIN_VALUE)));
148     assertThat(actualServerValue, is(equalTo(NEW_RESOLVED_SERVER_VALUE)));
149     assertThat(actualMailServerValue,
150         is(equalTo(NEW_RESOLVED_MAIL_SERVER_VALUE)));
151   }
152 }