Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / lcm / service / RebootServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.provider.lcm.service;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.RebootInput;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.RebootOutputBuilder;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.ZULU;
37 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
38 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.powermock.reflect.Whitebox;
42
43 import static org.mockito.Mockito.times;
44
45 @RunWith(PowerMockRunner.class)
46 public class RebootServiceTest {
47     private final String vserverId = "vserverId";
48
49     @Mock
50     private RebootService rebootService;
51     @Mock
52     private RebootInput rebootInput;
53     @Mock
54     private ActionIdentifiers actionIdentifiers;
55     @Mock
56     private org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader commonHeader;
57
58     private Payload payload;
59
60     @Before
61     public void setUp() throws Exception {
62         rebootService = new RebootService();
63         payload = new Payload("{\"reboot-type\":\"hard\"}");
64
65         PowerMockito.doReturn(actionIdentifiers).when(rebootInput).getActionIdentifiers();
66         PowerMockito.doReturn(payload).when(rebootInput).getPayload();
67         PowerMockito.doReturn(commonHeader).when(rebootInput).getCommonHeader();
68         PowerMockito.doReturn(new ZULU("2017-09-05T16:55:55.807Z")).when(commonHeader).getTimestamp();
69         PowerMockito.doReturn("2.00").when(commonHeader).getApiVer();
70         PowerMockito.doReturn("demo-lcm-stop-id#1").when(commonHeader).getRequestId();
71         PowerMockito.doReturn("demo-lcm-stop-id#2").when(commonHeader).getSubRequestId();
72         PowerMockito.doReturn("originator-id").when(commonHeader).getOriginatorId();
73         PowerMockito.doReturn(Action.Reboot).when(rebootInput).getAction();
74     }
75
76     @Test
77     public void testConstructor() throws Exception {
78         Action expectedAction = Action.Reboot;
79         Assert.assertEquals("Should have proper ACTION", expectedAction,
80             (Action) org.powermock.reflect.Whitebox.getInternalState(rebootService, "expectedAction"));
81         Assert.assertEquals("Should have reboot RPC name", expectedAction.name().toLowerCase(),
82             (org.powermock.reflect.Whitebox.getInternalState(rebootService, "rpcName")).toString());
83     }
84
85     @Test
86     public void testProcessAccepted() throws Exception {
87         PowerMockito.doReturn(vserverId).when(actionIdentifiers).getVserverId();
88         RebootOutputBuilder process = rebootService.process(rebootInput);
89         PowerMockito.verifyPrivate(rebootService, times(1)).invoke("validate", rebootInput);
90         Assert.assertNotNull(process.getCommonHeader());
91         Assert.assertNotNull(process.getStatus());
92     }
93
94     @Test
95     public void testProcessError() throws Exception {
96         RebootOutputBuilder process = rebootService.process(rebootInput);
97         PowerMockito.verifyPrivate(rebootService, times(1))
98             .invoke("validate", rebootInput);
99         Assert.assertNotNull(process.getStatus());
100     }
101
102     @Test
103     public void testValidateSuccess() throws Exception {
104         PowerMockito.doReturn(vserverId).when(actionIdentifiers).getVserverId();
105         Status validate = Whitebox.invokeMethod(rebootService, "validate", rebootInput);
106         Assert.assertNull(validate);
107     }
108
109     @Test
110     public void testValidateMissingVserverId() throws Exception {
111         PowerMockito.doReturn("").when(actionIdentifiers).getVserverId();
112         Whitebox.invokeMethod(rebootService, "validate", rebootInput);
113         Status status = Whitebox.getInternalState(rebootService, "status");
114         Assert.assertNotNull(status);
115     }
116
117     @Test
118     public void testValidateWrongAction() throws Exception {
119         PowerMockito.doReturn(Action.Audit).when(rebootInput).getAction();
120         PowerMockito.doReturn("").when(actionIdentifiers).getVserverId();
121         Whitebox.invokeMethod(rebootService, "validate", rebootInput);
122         Status status = Whitebox.getInternalState(rebootService, "status");
123         Assert.assertNotNull(status);
124     }
125
126     @Test
127     public void testValidateMissingActionIdentifier() throws Exception {
128         PowerMockito.doReturn(actionIdentifiers).when(rebootInput).getActionIdentifiers();
129         Whitebox.invokeMethod(rebootService, "validate", rebootInput);
130         Status status = Whitebox.getInternalState(rebootService, "status");
131         Assert.assertNotNull(status);
132     }
133
134     @Test
135     public void testValidateMissingRebootType() throws Exception {
136         Payload payload = new Payload("{}");
137         PowerMockito.doReturn(payload).when(rebootInput).getPayload();
138         PowerMockito.doReturn(vserverId).when(actionIdentifiers).getVserverId();
139         Whitebox.invokeMethod(rebootService, "validate", rebootInput);
140         Status status = Whitebox.getInternalState(rebootService, "status");
141         Assert.assertNotNull(status);
142     }
143
144     @Test
145     public void testValidateWrongRebootType() throws Exception {
146         Payload payload = new Payload("{\"reboot-type\":\"1\"}");
147         PowerMockito.doReturn(payload).when(rebootInput).getPayload();
148         PowerMockito.doReturn(vserverId).when(actionIdentifiers).getVserverId();
149         Whitebox.invokeMethod(rebootService, "validate", rebootInput);
150         Status status = Whitebox.getInternalState(rebootService, "status");
151         Assert.assertNotNull(status);
152     }
153 }