7e3fd5d2c98393e664e012db559a1dec5609a1b6
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
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
17 package org.onap.config.api;
18
19 import java.util.concurrent.CompletableFuture;
20 import java.util.concurrent.ExecutionException;
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.ArgumentMatchers.same;
31 import static org.mockito.Mockito.doCallRealMethod;
32 import static org.mockito.Mockito.when;
33
34 /**
35  * @author evitaliy
36  * @since 28 Oct 2018
37  */
38 @RunWith(MockitoJUnitRunner.class)
39 public class ConfigurationTest {
40
41     @Mock
42     private Configuration configuration;
43
44     @After
45     public void cleanUp() {
46         Configuration.TENANT.remove();
47     }
48
49     @Test
50     public void tenantRetrievedWhenPreviouslySet() {
51         final String tenantId = "abc";
52         Configuration.setTenantId(tenantId);
53         Assert.assertEquals(tenantId, Configuration.TENANT.get());
54     }
55
56     @Test
57     public void tenantEmptyWhenNeverSet() {
58         Assert.assertNull(Configuration.TENANT.get());
59     }
60
61     @Test
62     public void tenantNullWhenNullSet() {
63         Configuration.setTenantId("xyz");
64         Configuration.setTenantId(null);
65         Assert.assertNull(Configuration.TENANT.get());
66     }
67
68     @Test
69     public void tenantNullWhenEmptySet() {
70         Configuration.setTenantId("xyz");
71         Configuration.setTenantId("");
72         Assert.assertNull(Configuration.TENANT.get());
73     }
74
75     @Test
76     public void tenantDoesNotPropagateToAnotherThread() throws ExecutionException, InterruptedException {
77         final String currentTenant = "xyz";
78         Configuration.setTenantId(currentTenant);
79         CompletableFuture<String> result = new CompletableFuture<>();
80         Thread otherThread = new Thread(() -> result.complete(Configuration.TENANT.get()));
81         otherThread.start();
82         Assert.assertNull("Tenant in the other thread expected to be null", result.get());
83         Assert.assertEquals(currentTenant, Configuration.TENANT.get());
84     }
85
86     @Test
87     public void testGetAsString() {
88         doCallRealMethod().when(configuration).getAsString(anyString());
89         doCallRealMethod().when(configuration).getAsString(any(), anyString());
90         doCallRealMethod().when(configuration).getAsString(any(), any(), anyString());
91         when(configuration.get(any(), any(), anyString(), same(String.class))).thenReturn("42");
92
93         Assert.assertEquals(String.class, configuration.getAsString("key").getClass());
94         Assert.assertEquals("42", configuration.getAsString("key"));
95     }
96
97     @Test
98     public void testGetAsByte() {
99         doCallRealMethod().when(configuration).getAsByteValue(anyString());
100         doCallRealMethod().when(configuration).getAsByteValue(any(), anyString());
101         doCallRealMethod().when(configuration).getAsByteValue(any(), any(), anyString());
102         when(configuration.get(any(), any(), anyString(), same(Byte.class))).thenReturn((byte) 42);
103
104         Assert.assertEquals(Byte.class, configuration.getAsByteValue("key").getClass());
105         Assert.assertEquals(Byte.valueOf((byte) 42), configuration.getAsByteValue("key"));
106     }
107
108     @Test
109     public void testGetAsShort() {
110         doCallRealMethod().when(configuration).getAsShortValue(anyString());
111         doCallRealMethod().when(configuration).getAsShortValue(any(), anyString());
112         doCallRealMethod().when(configuration).getAsShortValue(any(), any(), anyString());
113         when(configuration.get(any(), any(), anyString(), same(Short.class))).thenReturn((short) 42);
114
115         Assert.assertEquals(Short.class, configuration.getAsShortValue("key").getClass());
116         Assert.assertEquals(Short.valueOf((short) 42), configuration.getAsShortValue("key"));
117     }
118
119     @Test
120     public void testGetAsInteger() {
121         doCallRealMethod().when(configuration).getAsIntegerValue(anyString());
122         doCallRealMethod().when(configuration).getAsIntegerValue(any(), anyString());
123         doCallRealMethod().when(configuration).getAsIntegerValue(any(), any(), anyString());
124         when(configuration.get(any(), any(), anyString(), same(Integer.class))).thenReturn(42);
125
126         Assert.assertEquals(Integer.class, configuration.getAsIntegerValue("key").getClass());
127         Assert.assertEquals(Integer.valueOf(42), configuration.getAsIntegerValue("key"));
128     }
129
130     @Test
131     public void testGetAsLong() {
132         doCallRealMethod().when(configuration).getAsLongValue(anyString());
133         doCallRealMethod().when(configuration).getAsLongValue(any(), anyString());
134         doCallRealMethod().when(configuration).getAsLongValue(any(), any(), anyString());
135         when(configuration.get(any(), any(), anyString(), same(Long.class))).thenReturn((long) 42);
136
137         Assert.assertEquals(Long.class, configuration.getAsLongValue("key").getClass());
138         Assert.assertEquals(Long.valueOf(42), configuration.getAsLongValue("key"));
139     }
140
141     @Test
142     public void testGetAsDouble() {
143         doCallRealMethod().when(configuration).getAsDoubleValue(anyString());
144         doCallRealMethod().when(configuration).getAsDoubleValue(any(), anyString());
145         doCallRealMethod().when(configuration).getAsDoubleValue(any(), any(), anyString());
146         when(configuration.get(any(), any(), anyString(), same(Double.class))).thenReturn((double) 42);
147
148         Assert.assertEquals(Double.class, configuration.getAsDoubleValue("key").getClass());
149         Assert.assertEquals(Double.valueOf(42), configuration.getAsDoubleValue("key"));
150     }
151
152     @Test
153     public void testGetAsFloat() {
154         doCallRealMethod().when(configuration).getAsFloatValue(anyString());
155         doCallRealMethod().when(configuration).getAsFloatValue(any(), anyString());
156         doCallRealMethod().when(configuration).getAsFloatValue(any(), any(), anyString());
157         when(configuration.get(any(), any(), anyString(), same(Float.class))).thenReturn((float) 42);
158
159         Assert.assertEquals(Float.class, configuration.getAsFloatValue("key").getClass());
160         Assert.assertEquals(Float.valueOf(42), configuration.getAsFloatValue("key"));
161     }
162
163     @Test
164     public void testGetAsBoolean() {
165         doCallRealMethod().when(configuration).getAsBooleanValue(anyString());
166         doCallRealMethod().when(configuration).getAsBooleanValue(any(), anyString());
167         doCallRealMethod().when(configuration).getAsBooleanValue(any(), any(), anyString());
168         when(configuration.get(any(), any(), anyString(), same(Boolean.class))).thenReturn(true);
169
170         Assert.assertEquals(Boolean.class, configuration.getAsBooleanValue("key").getClass());
171         Assert.assertEquals(Boolean.TRUE, configuration.getAsBooleanValue("key"));
172     }
173
174     @Test
175     public void testGetAsCharacter() {
176         doCallRealMethod().when(configuration).getAsCharValue(anyString());
177         doCallRealMethod().when(configuration).getAsCharValue(any(), anyString());
178         doCallRealMethod().when(configuration).getAsCharValue(any(), any(), anyString());
179         when(configuration.get(any(), any(), anyString(), same(Character.class))).thenReturn('\u0042');
180
181         Assert.assertEquals(Character.class, configuration.getAsCharValue("key").getClass());
182         Assert.assertEquals(Character.valueOf('\u0042'), configuration.getAsCharValue("key"));
183     }
184 }