OAM operations 3 - JUnit tests
[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
26 package org.openecomp.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.mockito.Mockito;
33 import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.status.Status;
34 import org.openecomp.appc.exceptions.InvalidInputException;
35 import org.openecomp.appc.exceptions.InvalidStateException;
36 import org.openecomp.appc.oam.AppcOam;
37 import org.openecomp.appc.oam.OAMCommandStatus;
38 import org.openecomp.appc.oam.util.ConfigurationHelper;
39 import org.openecomp.appc.oam.util.OperationHelper;
40 import org.openecomp.appc.oam.util.StateHelper;
41 import org.openecomp.appc.statemachine.impl.readers.AppcOamStates;
42 import org.powermock.reflect.Whitebox;
43
44 import static org.mockito.Mockito.mock;
45
46 public class BaseCommonTest {
47     private class TestAbc extends BaseCommon {
48
49         /**
50          * Constructor
51          *
52          * @param eelfLogger            for logging
53          * @param configurationHelperIn for property reading
54          * @param stateHelperIn         for APP-C OAM state checking
55          * @param operationHelperIn     for operational helper
56          */
57         TestAbc(EELFLogger eelfLogger,
58                 ConfigurationHelper configurationHelperIn,
59                 StateHelper stateHelperIn,
60                 OperationHelper operationHelperIn) {
61             super(eelfLogger, configurationHelperIn, stateHelperIn, operationHelperIn);
62         }
63     }
64
65     private TestAbc testBaseCommon;
66     private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class);
67     private StateHelper mockStateHelper = mock(StateHelper.class);
68
69     @Before
70     public void setUp() throws Exception {
71         testBaseCommon = new TestAbc(null, mockConfigHelper, mockStateHelper, null);
72
73         // to avoid operation on logger fail, mock up the logger
74         EELFLogger mockLogger = mock(EELFLogger.class);
75         Whitebox.setInternalState(testBaseCommon, "logger", mockLogger);
76     }
77
78     @Test
79     public void testSetStatus() throws Exception {
80         OAMCommandStatus oamCommandStatus = OAMCommandStatus.ACCEPTED;
81         testBaseCommon.setStatus(oamCommandStatus);
82         Status status = testBaseCommon.status;
83         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
84         Assert.assertEquals("Should have message", oamCommandStatus.getResponseMessage(), status.getMessage());
85     }
86
87     @Test
88     public void testSetStatusWithParams() throws Exception {
89         String message = "testing";
90         OAMCommandStatus oamCommandStatus = OAMCommandStatus.REJECTED;
91         testBaseCommon.setStatus(oamCommandStatus, message);
92         Status status = testBaseCommon.status;
93         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
94         Assert.assertTrue("Should have message", status.getMessage().endsWith(message));
95     }
96
97     @Test
98     public void testSetErrorStatus() throws Exception {
99         Mockito.doReturn("testName").when(mockConfigHelper).getAppcName();
100         Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState();
101         Whitebox.setInternalState(testBaseCommon, "rpc", AppcOam.RPC.maintenance_mode);
102
103         String exceptionMessage = "testing";
104
105         OAMCommandStatus oamCommandStatus = OAMCommandStatus.INVALID_PARAMETER;
106         Throwable t = new InvalidInputException(exceptionMessage);
107         testBaseCommon.setErrorStatus(t);
108         Status status = testBaseCommon.status;
109         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
110
111         oamCommandStatus = OAMCommandStatus.REJECTED;
112         t = new InvalidStateException(exceptionMessage);
113         testBaseCommon.setErrorStatus(t);
114         status = testBaseCommon.status;
115         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
116
117         oamCommandStatus = OAMCommandStatus.UNEXPECTED_ERROR;
118         t = new NullPointerException(exceptionMessage);
119         testBaseCommon.setErrorStatus(t);
120         status = testBaseCommon.status;
121         Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue());
122     }
123
124 }