Checkstyle fixes
[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
29 import org.apache.camel.CamelContext;
30 import org.apache.camel.Exchange;
31 import org.apache.camel.impl.DefaultExchange;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.onap.clamp.clds.util.LoggingUtils;
35 import org.onap.clamp.clds.util.OnapLogConstants;
36 import org.onap.clamp.flow.log.FlowLogOperation;
37 import org.slf4j.MDC;
38 import org.slf4j.spi.MDCAdapter;
39 import org.springframework.test.util.ReflectionTestUtils;
40
41 public class FlowLogOperationTest {
42
43     private FlowLogOperation flowLogOperation = new FlowLogOperation();
44
45     @Test
46     public void testStratLog() {
47         // given
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         Exchange exchange = new DefaultExchange(mock(CamelContext.class));
56         flowLogOperation.startLog(exchange, "serviceName");
57
58         // then
59         assertThat(exchange.getProperty(OnapLogConstants.Headers.REQUEST_ID)).isEqualTo("MockRequestId");
60         assertThat(exchange.getProperty(OnapLogConstants.Headers.INVOCATION_ID)).isEqualTo("MockInvocationId");
61         assertThat(exchange.getProperty(OnapLogConstants.Headers.PARTNER_NAME)).isEqualTo("MockPartnerName");
62     }
63
64     @Test
65     public void testInvokeLog() {
66         // given
67         final String mockEntity = "mockEntity";
68         final String mockServiceName = "mockSerivceName";
69         MDCAdapter mdcAdapter = MDC.getMDCAdapter();
70         // when
71         flowLogOperation.invokeLog(mockEntity, mockServiceName);
72         // then
73         String entity = mdcAdapter.get(OnapLogConstants.Mdcs.TARGET_ENTITY);
74         String serviceName = mdcAdapter.get(OnapLogConstants.Mdcs.TARGET_SERVICE_NAME);
75         assertEquals(entity, mockEntity);
76         assertEquals(serviceName, mockServiceName);
77     }
78
79     @Test
80     public void testEndLog() {
81         // given
82         MDC.put(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP, "2019-05-19T00:00:00.007Z");
83         MDCAdapter mdcAdapter = MDC.getMDCAdapter();
84         /// when
85         flowLogOperation.endLog();
86         // then
87         assertThat(mdcAdapter.get(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP)).isNull();
88     }
89
90     @Test
91     public void testErrorLog() {
92         // given
93         MDC.put(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP, "2019-05-19T00:00:00.007Z");
94         MDCAdapter mdcAdapter = MDC.getMDCAdapter();
95         // when
96         flowLogOperation.errorLog();
97         // then
98         assertThat(mdcAdapter.get(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP)).isNull();
99     }
100 }