View Javadoc

1   /*
2    * Copyright 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.tutorial.tenant;
17  
18  import static de.smartics.properties.impl.config.domain.key.envapp.EnvAppConfigurationKeyHelper.SYSTEM_PROPERTY_ENVIRONMENT_NAME;
19  import static org.hamcrest.MatcherAssert.assertThat;
20  import static org.hamcrest.Matchers.is;
21  
22  import org.junit.BeforeClass;
23  import org.junit.Test;
24  
25  import de.smartics.projectdoc.annotations.DocCategory;
26  import de.smartics.projectdoc.annotations.Document;
27  import de.smartics.projectdoc.annotations.topic.DocChapter;
28  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactory;
29  import de.smartics.properties.api.config.app.ConfigurationPropertiesFactoryFactory;
30  import de.smartics.properties.api.config.domain.ConfigurationPropertiesManagement;
31  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
32  import de.smartics.properties.api.config.domain.key.ConfigurationKeyHelper;
33  import de.smartics.properties.impl.config.domain.key.rtaware.ThreadLocalRuntimeAdapter;
34  import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContext;
35  import de.smartics.properties.spi.config.domain.key.ConfigurationKeyContextManager;
36  import de.smartics.sandbox.mail.MailProperties;
37  
38  /**
39   * This tutorial shows tenant and user specific configurations at work.
40   */
41  @Document(title = "Tenant and User specific Configurations",
42      sortKey = "basics3000")
43  @DocCategory({ "basics" })
44  // @DocTopic(path="basics", step="3000")
45  public class TenantAndUserSupportTutorial
46  {
47    private static ConfigurationPropertiesFactory factory;
48  
49    @BeforeClass
50    public static void setUpClass()
51    {
52      factory = ConfigurationPropertiesFactoryFactory.createDefaultFactory();
53    }
54  
55    @DocChapter
56    @Test
57    public void accessUserConfiguration()
58    {
59      System.setProperty(SYSTEM_PROPERTY_ENVIRONMENT_NAME, "test");
60      ThreadLocalRuntimeAdapter.setTenantId("corp");
61      ThreadLocalRuntimeAdapter.setUserId("jane");
62  
63      try
64      {
65        final MailProperties properties = configure();
66  
67        final int mailsPerPage = properties.mailsPerPage();
68        assertThat(mailsPerPage, is(1));
69      }
70      finally
71      {
72        ThreadLocalRuntimeAdapter.remove();
73      }
74    }
75  
76    @DocChapter
77    @Test
78    public void accessTenantConfiguration()
79    {
80      System.setProperty(SYSTEM_PROPERTY_ENVIRONMENT_NAME, "test");
81      ThreadLocalRuntimeAdapter.setTenantId("corp");
82  
83      try
84      {
85        final MailProperties properties = configure();
86  
87        final int mailsPerPage = properties.mailsPerPage();
88        assertThat(mailsPerPage, is(2));
89      }
90      finally
91      {
92        ThreadLocalRuntimeAdapter.remove();
93      }
94    }
95  
96    @DocChapter
97    @Test
98    public void accessEnvironmentConfiguration()
99    {
100     System.setProperty(SYSTEM_PROPERTY_ENVIRONMENT_NAME, "test");
101 
102     try
103     {
104       final MailProperties properties = configure();
105 
106       final int mailsPerPage = properties.mailsPerPage();
107       assertThat(mailsPerPage, is(3));
108     }
109     finally
110     {
111       ThreadLocalRuntimeAdapter.remove();
112     }
113   }
114 
115   private MailProperties configure()
116   {
117     final ConfigurationKeyContext context =
118         ConfigurationKeyContextManager.INSTANCE.context();
119 
120     final ConfigurationKeyHelper helper = context.configurationKeyHelper(true);
121     final Class<?> locator = TenantAndUserSupportTutorial.class;
122     final ConfigurationKey<?> key = helper.load(locator);
123 
124     final ConfigurationPropertiesManagement config =
125         factory.createManagement(key);
126 
127     final MailProperties properties =
128         config.getProperties(MailProperties.class);
129     return properties;
130   }
131 }