a2d36ef0858871dd14e260f98aba5c2e9b0a094c
[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.openecomp.sdc.logging.slf4j;
18
19 import static org.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertNull;
21 import static org.testng.Assert.assertTrue;
22
23 import java.util.HashMap;
24 import org.openecomp.sdc.logging.api.ContextData;
25 import org.testng.annotations.Test;
26
27 /**
28  * Unit-test retrieving values from client-provided request data.
29  *
30  * @author evitaliy
31  * @since 23 Mar 2018
32  */
33 public class RequestContextProviderTest {
34
35     @Test
36     public void valuesEmptyWhenInputDataEmpty() {
37         RequestContextProvider provider = RequestContextProvider.from(ContextData.builder().build());
38         assertTrue(provider.values().isEmpty());
39     }
40
41     @Test
42     public void serviceNameReturnedWhenSuppliedInData() {
43         final String service = "supplied-service-name";
44         RequestContextProvider provider =
45                 RequestContextProvider.from(ContextData.builder().serviceName(service).build());
46         assertEquals(provider.values().get(ContextField.SERVICE_NAME), service);
47     }
48
49     @Test
50     public void partnerNameReturnedWhenSuppliedInData() {
51         final String partner = "supplied-partner-name";
52         RequestContextProvider provider =
53                 RequestContextProvider.from(ContextData.builder().partnerName(partner).build());
54         assertEquals(provider.values().get(ContextField.PARTNER_NAME), partner);
55     }
56
57     @Test
58     public void requestIdReturnedWhenSuppliedInData() {
59         final String request = "supplied-request-id";
60         RequestContextProvider provider =
61                 RequestContextProvider.from(ContextData.builder().requestId(request).build());
62         assertEquals(provider.values().get(ContextField.REQUEST_ID), request);
63     }
64
65     @Test
66     public void dataEmptyWhenValuesEmpty() {
67         ContextData data = RequestContextProvider.to(new HashMap<>());
68         assertNull(data.getPartnerName());
69         assertNull(data.getRequestId());
70         assertNull(data.getServiceName());
71     }
72
73     @Test
74     public void serviceNameInDataWhenSuppliedInValues() {
75         final String service = "values-service-name";
76         HashMap<ContextField, String> values = new HashMap<>();
77         values.put(ContextField.SERVICE_NAME, service);
78         ContextData data = RequestContextProvider.to(values);
79         assertEquals(data.getServiceName(), service);
80     }
81
82     @Test
83     public void partnerNameInDataWhenSuppliedInValues() {
84         final String partner = "values-partner-name";
85         HashMap<ContextField, String> values = new HashMap<>();
86         values.put(ContextField.PARTNER_NAME, partner);
87         ContextData data = RequestContextProvider.to(values);
88         assertEquals(data.getPartnerName(), partner);
89     }
90
91     @Test
92     public void requestIdInDataWhenSuppliedInValues() {
93         final String request = "values-request-id";
94         HashMap<ContextField, String> values = new HashMap<>();
95         values.put(ContextField.REQUEST_ID, request);
96         ContextData data = RequestContextProvider.to(values);
97         assertEquals(data.getRequestId(), request);
98     }
99 }