Fix unit tests in provider-lcm-service
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / ResumeTrafficServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications (C) 2019 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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.provider.lcm.service;
27
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.mockito.internal.util.reflection.Whitebox;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.*;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
37 import org.onap.appc.executor.objects.LCMCommandStatus;
38
39 import static org.mockito.Matchers.any;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.spy;
42 import static org.mockito.Mockito.times;
43
44
45 public class ResumeTrafficServiceTest {
46     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
47     private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
48     private Payload mockPayload = mock(Payload.class);
49
50     private ResumeTrafficService resumeServiceAction;
51     @Before
52     public void setUp() throws Exception {
53         resumeServiceAction = spy(new ResumeTrafficService());
54     }
55
56     @Test
57     public void testConstructor() throws Exception {
58         Action expectedAction = Action.ResumeTraffic;
59         Assert.assertEquals("Should have proper ACTION", expectedAction,
60                 (Action) org.powermock.reflect.Whitebox.getInternalState(resumeServiceAction, "expectedAction"));
61         Assert.assertEquals("Should have resume-traffic RPC name", "resume-traffic",
62                 (org.powermock.reflect.Whitebox.getInternalState(resumeServiceAction, "rpcName")).toString());
63     }
64
65     @Test
66     public void testValidate() throws Exception {
67         resumeServiceAction.validate(mockCommonHeader, Action.ResumeTraffic, mockAI,mockPayload);
68         Status status = (Status) Whitebox.getInternalState(resumeServiceAction, "status");
69         Assert.assertEquals("should return missing parameter",
70             Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
71         Mockito.verify(resumeServiceAction, times(0)).buildStatusForParamName(any(), any());
72         Mockito.verify(resumeServiceAction, times(0)).buildStatusForErrorMsg(any(), any());
73
74         ZULU mockTimeStamp = mock(ZULU.class);
75         Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
76         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
77         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
78         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
79
80         // test empty action
81         resumeServiceAction.validate(mockCommonHeader, Action.ResumeTraffic, mockAI,mockPayload);
82         status = (Status) Whitebox.getInternalState(resumeServiceAction, "status");
83         Assert.assertEquals("Should return missing parameter for action",
84                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
85
86         // test empty ActionIdentifier
87         resumeServiceAction.validate(mockCommonHeader, Action.ResumeTraffic, mockAI,mockPayload);
88         status = (Status) Whitebox.getInternalState(resumeServiceAction, "status");
89         Assert.assertEquals("should return missing parameter",
90                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
91
92         // test Invalid VNF_ID
93         Mockito.doReturn("").when(mockAI).getVnfId();
94         resumeServiceAction.validate(mockCommonHeader, Action.ResumeTraffic, mockAI,mockPayload);
95         status = (Status) Whitebox.getInternalState(resumeServiceAction, "status");
96         Assert.assertEquals("should return invalid parameter",
97                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
98         // test null payload
99         Mockito.doReturn("vnfId").when(mockAI).getVnfId();
100         resumeServiceAction.validate(mockCommonHeader, Action.ResumeTraffic, mockAI, null);
101         Mockito.verify(resumeServiceAction, times(1)).validateExcludedActIds(any(), any());
102         status = (Status) Whitebox.getInternalState(resumeServiceAction, "status");
103         Assert.assertEquals("should return missing parameter",
104                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
105
106         // test empty payload
107         Mockito.doReturn("").when(mockPayload).getValue();
108         resumeServiceAction.validate(mockCommonHeader, Action.ResumeTraffic, mockAI, mockPayload);
109         status = (Status) Whitebox.getInternalState(resumeServiceAction, "status");
110         Assert.assertEquals("should return invalid parameter",
111                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
112
113         // test space payload
114         Mockito.doReturn(null).when(resumeServiceAction).validateMustHaveParamValue(Mockito.anyString(),
115                 Mockito.anyString());
116         Mockito.doReturn(" ").when(mockPayload).getValue();
117         resumeServiceAction.validate(mockCommonHeader, Action.ResumeTraffic, mockAI, mockPayload);
118         status = (Status) Whitebox.getInternalState(resumeServiceAction, "status");
119         Assert.assertEquals("should return invalid parameter",
120                 Integer.valueOf(LCMCommandStatus.UNEXPECTED_ERROR.getResponseCode()), status.getCode());
121     }
122 }