2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.oam.processor;
26 import com.att.eelf.configuration.EELFLogger;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status;
34 import org.onap.appc.exceptions.InvalidInputException;
35 import org.onap.appc.exceptions.InvalidStateException;
36 import org.onap.appc.oam.AppcOam;
37 import org.onap.appc.oam.OAMCommandStatus;
38 import org.onap.appc.oam.util.ConfigurationHelper;
39 import org.onap.appc.oam.util.OperationHelper;
40 import org.onap.appc.oam.util.StateHelper;
41 import org.onap.appc.statemachine.impl.readers.AppcOamStates;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
45 import org.powermock.reflect.Whitebox;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.times;
53 import static org.powermock.api.mockito.PowerMockito.mockStatic;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({BaseCommon.class, MDC.class})
57 public class BaseCommonTest {
58 private class TestAbc extends BaseCommon {
63 * @param eelfLogger for logging
64 * @param configurationHelperIn for property reading
65 * @param stateHelperIn for APP-C OAM state checking
66 * @param operationHelperIn for operational helper
68 TestAbc(EELFLogger eelfLogger,
69 ConfigurationHelper configurationHelperIn,
70 StateHelper stateHelperIn,
71 OperationHelper operationHelperIn) {
72 super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
76 private TestAbc testBaseCommon;
77 private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class);
78 private StateHelper mockStateHelper = mock(StateHelper.class);
79 private CommonHeader mockCommonHeader = mock(CommonHeader.class);
82 public void setUp() throws Exception {
83 testBaseCommon = spy(new TestAbc(null, mockConfigHelper, mockStateHelper, null));
85 Whitebox.setInternalState(testBaseCommon, "commonHeader", mockCommonHeader);
86 Whitebox.setInternalState(testBaseCommon, "rpc", AppcOam.RPC.maintenance_mode);
88 // to avoid operation on logger fail, mock up the logger
89 EELFLogger mockLogger = mock(EELFLogger.class);
90 Whitebox.setInternalState(testBaseCommon, "logger", mockLogger);
94 public void testSetStatus() throws Exception {
95 OAMCommandStatus oamCommandStatus = OAMCommandStatus.ACCEPTED;
96 testBaseCommon.setStatus(oamCommandStatus);
97 Status status = testBaseCommon.status;
98 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
99 Assert.assertEquals("Should have message", oamCommandStatus.getResponseMessage(), status.getMessage());
103 public void testSetStatusWithParams() throws Exception {
104 String message = "testing";
105 OAMCommandStatus oamCommandStatus = OAMCommandStatus.REJECTED;
106 testBaseCommon.setStatus(oamCommandStatus, message);
107 Status status = testBaseCommon.status;
108 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
109 Assert.assertTrue("Should have message", status.getMessage().endsWith(message));
113 public void testSetErrorStatus() throws Exception {
114 Mockito.doReturn("testName").when(mockConfigHelper).getAppcName();
115 Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState();
116 Mockito.doReturn("testRequestId").when(mockCommonHeader).getRequestId();
117 Mockito.doReturn("testOrigId").when(mockCommonHeader).getOriginatorId();
119 String exceptionMessage = "testing";
121 OAMCommandStatus oamCommandStatus = OAMCommandStatus.INVALID_PARAMETER;
122 Throwable t = new InvalidInputException(exceptionMessage);
123 testBaseCommon.setErrorStatus(t);
124 Status status = testBaseCommon.status;
125 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
126 Mockito.verify(testBaseCommon, times(1)).resetLogProperties(false);
127 Mockito.verify(testBaseCommon, times(1)).resetLogProperties(true);
129 oamCommandStatus = OAMCommandStatus.REJECTED;
130 t = new InvalidStateException(exceptionMessage);
131 testBaseCommon.setErrorStatus(t);
132 status = testBaseCommon.status;
133 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
134 Mockito.verify(testBaseCommon, times(2)).resetLogProperties(false);
135 Mockito.verify(testBaseCommon, times(2)).resetLogProperties(true);
137 oamCommandStatus = OAMCommandStatus.UNEXPECTED_ERROR;
138 t = new NullPointerException(exceptionMessage);
139 testBaseCommon.setErrorStatus(t);
140 status = testBaseCommon.status;
141 Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
142 Mockito.verify(testBaseCommon, times(3)).resetLogProperties(false);
143 Mockito.verify(testBaseCommon, times(3)).resetLogProperties(true);
147 public void testSetInitialLogProperties() throws Exception {
148 mockStatic(MDC.class);
149 testBaseCommon.setInitialLogProperties();
150 PowerMockito.verifyStatic(times(5));
154 public void testClearRequestLogProperties() throws Exception {
155 mockStatic(MDC.class);
156 testBaseCommon.clearRequestLogProperties();
157 PowerMockito.verifyStatic(times(5));
161 public void testResetLogProperties() throws Exception {
162 testBaseCommon.setInitialLogProperties();
164 testBaseCommon.resetLogProperties(false);
165 Mockito.verify(mockCommonHeader, times(2)).getRequestId();
166 Mockito.verify(mockCommonHeader, times(2)).getOriginatorId();
167 Map<String, String> oldMdcMap = Whitebox.getInternalState(testBaseCommon, "oldMdcContent");
168 Assert.assertTrue("Should have 5 entries in persisted map", oldMdcMap.size() == 5);
170 testBaseCommon.resetLogProperties(false);
171 Mockito.verify(mockCommonHeader, times(3)).getRequestId();
172 Mockito.verify(mockCommonHeader, times(3)).getOriginatorId();
174 // test oldMdcMap is cleared
175 testBaseCommon.resetLogProperties(false);
176 Mockito.verify(mockCommonHeader, times(4)).getRequestId();
177 Mockito.verify(mockCommonHeader, times(4)).getOriginatorId();
178 oldMdcMap = Whitebox.getInternalState(testBaseCommon, "oldMdcContent");
179 Assert.assertTrue("Should have 5 entries in persisted map", oldMdcMap.size() == 5);