Update license header in appc-provider files
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / ConfigScaleOutServiceTest.java
1
2 /*-
3  * ============LICENSE_START=======================================================
4  * ONAP : APPC
5  * ================================================================================
6  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.provider.lcm.service;
24
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mockito;
30 import org.mockito.internal.util.reflection.Whitebox;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.onap.appc.util.JsonUtil;
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.domainmodel.lcm.ResponseContext;
38 import org.onap.appc.executor.objects.LCMCommandStatus;
39 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
40 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
41
42 import java.util.Map;
43
44 import static org.mockito.Matchers.any;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.spy;
47 import static org.mockito.Mockito.times;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class ConfigScaleOutServiceTest {
51     private final Action myAction = Action.ConfigScaleOut;
52     private final String PAYLOAD_STRING = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
53     private ConfigScaleOutInput mockInput = mock(ConfigScaleOutInput.class);
54     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
55     private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
56     private Payload mockPayload = mock(Payload.class);
57
58     private ConfigScaleOutService configscaleoutServiceAction;
59     @Before
60     public void setUp() throws Exception {
61
62         configscaleoutServiceAction = spy(new ConfigScaleOutService());
63     }
64
65     @Test
66     public void testProcess() throws Exception {
67         // test error occurs in validation
68         ConfigScaleOutOutputBuilder outputBuilder = configscaleoutServiceAction.process(mockInput);
69         Mockito.verify(configscaleoutServiceAction, times(0)).proceedAction(any(),any(),any());
70         Assert.assertTrue("Should not have commonHeader as we did not mock it",outputBuilder.getCommonHeader() == null);
71         Assert.assertEquals("should return missing parameter status",
72                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
73                 outputBuilder.getStatus().getCode());
74
75         // make validation pass
76         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
77         Mockito.doReturn(mockPayload).when(mockInput).getPayload();
78         Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
79         // to make validation pass
80         ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
81         Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
82         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
83         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
84         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
85
86         Mockito.doReturn(myAction).when(mockInput).getAction();
87         Mockito.doReturn(mockAI).when(mockInput).getActionIdentifiers();
88         Mockito.doReturn("vnfId").when(mockAI).getVnfId();
89
90         // test processAction return without error
91         RequestExecutor mockExecutor = mock(RequestExecutor.class);
92       //  whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
93
94         RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
95         Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
96
97         ResponseContext mockResponseContext = mock(ResponseContext.class);
98         Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
99
100         org.onap.appc.domainmodel.lcm.Status mockStatus = mock(org.onap.appc.domainmodel.lcm.Status.class);
101         Integer successCode = Integer.valueOf(LCMCommandStatus.SUCCESS.getResponseCode());
102         Mockito.doReturn(successCode).when(mockStatus).getCode();
103         Mockito.doReturn(mockStatus).when(mockResponseContext).getStatus();
104         RequestHandlerInput requestHandlerInputInput = mock(RequestHandlerInput.class);
105         AbstractBaseService abstractBaseService = mock(AbstractBaseService.class);
106         Mockito.when(abstractBaseService.executeAction(requestHandlerInputInput)).thenReturn(mockOutput);
107         try {
108             outputBuilder = configscaleoutServiceAction.process(mockInput);
109         }catch(Exception e){
110             Assert.assertTrue(true);
111         }
112         Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() == null);
113         Assert.assertEquals("should return success status", new Integer(302), outputBuilder.getStatus().getCode());
114     }
115
116     @Test
117     public void testValidate() throws Exception {
118         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
119         Status status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
120         Assert.assertEquals("should return missing parameter",
121                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
122         Mockito.verify(configscaleoutServiceAction, times(0)).buildStatusForParamName(any(), any());
123         Mockito.verify(configscaleoutServiceAction, times(0)).buildStatusForErrorMsg(any(), any());
124
125         ZULU mockTimeStamp = mock(ZULU.class);
126         Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
127         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
128         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
129         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
130
131         // test empty action
132         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
133         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
134         Assert.assertEquals("Should return missing parameter for action",
135                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
136
137         // test empty ActionIdentifier
138         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
139         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
140         Assert.assertEquals("should return missing parameter",
141                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
142
143         // test Invalid VNF_ID
144         Mockito.doReturn("").when(mockAI).getVnfId();
145         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI,mockPayload);
146         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
147         Assert.assertEquals("should return invalid parameter",
148                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
149
150         // test null payload
151         Mockito.doReturn("vnfId").when(mockAI).getVnfId();
152         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, null);
153         Mockito.verify(configscaleoutServiceAction, times(1)).validateExcludedActIds(any(), any());
154         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
155         Assert.assertEquals("should return missing parameter",
156                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
157
158         // test empty payload
159
160         Mockito.doReturn("").when(mockPayload).getValue();
161         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, mockPayload);
162         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
163         Assert.assertEquals("should return invalid parameter",
164                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
165
166         // test space payload
167         Mockito.doReturn(" ").when(mockPayload).getValue();
168         configscaleoutServiceAction.validate(mockCommonHeader, Action.ConfigScaleOut, mockAI, mockPayload);
169         status = (Status) Whitebox.getInternalState(configscaleoutServiceAction, "status");
170         Assert.assertEquals("should return invalid parameter",
171                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
172     }
173 }
174