serializing OAM async task
[appc.git] / appc-oam / appc-oam-bundle / src / test / java / org / openecomp / appc / oam / processor / BaseCommonTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.oam.processor;
26
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;
47 import org.slf4j.MDC;
48
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
120         String exceptionMessage = "testing";
121
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);
129
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);
137
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);
145     }
146
147     @Test
148     public void testSetInitialLogProperties() throws Exception {
149         mockStatic(MDC.class);
150         testBaseCommon.setInitialLogProperties();
151         PowerMockito.verifyStatic(times(5));
152     }
153
154     @Test
155     public void testClearRequestLogProperties() throws Exception {
156         mockStatic(MDC.class);
157         testBaseCommon.clearRequestLogProperties();
158         PowerMockito.verifyStatic(times(5));
159     }
160
161     @Test
162     public void testResetLogProperties() throws Exception {
163         testBaseCommon.setInitialLogProperties();
164
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);
170
171         testBaseCommon.resetLogProperties(false);
172         Mockito.verify(mockCommonHeader, times(3)).getRequestId();
173         Mockito.verify(mockCommonHeader, times(3)).getOriginatorId();
174
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);
181     }
182 }