e0c06f77451bf0cd678a190e2fc8708e879d3a8d
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Orange
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.provider.lcm.service;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.onap.appc.domainmodel.lcm.ResponseContext;
30 import org.onap.appc.executor.objects.LCMCommandStatus;
31 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.*;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
36 import org.powermock.reflect.Whitebox;
37
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
40 import static org.junit.Assert.assertNull;
41 import static org.mockito.Matchers.any;
42
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.spy;
45 import static org.mockito.Mockito.times;
46 import static org.powermock.api.mockito.PowerMockito.whenNew;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class DistributeTrafficServiceTest {
50
51     private final Action myAction = Action.DistributeTraffic;
52     private final String rpcName = "distribute-traffic";
53     private  String PAYLOAD_STRING = "{\"ConfigFileName\":\"test\"}";
54
55     private final DistributeTrafficInput mockInput = mock(DistributeTrafficInput.class);
56     private final CommonHeader mockCommonHeader = mock(CommonHeader.class);
57     private final ActionIdentifiers mockActionIdentifiers = mock(ActionIdentifiers.class);
58     private final Payload mockPayload = mock(Payload.class);
59
60     private DistributeTrafficService distributeTrafficService;
61
62     @Before
63     public void setUp() throws Exception {
64         distributeTrafficService = spy(new DistributeTrafficService());
65     }
66
67     @Test
68     public void testConstructor() throws Exception {
69         assertEquals("Should have proper ACTION", myAction,
70                 (Action) Whitebox.getInternalState(distributeTrafficService, "expectedAction"));
71         assertEquals("Should have action-status RPC name", rpcName,
72                 Whitebox.getInternalState(distributeTrafficService, "rpcName").toString());
73     }
74
75     private void helpInitializeRequestParameters() {
76         Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
77         Mockito.doReturn(mockPayload).when(mockInput).getPayload();
78         Mockito.doReturn(myAction).when(mockInput).getAction();
79         Mockito.doReturn(mockActionIdentifiers).when(mockInput).getActionIdentifiers();
80         Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
81         ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
82         Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
83         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
84         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
85         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
86         Mockito.doReturn("vnfId").when(mockActionIdentifiers).getVnfId();
87     }
88
89     @Test
90     public void testProcess() throws Exception {
91
92         helpInitializeRequestParameters();
93
94         // test processAction return without error
95         RequestExecutor mockExecutor = mock(RequestExecutor.class);
96         whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
97
98         RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
99         Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
100
101         ResponseContext mockResponseContext = mock(ResponseContext.class);
102         Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
103
104         Mockito.when(distributeTrafficService.executeAction(any())).thenReturn(mockOutput);
105
106         DistributeTrafficOutputBuilder  outputBuilder = distributeTrafficService.process(mockInput);
107
108         Mockito.verify(distributeTrafficService, times(1)).proceedAction(mockInput);
109         assertNotNull("Should have commonHeader", outputBuilder.getCommonHeader());
110     }
111
112     @Test
113     public void testValidateMissingParameters() throws Exception {
114         DistributeTrafficOutputBuilder outputBuilder = distributeTrafficService.process(mockInput);
115         Mockito.verify(distributeTrafficService, times(0)).proceedAction(any());
116         assertNull("Should not have commonHeader as we did not mock it", outputBuilder.getCommonHeader());
117         assertEquals("should return missing parameter status",
118                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
119                 outputBuilder.getStatus().getCode());
120     }
121
122     @Test
123     public void testValidateForMissingOrInvalidAction() throws Exception {
124         helpInitializeRequestParameters();
125
126         // check missing Action
127         Mockito.doReturn(null).when(mockInput).getAction();
128         distributeTrafficService.validate(mockInput);
129         Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
130         assertEquals("Should return missing parameter for action",
131                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
132
133         // check invalid Action
134         Mockito.doReturn(Action.Migrate).when(mockInput).getAction();
135
136         distributeTrafficService.validate(mockInput);
137         status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
138         assertEquals("should return missing parameter",
139                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
140     }
141
142     @Test
143     public void testValidateForMissingActionIdentifiers() throws Exception {
144
145         helpInitializeRequestParameters();
146         Mockito.doReturn(null).when(mockInput).getActionIdentifiers();
147
148         // test missing ActionIdentifiers
149         distributeTrafficService.validate(mockInput);
150         Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
151         assertEquals("should return missing parameter",
152                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
153     }
154
155     @Test
156     public void testValidateEmptyOrMissingPayload() throws Exception {
157         helpInitializeRequestParameters();
158
159         // validate empty payload
160         Mockito.doReturn("").when(mockPayload).getValue();
161         distributeTrafficService.validate(mockInput);
162         Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
163         assertEquals("should return invalid parameter",
164                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
165
166         // validate missing payload
167         Mockito.doReturn(null).when(mockInput).getPayload();
168         distributeTrafficService.validate(mockInput);
169         status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
170
171         assertEquals("should return missing parameter",
172                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
173
174     }
175
176     @Test
177     public void testValidateMissingConfigFileName() throws Exception {
178         helpInitializeRequestParameters();
179         String wrongPayload = "{\"test\":\"test\"}";
180         Mockito.doReturn(wrongPayload).when(mockPayload).getValue();
181         distributeTrafficService.validate(mockInput);
182         Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
183         assertEquals("should return status null",
184                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
185     }
186
187
188
189 }