2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.oam.processor;
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;
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;
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({BaseCommon.class, MDC.class})
58 public class BaseCommonTest {
59 private class TestAbc extends BaseCommon {
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
69 TestAbc(EELFLogger eelfLogger,
70 ConfigurationHelper configurationHelperIn,
71 StateHelper stateHelperIn,
72 OperationHelper operationHelperIn) {
73 super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
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);
83 public void setUp() throws Exception {
84 testBaseCommon = spy(new TestAbc(null, mockConfigHelper, mockStateHelper, null));
86 Whitebox.setInternalState(testBaseCommon, "commonHeader", mockCommonHeader);
87 Whitebox.setInternalState(testBaseCommon, "rpc", AppcOam.RPC.maintenance_mode);
89 // to avoid operation on logger fail, mock up the logger
90 EELFLogger mockLogger = mock(EELFLogger.class);
91 Whitebox.setInternalState(testBaseCommon, "logger", mockLogger);
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());
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));
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());
121 String exceptionMessage = "testing";
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);
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);
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);
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));
157 public void testClearRequestLogProperties() throws Exception {
158 mockStatic(MDC.class);
159 testBaseCommon.clearRequestLogProperties();
160 PowerMockito.verifyStatic(times(5));
164 public void testResetLogProperties() throws Exception {
165 Mockito.doReturn("SOME HOST NAME").when(testBaseCommon).getHostInfo(Mockito.anyString());
166 testBaseCommon.setInitialLogProperties();
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);
174 testBaseCommon.resetLogProperties(false);
175 Mockito.verify(mockCommonHeader, times(3)).getRequestId();
176 Mockito.verify(mockCommonHeader, times(3)).getOriginatorId();
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);