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