aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/src/test/java/org/onap/policy/clamp/flow/FlowLogOperationTest.java
blob: 622fd59992af4f56919271d1f3289617333bd34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*-
 * ============LICENSE_START=======================================================
 * ONAP POLICY-CLAMP
 * ================================================================================
 * Copyright (C) 2019 Samsung. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2021 AT&T
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.policy.clamp.flow;

import static junit.framework.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.support.DefaultExchange;
import org.junit.Test;
import org.mockito.Mockito;
import org.onap.policy.clamp.clds.util.LoggingUtils;
import org.onap.policy.clamp.clds.util.OnapLogConstants;
import org.onap.policy.clamp.flow.log.FlowLogOperation;
import org.slf4j.MDC;
import org.slf4j.spi.MDCAdapter;
import org.springframework.test.util.ReflectionTestUtils;

public class FlowLogOperationTest {

    private FlowLogOperation flowLogOperation = new FlowLogOperation();

    @Test
    public void testStartLog() {
        // given
        LoggingUtils loggingUtils = mock(LoggingUtils.class);
        ReflectionTestUtils.setField(flowLogOperation, "util", loggingUtils);

        // when
        Mockito.when(loggingUtils.getProperties(OnapLogConstants.Mdcs.REQUEST_ID)).thenReturn("MockRequestId");
        Mockito.when(loggingUtils.getProperties(OnapLogConstants.Mdcs.INVOCATION_ID)).thenReturn("MockInvocationId");
        Mockito.when(loggingUtils.getProperties(OnapLogConstants.Mdcs.PARTNER_NAME)).thenReturn("MockPartnerName");
        Exchange exchange = new DefaultExchange(mock(CamelContext.class));
        flowLogOperation.startLog(exchange, "serviceName");

        // then
        assertThat(exchange.getProperty(OnapLogConstants.Headers.REQUEST_ID)).isEqualTo("MockRequestId");
        assertThat(exchange.getProperty(OnapLogConstants.Headers.INVOCATION_ID)).isEqualTo("MockInvocationId");
        assertThat(exchange.getProperty(OnapLogConstants.Headers.PARTNER_NAME)).isEqualTo("MockPartnerName");
    }

    @Test
    public void testInvokeLog() {
        // given
        final String mockEntity = "mockEntity";
        final String mockServiceName = "mockServiceName";
        MDCAdapter mdcAdapter = MDC.getMDCAdapter();
        // when
        flowLogOperation.invokeLog(mockEntity, mockServiceName);
        // then
        String entity = mdcAdapter.get(OnapLogConstants.Mdcs.TARGET_ENTITY);
        String serviceName = mdcAdapter.get(OnapLogConstants.Mdcs.TARGET_SERVICE_NAME);
        assertEquals(entity, mockEntity);
        assertEquals(serviceName, mockServiceName);
    }

    @Test
    public void testEndLog() {
        // given
        MDC.put(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP, "2019-05-19T00:00:00.007Z");
        MDCAdapter mdcAdapter = MDC.getMDCAdapter();
        /// when
        flowLogOperation.endLog();
        // then
        assertThat(mdcAdapter.get(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP)).isNull();
    }

    @Test
    public void testErrorLog() {
        // given
        MDC.put(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP, "2019-05-19T00:00:00.007Z");
        MDCAdapter mdcAdapter = MDC.getMDCAdapter();
        // when
        flowLogOperation.errorLog();
        // then
        assertThat(mdcAdapter.get(OnapLogConstants.Mdcs.ENTRY_TIMESTAMP)).isNull();
    }
}