2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.config.api;
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;
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;
38 @RunWith(MockitoJUnitRunner.class)
39 public class ConfigurationTest {
42 private Configuration configuration;
45 public void cleanUp() {
46 Configuration.TENANT.remove();
50 public void tenantRetrievedWhenPreviouslySet() {
51 final String tenantId = "abc";
52 Configuration.setTenantId(tenantId);
53 Assert.assertEquals(tenantId, Configuration.TENANT.get());
57 public void tenantEmptyWhenNeverSet() {
58 Assert.assertNull(Configuration.TENANT.get());
62 public void tenantNullWhenNullSet() {
63 Configuration.setTenantId("xyz");
64 Configuration.setTenantId(null);
65 Assert.assertNull(Configuration.TENANT.get());
69 public void tenantNullWhenEmptySet() {
70 Configuration.setTenantId("xyz");
71 Configuration.setTenantId("");
72 Assert.assertNull(Configuration.TENANT.get());
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()));
82 Assert.assertNull("Tenant in the other thread expected to be null", result.get());
83 Assert.assertEquals(currentTenant, Configuration.TENANT.get());
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");
93 Assert.assertEquals(String.class, configuration.getAsString("key").getClass());
94 Assert.assertEquals("42", configuration.getAsString("key"));
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);
104 Assert.assertEquals(Byte.class, configuration.getAsByteValue("key").getClass());
105 Assert.assertEquals(Byte.valueOf((byte) 42), configuration.getAsByteValue("key"));
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);
115 Assert.assertEquals(Short.class, configuration.getAsShortValue("key").getClass());
116 Assert.assertEquals(Short.valueOf((short) 42), configuration.getAsShortValue("key"));
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);
126 Assert.assertEquals(Integer.class, configuration.getAsIntegerValue("key").getClass());
127 Assert.assertEquals(Integer.valueOf(42), configuration.getAsIntegerValue("key"));
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);
137 Assert.assertEquals(Long.class, configuration.getAsLongValue("key").getClass());
138 Assert.assertEquals(Long.valueOf(42), configuration.getAsLongValue("key"));
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);
148 Assert.assertEquals(Double.class, configuration.getAsDoubleValue("key").getClass());
149 Assert.assertEquals(Double.valueOf(42), configuration.getAsDoubleValue("key"));
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);
159 Assert.assertEquals(Float.class, configuration.getAsFloatValue("key").getClass());
160 Assert.assertEquals(Float.valueOf(42), configuration.getAsFloatValue("key"));
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);
170 Assert.assertEquals(Boolean.class, configuration.getAsBooleanValue("key").getClass());
171 Assert.assertEquals(Boolean.TRUE, configuration.getAsBooleanValue("key"));
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');
181 Assert.assertEquals(Character.class, configuration.getAsCharValue("key").getClass());
182 Assert.assertEquals(Character.valueOf('\u0042'), configuration.getAsCharValue("key"));