Fix unit tests in provider-lcm-service
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / VolumeServiceTest.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.Action;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ZULU;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
39 import org.onap.appc.executor.objects.LCMCommandStatus;
40
41 import static org.mockito.Matchers.any;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.spy;
44 import static org.mockito.Mockito.times;
45
46
47 public class VolumeServiceTest {
48     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
49     private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
50     private Payload mockPayload = mock(Payload.class);
51
52     private VolumeService volumeServiceForAttachAction;
53     private VolumeService volumeServiceForDetachAction;
54
55     @Before
56     public void setUp() throws Exception {
57         volumeServiceForAttachAction = spy(new VolumeService(true));
58         volumeServiceForDetachAction = spy(new VolumeService(false));
59     }
60
61     @Test
62     public void testConstructor() throws Exception {
63         Action expectedAction = Action.AttachVolume;
64         Assert.assertEquals("Should have proper ACTION", expectedAction,
65             (Action) org.powermock.reflect.Whitebox.getInternalState(volumeServiceForAttachAction, "expectedAction"));
66         Assert.assertEquals("Should have attach-volume RPC name", "attach-volume",
67             (org.powermock.reflect.Whitebox.getInternalState(volumeServiceForAttachAction, "rpcName")).toString());
68
69         expectedAction = Action.DetachVolume;
70         Assert.assertEquals("Should have proper ACTION", expectedAction,
71             (Action) org.powermock.reflect.Whitebox.getInternalState(volumeServiceForDetachAction, "expectedAction"));
72         Assert.assertEquals("Should have detach-volume RPC name", "detach-volume",
73             (org.powermock.reflect.Whitebox.getInternalState(volumeServiceForDetachAction, "rpcName")).toString());
74     }
75
76     @Test
77     public void testValidateForAttachAction() throws Exception {
78         // test commonHeader error
79         volumeServiceForAttachAction.validate(mockCommonHeader, Action.AttachVolume, mockAI, mockPayload);
80         Status status = (Status) Whitebox.getInternalState(volumeServiceForAttachAction, "status");
81         Assert.assertEquals("should return missing parameter",
82             Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
83         Mockito.verify(volumeServiceForDetachAction, times(0)).buildStatusForParamName(any(), any());
84         Mockito.verify(volumeServiceForDetachAction, times(0)).buildStatusForErrorMsg(any(), any());
85
86         ZULU mockTimeStamp = mock(ZULU.class);
87         Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
88         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
89         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
90         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
91
92         // test Invalid action
93         volumeServiceForAttachAction.validate(mockCommonHeader, Action.DetachVolume, mockAI, mockPayload);
94         status = (Status) Whitebox.getInternalState(volumeServiceForAttachAction, "status");
95         Assert.assertEquals("Should return invalid parameter for action",
96             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
97
98         // test empty ActionIdentifier
99         volumeServiceForAttachAction.validate(mockCommonHeader, Action.AttachVolume, mockAI, mockPayload);
100         status = (Status) Whitebox.getInternalState(volumeServiceForAttachAction, "status");
101         Assert.assertEquals("should return missing parameter",
102             Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
103
104         // test empty VSERVER_ID
105         Mockito.doReturn("").when(mockAI).getVserverId();
106         volumeServiceForAttachAction.validate(mockCommonHeader, Action.AttachVolume, mockAI, mockPayload);
107         status = (Status) Whitebox.getInternalState(volumeServiceForAttachAction, "status");
108         Assert.assertEquals("should return invalid parameter",
109             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
110
111         Mockito.doReturn("vserverId").when(mockAI).getVserverId();
112
113         // test null payload
114         volumeServiceForAttachAction.validate(mockCommonHeader, Action.AttachVolume, mockAI, null);
115         Mockito.verify(volumeServiceForAttachAction, times(1)).validateExcludedActIds(any(), any());
116         status = (Status) Whitebox.getInternalState(volumeServiceForAttachAction, "status");
117         Assert.assertEquals("should return missing parameter",
118             Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
119
120         // test empty payload
121         Mockito.doReturn("").when(mockPayload).getValue();
122         volumeServiceForAttachAction.validate(mockCommonHeader, Action.AttachVolume, mockAI, mockPayload);
123         status = (Status) Whitebox.getInternalState(volumeServiceForAttachAction, "status");
124         Assert.assertEquals("should return invalid parameter",
125             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
126
127         // test space payload
128         Mockito.doReturn(" ").when(mockPayload).getValue();
129         volumeServiceForAttachAction.validate(mockCommonHeader, Action.AttachVolume, mockAI, mockPayload);
130         status = (Status) Whitebox.getInternalState(volumeServiceForAttachAction, "status");
131         Assert.assertEquals("should return invalid parameter",
132             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
133     }
134
135     @Test
136     public void testValidateForDetachAction() throws Exception {
137         // test commonHeader error
138         volumeServiceForDetachAction.validate(mockCommonHeader, Action.DetachVolume, mockAI, mockPayload);
139         Status status = (Status) Whitebox.getInternalState(volumeServiceForDetachAction, "status");
140         Assert.assertEquals("should return missing parameter",
141             Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
142         Mockito.verify(volumeServiceForDetachAction, times(0)).buildStatusForParamName(any(), any());
143         Mockito.verify(volumeServiceForDetachAction, times(0)).buildStatusForErrorMsg(any(), any());
144
145         ZULU mockTimeStamp = mock(ZULU.class);
146         Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
147         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
148         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
149         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
150
151         // test Invalid action
152         volumeServiceForDetachAction.validate(mockCommonHeader, Action.AttachVolume, mockAI, mockPayload);
153         status = (Status) Whitebox.getInternalState(volumeServiceForDetachAction, "status");
154         Assert.assertEquals("Should return invalid parameter for action",
155             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
156
157         // test empty ActionIdentifier
158         volumeServiceForDetachAction.validate(mockCommonHeader, Action.DetachVolume, mockAI, mockPayload);
159         status = (Status) Whitebox.getInternalState(volumeServiceForDetachAction, "status");
160         Assert.assertEquals("should return missing parameter",
161             Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
162
163         // test empty VSERVER_ID
164         Mockito.doReturn("").when(mockAI).getVserverId();
165         volumeServiceForDetachAction.validate(mockCommonHeader, Action.DetachVolume, mockAI, mockPayload);
166         status = (Status) Whitebox.getInternalState(volumeServiceForDetachAction, "status");
167         Assert.assertEquals("should return invalid parameter",
168             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
169
170         Mockito.doReturn("vserverId").when(mockAI).getVserverId();
171
172         // test null payload
173         volumeServiceForDetachAction.validate(mockCommonHeader, Action.DetachVolume, mockAI, null);
174         Mockito.verify(volumeServiceForDetachAction, times(1)).validateExcludedActIds(any(), any());
175         status = (Status) Whitebox.getInternalState(volumeServiceForDetachAction, "status");
176         Assert.assertEquals("should return missing parameter",
177             Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
178
179         // test empty payload
180         Mockito.doReturn("").when(mockPayload).getValue();
181         volumeServiceForDetachAction.validate(mockCommonHeader, Action.DetachVolume, mockAI, mockPayload);
182         status = (Status) Whitebox.getInternalState(volumeServiceForDetachAction, "status");
183         Assert.assertEquals("should return invalid parameter",
184             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
185
186         // test space payload
187         Mockito.doReturn(" ").when(mockPayload).getValue();
188         volumeServiceForDetachAction.validate(mockCommonHeader, Action.DetachVolume, mockAI, mockPayload);
189         status = (Status) Whitebox.getInternalState(volumeServiceForDetachAction, "status");
190         Assert.assertEquals("should return invalid parameter",
191             Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
192     }
193 }