2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.service;
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;
42 import static org.mockito.Mockito.times;
44 @RunWith(PowerMockRunner.class)
45 public class RebootServiceTest {
46 private final String vserverId = "vserverId";
49 private RebootService rebootService;
51 private RebootInput rebootInput;
53 private ActionIdentifiers actionIdentifiers;
55 private org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader commonHeader;
57 private Payload payload;
60 public void setUp() throws Exception {
61 rebootService = new RebootService();
62 payload = new Payload("{\"reboot-type\":\"hard\"}");
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();
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());
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());
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());
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);
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);
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);
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);
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);
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);