Update license header in appc-provider files
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / QuiesceTrafficServiceTest.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  * 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider.lcm.service;
25
26 import org.junit.Assert;
27 import junit.framework.TestCase;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
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.domainmodel.lcm.ResponseContext;
38 import org.onap.appc.executor.objects.LCMCommandStatus;
39 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42
43 import static org.mockito.Matchers.any;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.spy;
46 import static org.mockito.Mockito.times;
47 import static org.powermock.api.mockito.PowerMockito.whenNew;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({QuiesceTrafficService.class, RequestExecutor.class})
51 public class QuiesceTrafficServiceTest {
52     private final Action myAction = Action.QuiesceTraffic;
53     private final String PAYLOAD_STRING = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
54     private QuiesceTrafficInput mockInput = mock(QuiesceTrafficInput.class);
55     private CommonHeader mockCommonHeader = mock(CommonHeader.class);
56     private ActionIdentifiers mockAI = mock(ActionIdentifiers.class);
57     private Payload mockPayload = mock(Payload.class);
58
59     private QuiesceTrafficService quiesceServiceAction;
60     @Before
61     public void setUp() throws Exception {
62         quiesceServiceAction = spy(new QuiesceTrafficService());
63     }
64
65
66     @Test
67     public void testConstructor() throws Exception {
68         Action expectedAction = Action.QuiesceTraffic;
69         Assert.assertEquals("Should have proper ACTION", expectedAction,
70                 (Action) org.powermock.reflect.Whitebox.getInternalState(quiesceServiceAction, "expectedAction"));
71         Assert.assertEquals("Should have quiesce-traffic RPC name", "quiesce-traffic",
72                 (org.powermock.reflect.Whitebox.getInternalState(quiesceServiceAction, "rpcName")).toString());
73     }
74
75 //    @Test
76 //    public void testProcess() throws Exception {
77 //        // test error occurs in validation
78 //        QuiesceTrafficOutputBuilder outputBuilder = quiesceServiceAction.process(mockInput);
79 //        Mockito.verify(quiesceServiceAction, times(0)).proceedAction(any(),any(),any());
80 //        Assert.assertTrue("Should not have commonHeader as we did not mock it",outputBuilder.getCommonHeader() == null);
81 //        Assert.assertEquals("should return missing parameter status",
82 //                Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
83 //                outputBuilder.getStatus().getCode());
84 //
85 //        // make validation pass
86 //        Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
87 //        Mockito.doReturn(mockPayload).when(mockInput).getPayload();
88 //        Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
89 //        // to make validation pass
90 //        ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
91 //        Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
92 //        Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
93 //        Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
94 //        Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
95 //
96 //        Mockito.doReturn(myAction).when(mockInput).getAction();
97 //        Mockito.doReturn(mockAI).when(mockInput).getActionIdentifiers();
98 //        Mockito.doReturn("vnfId").when(mockAI).getVnfId();
99 //
100 //        // test processAction return with error
101 //        outputBuilder = quiesceServiceAction.process(mockInput);
102 //        Mockito.verify(quiesceServiceAction, times(1)).proceedAction(any(),any(),any());
103 //        Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() != null);
104 //        Assert.assertEquals("should return rejected status",
105 //                Integer.valueOf(LCMCommandStatus.REJECTED.getResponseCode()),
106 //                outputBuilder.getStatus().getCode());
107 //
108 //        // test processAction return without error
109 //        RequestExecutor mockExecutor = mock(RequestExecutor.class);
110 //        whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
111 //
112 //        RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
113 //        Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
114 //
115 //        ResponseContext mockResponseContext = mock(ResponseContext.class);
116 //        Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
117 //
118 //        org.onap.appc.domainmodel.lcm.Status mockStatus = mock(org.onap.appc.domainmodel.lcm.Status.class);
119 //        Integer successCode = Integer.valueOf(LCMCommandStatus.SUCCESS.getResponseCode());
120 //        Mockito.doReturn(successCode).when(mockStatus).getCode();
121 //        Mockito.doReturn(mockStatus).when(mockResponseContext).getStatus();
122 //
123 //        outputBuilder = quiesceServiceAction.process(mockInput);
124 //        Assert.assertTrue("Should have commonHeader",outputBuilder.getCommonHeader() != null);
125 //        Assert.assertEquals("should return success status", successCode, outputBuilder.getStatus().getCode());
126 //    }
127
128     @Test
129     public void testValidate() throws Exception {
130         quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
131         Status status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
132         Assert.assertEquals("should return missing parameter",
133                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
134         Mockito.verify(quiesceServiceAction, times(0)).buildStatusForParamName(any(), any());
135         Mockito.verify(quiesceServiceAction, times(0)).buildStatusForErrorMsg(any(), any());
136
137         ZULU mockTimeStamp = mock(ZULU.class);
138         Mockito.doReturn(mockTimeStamp).when(mockCommonHeader).getTimestamp();
139         Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
140         Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
141         Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
142
143         // test empty action
144         quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
145         status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
146         Assert.assertEquals("Should return missing parameter for action",
147                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
148
149         // test empty ActionIdentifier
150         quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
151         status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
152         Assert.assertEquals("should return missing parameter",
153                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
154
155         // test Invalid VNF_ID
156         Mockito.doReturn("").when(mockAI).getVnfId();
157         quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI,mockPayload);
158         status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
159         Assert.assertEquals("should return invalid parameter",
160                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
161
162         // test null payload
163         Mockito.doReturn("vnfId").when(mockAI).getVnfId();
164         quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI, null);
165         Mockito.verify(quiesceServiceAction, times(1)).validateExcludedActIds(any(), any());
166         status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
167         Assert.assertEquals("should return missing parameter",
168                 Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
169
170         // test empty payload
171
172         Mockito.doReturn("").when(mockPayload).getValue();
173         quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI, mockPayload);
174         status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
175         Assert.assertEquals("should return invalid parameter",
176                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
177
178         // test space payload
179         Mockito.doReturn(" ").when(mockPayload).getValue();
180         quiesceServiceAction.validate(mockCommonHeader, Action.QuiesceTraffic, mockAI, mockPayload);
181         status = (Status) Whitebox.getInternalState(quiesceServiceAction, "status");
182         Assert.assertEquals("should return invalid parameter",
183                 Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
184     }
185 }