16136ae2ed21767f22d5fd8df820c470d3aa09ed
[clamp.git] / src / test / java / org / onap / clamp / flow / FlowLogOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Samsung. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END============================================
19  * ===================================================================
20  *
21  */
22
23 package org.onap.clamp.flow;
24
25 import static junit.framework.Assert.assertEquals;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.mockito.Mockito.mock;
28 import org.apache.camel.CamelContext;
29 import org.apache.camel.Exchange;
30 import org.apache.camel.impl.DefaultExchange;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.clamp.clds.util.LoggingUtils;
34 import org.onap.clamp.clds.util.ONAPLogConstants;
35 import org.onap.clamp.flow.log.FlowLogOperation;
36 import org.slf4j.MDC;
37 import org.slf4j.spi.MDCAdapter;
38 import org.springframework.test.util.ReflectionTestUtils;
39
40 public class FlowLogOperationTest {
41
42     private FlowLogOperation flowLogOperation = new FlowLogOperation();
43
44     @Test
45     public void testStratLog() {
46         //given
47         Exchange exchange = new DefaultExchange(mock(CamelContext.class));
48         LoggingUtils loggingUtils = mock(LoggingUtils.class);
49         ReflectionTestUtils.setField(flowLogOperation, "util", loggingUtils);
50
51         //when
52         Mockito.when(loggingUtils.getProperties(ONAPLogConstants.MDCs.REQUEST_ID)).thenReturn("MockRequestId");
53         Mockito.when(loggingUtils.getProperties(ONAPLogConstants.MDCs.INVOCATION_ID)).thenReturn("MockInvocationId");
54         Mockito.when(loggingUtils.getProperties(ONAPLogConstants.MDCs.PARTNER_NAME)).thenReturn("MockPartnerName");
55         flowLogOperation.startLog(exchange, "serviceName");
56
57         //then
58         assertThat(exchange.getProperty(ONAPLogConstants.Headers.REQUEST_ID)).isEqualTo("MockRequestId");
59         assertThat(exchange.getProperty(ONAPLogConstants.Headers.INVOCATION_ID)).isEqualTo("MockInvocationId");
60         assertThat(exchange.getProperty(ONAPLogConstants.Headers.PARTNER_NAME)).isEqualTo("MockPartnerName");
61     }
62
63     @Test
64     public void testInvokeLog() {
65         //given
66         final String mockEntity = "mockEntity";
67         final String mockServiceName = "mockSerivceName";
68         MDCAdapter mdcAdapter = MDC.getMDCAdapter();
69         //when
70         flowLogOperation.invokeLog(mockEntity, mockServiceName);
71         //then
72         String entity = mdcAdapter.get(ONAPLogConstants.MDCs.TARGET_ENTITY);
73         String serviceName = mdcAdapter.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME);
74         assertEquals(entity,mockEntity);
75         assertEquals(serviceName,mockServiceName);
76     }
77
78    @Test
79     public void testEndLog() {
80         //given
81        MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, "2019-05-19T00:00:00.007Z");
82        MDCAdapter mdcAdapter = MDC.getMDCAdapter();
83        ///when
84        flowLogOperation.endLog();
85        //then
86        assertThat(mdcAdapter.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP)).isNull();
87     }
88
89     @Test
90     public void testErrorLog() {
91         //given
92         MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, "2019-05-19T00:00:00.007Z");
93         MDCAdapter mdcAdapter = MDC.getMDCAdapter();
94         //when
95         flowLogOperation.errorLog();
96         //then
97         assertThat(mdcAdapter.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP)).isNull();
98     }
99 }