60d9e3c9210fcffd6060d23017dfb347e854538e
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / onap / appc / oam / processor / BaseCommonTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2018 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.oam.processor;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mockito;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
36 import org.onap.appc.exceptions.InvalidInputException;
37 import org.onap.appc.exceptions.InvalidStateException;
38 import org.onap.appc.oam.AppcOam;
39 import org.onap.appc.oam.OAMCommandStatus;
40 import org.onap.appc.oam.util.ConfigurationHelper;
41 import org.onap.appc.oam.util.OperationHelper;
42 import org.onap.appc.oam.util.StateHelper;
43 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.powermock.reflect.Whitebox;
48 import org.slf4j.MDC;
49 import java.util.Map;
50
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.spy;
53 import static org.mockito.Mockito.times;
54 import static org.powermock.api.mockito.PowerMockito.mockStatic;
55
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({BaseCommon.class, MDC.class})
58 public class BaseCommonTest {
59     private class TestAbc extends BaseCommon {
60
61         /**
62          * Constructor
63          *
64          * @param eelfLogger            for logging
65          * @param configurationHelperIn for property reading
66          * @param stateHelperIn         for APP-C OAM state checking
67          * @param operationHelperIn     for operational helper
68          */
69         TestAbc(EELFLogger eelfLogger,
70                 ConfigurationHelper configurationHelperIn,
71                 StateHelper stateHelperIn,
72                 OperationHelper operationHelperIn) {
73             super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
74         }
75     }
76
77     private TestAbc testBaseCommon;
78     private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class);
79     private StateHelper mockStateHelper = mock(StateHelper.class);
80     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
81
82     @Before
83     public void setUp() throws Exception {
84         testBaseCommon = spy(new TestAbc(null, mockConfigHelper, mockStateHelper, null));
85
86         Whitebox.setInternalState(testBaseCommon, "commonHeader", mockCommonHeader);
87         Whitebox.setInternalState(testBaseCommon, "rpc", AppcOam.RPC.maintenance_mode);
88
89         // to avoid operation on logger fail, mock up the logger
90         EELFLogger mockLogger = mock(EELFLogger.class);
91         Whitebox.setInternalState(testBaseCommon, "logger", mockLogger);
92     }
93
94     @Test
95     public void testSetStatus() throws Exception {
96         OAMCommandStatus oamCommandStatus = OAMCommandStatus.ACCEPTED;
97         testBaseCommon.setStatus(oamCommandStatus);
98         Status status = testBaseCommon.status;
99         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
100         Assert.assertEquals("Should have message", oamCommandStatus.getResponseMessage(), status.getMessage());
101     }
102
103     @Test
104     public void testSetStatusWithParams() throws Exception {
105         String message = "testing";
106         OAMCommandStatus oamCommandStatus = OAMCommandStatus.REJECTED;
107         testBaseCommon.setStatus(oamCommandStatus, message);
108         Status status = testBaseCommon.status;
109         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
110         Assert.assertTrue("Should have message", status.getMessage().endsWith(message));
111     }
112
113     @Test
114     public void testSetErrorStatus() throws Exception {
115         Mockito.doReturn("testName").when(mockConfigHelper).getAppcName();
116         Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState();
117         Mockito.doReturn("testRequestId").when(mockCommonHeader).getRequestId();
118         Mockito.doReturn("testOrigId").when(mockCommonHeader).getOriginatorId();
119         Mockito.doReturn("SOME HOST NAME").when(testBaseCommon).getHostInfo(Mockito.anyString());
120
121         String exceptionMessage = "testing";
122
123         OAMCommandStatus oamCommandStatus = OAMCommandStatus.INVALID_PARAMETER;
124         Throwable t = new InvalidInputException(exceptionMessage);
125         testBaseCommon.setErrorStatus(t);
126         Status status = testBaseCommon.status;
127         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
128         Mockito.verify(testBaseCommon, times(1)).resetLogProperties(false);
129         Mockito.verify(testBaseCommon, times(1)).resetLogProperties(true);
130
131         oamCommandStatus = OAMCommandStatus.REJECTED;
132         t = new InvalidStateException(exceptionMessage);
133         testBaseCommon.setErrorStatus(t);
134         status = testBaseCommon.status;
135         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
136         Mockito.verify(testBaseCommon, times(2)).resetLogProperties(false);
137         Mockito.verify(testBaseCommon, times(2)).resetLogProperties(true);
138
139         oamCommandStatus = OAMCommandStatus.UNEXPECTED_ERROR;
140         t = new NullPointerException(exceptionMessage);
141         testBaseCommon.setErrorStatus(t);
142         status = testBaseCommon.status;
143         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
144         Mockito.verify(testBaseCommon, times(3)).resetLogProperties(false);
145         Mockito.verify(testBaseCommon, times(3)).resetLogProperties(true);
146     }
147
148     @Test
149     public void testSetInitialLogProperties() throws Exception {
150         mockStatic(MDC.class);
151         Mockito.doReturn("SOME HOST NAME").when(testBaseCommon).getHostInfo(Mockito.anyString());
152         testBaseCommon.setInitialLogProperties();
153         PowerMockito.verifyStatic(times(5));
154     }
155
156     @Test
157     public void testClearRequestLogProperties() throws Exception {
158         mockStatic(MDC.class);
159         testBaseCommon.clearRequestLogProperties();
160         PowerMockito.verifyStatic(times(5));
161     }
162
163     @Test
164     public void testResetLogProperties() throws Exception {
165         Mockito.doReturn("SOME HOST NAME").when(testBaseCommon).getHostInfo(Mockito.anyString());
166         testBaseCommon.setInitialLogProperties();
167
168         testBaseCommon.resetLogProperties(false);
169         Mockito.verify(mockCommonHeader, times(2)).getRequestId();
170         Mockito.verify(mockCommonHeader, times(2)).getOriginatorId();
171         Map<String, String> oldMdcMap = Whitebox.getInternalState(testBaseCommon, "oldMdcContent");
172         Assert.assertTrue("Should have 5 entries in persisted map", oldMdcMap.size() == 5);
173
174         testBaseCommon.resetLogProperties(false);
175         Mockito.verify(mockCommonHeader, times(3)).getRequestId();
176         Mockito.verify(mockCommonHeader, times(3)).getOriginatorId();
177
178         // test oldMdcMap is cleared
179         testBaseCommon.resetLogProperties(false);
180         Mockito.verify(mockCommonHeader, times(4)).getRequestId();
181         Mockito.verify(mockCommonHeader, times(4)).getOriginatorId();
182         oldMdcMap = Whitebox.getInternalState(testBaseCommon, "oldMdcContent");
183         Assert.assertTrue("Should have 5 entries in persisted map", oldMdcMap.size() == 5);
184     }
185 }