2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.oam.processor;
27 import com.att.eelf.configuration.EELFLogger;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader;
34 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.status.Status;
35 import org.openecomp.appc.exceptions.InvalidInputException;
36 import org.openecomp.appc.exceptions.InvalidStateException;
37 import org.openecomp.appc.oam.AppcOam;
38 import org.openecomp.appc.oam.OAMCommandStatus;
39 import org.openecomp.appc.oam.util.ConfigurationHelper;
40 import org.openecomp.appc.oam.util.OperationHelper;
41 import org.openecomp.appc.oam.util.StateHelper;
42 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 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();
120 String exceptionMessage = "testing";
122 OAMCommandStatus oamCommandStatus = OAMCommandStatus.INVALID_PARAMETER;
123 Throwable t = new InvalidInputException(exceptionMessage);
124 testBaseCommon.setErrorStatus(t);
125 Status status = testBaseCommon.status;
126 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
127 Mockito.verify(testBaseCommon, times(1)).resetLogProperties(false);
128 Mockito.verify(testBaseCommon, times(1)).resetLogProperties(true);
130 oamCommandStatus = OAMCommandStatus.REJECTED;
131 t = new InvalidStateException(exceptionMessage);
132 testBaseCommon.setErrorStatus(t);
133 status = testBaseCommon.status;
134 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
135 Mockito.verify(testBaseCommon, times(2)).resetLogProperties(false);
136 Mockito.verify(testBaseCommon, times(2)).resetLogProperties(true);
138 oamCommandStatus = OAMCommandStatus.UNEXPECTED_ERROR;
139 t = new NullPointerException(exceptionMessage);
140 testBaseCommon.setErrorStatus(t);
141 status = testBaseCommon.status;
142 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
143 Mockito.verify(testBaseCommon, times(3)).resetLogProperties(false);
144 Mockito.verify(testBaseCommon, times(3)).resetLogProperties(true);
148 public void testSetInitialLogProperties() throws Exception {
149 mockStatic(MDC.class);
150 testBaseCommon.setInitialLogProperties();
151 PowerMockito.verifyStatic(times(5));
155 public void testClearRequestLogProperties() throws Exception {
156 mockStatic(MDC.class);
157 testBaseCommon.clearRequestLogProperties();
158 PowerMockito.verifyStatic(times(5));
162 public void testResetLogProperties() throws Exception {
163 testBaseCommon.setInitialLogProperties();
165 testBaseCommon.resetLogProperties(false);
166 Mockito.verify(mockCommonHeader, times(2)).getRequestId();
167 Mockito.verify(mockCommonHeader, times(2)).getOriginatorId();
168 Map<String, String> oldMdcMap = Whitebox.getInternalState(testBaseCommon, "oldMdcContent");
169 Assert.assertTrue("Should have 5 entries in persisted map", oldMdcMap.size() == 5);
171 testBaseCommon.resetLogProperties(false);
172 Mockito.verify(mockCommonHeader, times(3)).getRequestId();
173 Mockito.verify(mockCommonHeader, times(3)).getOriginatorId();
175 // test oldMdcMap is cleared
176 testBaseCommon.resetLogProperties(false);
177 Mockito.verify(mockCommonHeader, times(4)).getRequestId();
178 Mockito.verify(mockCommonHeader, times(4)).getOriginatorId();
179 oldMdcMap = Whitebox.getInternalState(testBaseCommon, "oldMdcContent");
180 Assert.assertTrue("Should have 5 entries in persisted map", oldMdcMap.size() == 5);