2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.provider;
27 import org.junit.After;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
35 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
36 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
37 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
38 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateInput;
39 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.EvacuateOutput;
40 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.MigrateInput;
41 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.MigrateOutput;
42 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.ModifyConfigInput;
43 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.ModifyConfigOutput;
44 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RebuildInput;
45 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RebuildOutput;
46 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RestartInput;
47 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.RestartOutput;
48 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.SnapshotInput;
49 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.SnapshotOutput;
50 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.VmstatuscheckInput;
51 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.VmstatuscheckOutput;
52 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.common.request.header.CommonRequestHeader;
53 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.config.payload.ConfigPayload;
54 import org.opendaylight.yang.gen.v1.org.openecomp.appc.rev160104.vnf.resource.VnfResource;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.openecomp.appc.provider.topology.TopologyService;
57 import org.powermock.api.mockito.PowerMockito;
58 import org.powermock.core.classloader.annotations.PrepareForTest;
59 import org.powermock.modules.junit4.PowerMockRunner;
60 import org.powermock.reflect.Whitebox;
62 import java.util.concurrent.ExecutorService;
63 import java.util.concurrent.Executors;
64 import java.util.concurrent.Future;
66 import static org.mockito.Matchers.any;
67 import static org.mockito.Mockito.doReturn;
68 import static org.mockito.Mockito.mock;
69 import static org.mockito.Mockito.spy;
70 import static org.mockito.Mockito.times;
71 import static org.mockito.Mockito.verify;
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({AppcProvider.class})
75 public class AppcProviderTest extends AbstractDataBrokerTest {
78 private CommonRequestHeader commonRequestHeader;
80 private ConfigPayload configPayload;
82 private VnfResource vnfResource;
84 private TopologyService topologyService;
86 private AppcProvider provider;
87 private DataBroker dataBroker;
90 * The @Before annotation is defined in the AbstractDataBrokerTest class. The method setupWithDataBroker is invoked
91 * from inside the @Before method and is used to initialize the databroker with objects for a test runs. In our case
92 * we use this oportunity to create an instance of our provider and initialize it (which registers it as a listener
93 * etc). This method runs before every @Test method below.
96 protected void setupWithDataBroker(DataBroker dataBroker) {
97 super.setupWithDataBroker(dataBroker);
99 this.dataBroker = dataBroker;
103 public void setUp() throws Exception {
104 NotificationProviderService nps = mock(NotificationProviderService.class);
105 RpcProviderRegistry registry = mock(RpcProviderRegistry.class);
106 BindingAwareBroker.RpcRegistration rpcRegistration = mock(BindingAwareBroker.RpcRegistration.class);
107 PowerMockito.doReturn(rpcRegistration).when(registry).addRpcImplementation(any(), any());
109 provider = spy(new AppcProvider(dataBroker, nps, registry));
111 PowerMockito.doReturn(topologyService).when(provider).getTopologyService();
115 public void testConstructor() throws Exception {
116 ExecutorService executorService = Whitebox.getInternalState(provider, "executor");
117 Assert.assertNotNull(executorService);
118 BindingAwareBroker.RpcRegistration internalRpcRegistration = Whitebox.getInternalState(provider,
120 Assert.assertNotNull(internalRpcRegistration);
124 public void testClose() throws Exception {
125 ExecutorService executorService = spy(Executors.newFixedThreadPool(1));
126 Whitebox.setInternalState(provider, "executor", executorService);
127 BindingAwareBroker.RpcRegistration rpcRegistration = mock(BindingAwareBroker.RpcRegistration.class);
128 Whitebox.setInternalState(provider, "rpcRegistration", rpcRegistration);
131 verify(executorService, times(1)).shutdown();
132 verify(rpcRegistration, times(1)).close();
136 public void testModifyConfig() throws Exception {
137 ModifyConfigInput modifyConfigInput = mock(ModifyConfigInput.class);
138 doReturn(commonRequestHeader).when(modifyConfigInput).getCommonRequestHeader();
139 doReturn(configPayload).when(modifyConfigInput).getConfigPayload();
141 RpcResult<ModifyConfigOutput> modifyConfigOutput = mock(RpcResult.class);
142 PowerMockito.doReturn(modifyConfigOutput).when(topologyService).modifyConfig(any(), any());
144 Future<RpcResult<ModifyConfigOutput>> rpcResultFuture = provider.modifyConfig(modifyConfigInput);
146 Assert.assertNotNull(rpcResultFuture);
150 public void testRebuild() throws Exception {
151 RebuildInput input = mock(RebuildInput.class);
152 RpcResult<RebuildOutput> output = mock(RpcResult.class);
153 doReturn(vnfResource).when(input).getVnfResource();
154 doReturn(output).when(topologyService).rebuild(any(), any());
156 Future<RpcResult<RebuildOutput>> rpcResultFuture = provider.rebuild(input);
158 Assert.assertNotNull(rpcResultFuture);
162 public void testRestart() throws Exception {
163 RestartInput input = mock(RestartInput.class);
164 RpcResult<RestartOutput> output = mock(RpcResult.class);
165 doReturn(vnfResource).when(input).getVnfResource();
166 doReturn(output).when(topologyService).restart(any(), any());
168 Future<RpcResult<RestartOutput>> rpcResultFuture = provider.restart(input);
170 Assert.assertNotNull(rpcResultFuture);
174 public void testMigrate() throws Exception {
175 MigrateInput input = mock(MigrateInput.class);
176 RpcResult<MigrateOutput> output = mock(RpcResult.class);
177 doReturn(vnfResource).when(input).getVnfResource();
178 doReturn(output).when(topologyService).migrate(any(), any());
180 Future<RpcResult<MigrateOutput>> rpcResultFuture = provider.migrate(input);
182 Assert.assertNotNull(rpcResultFuture);
186 public void testEvacuate() throws Exception {
187 EvacuateInput input = mock(EvacuateInput.class);
188 doReturn(vnfResource).when(input).getVnfResource();
190 Future<RpcResult<EvacuateOutput>> rpcResultFuture = provider.evacuate(input);
192 Assert.assertNull(rpcResultFuture);
196 public void testSnapshot() throws Exception {
197 SnapshotInput input = mock(SnapshotInput.class);
198 RpcResult<SnapshotOutput> output = mock(RpcResult.class);
199 doReturn(vnfResource).when(input).getVnfResource();
200 doReturn(output).when(topologyService).snapshot(any(), any());
202 Future<RpcResult<SnapshotOutput>> rpcResultFuture = provider.snapshot(input);
204 Assert.assertNotNull(rpcResultFuture);
208 public void testVmstatuscheck() throws Exception {
209 VmstatuscheckInput input = mock(VmstatuscheckInput.class);
210 RpcResult<VmstatuscheckOutput> output = mock(RpcResult.class);
211 doReturn(vnfResource).when(input).getVnfResource();
212 doReturn(output).when(topologyService).vmstatuscheck(any(), any());
214 Future<RpcResult<VmstatuscheckOutput>> rpcResultFuture = provider.vmstatuscheck(input);
216 Assert.assertNotNull(rpcResultFuture);
220 public void tearDown() throws Exception {
221 if (provider != null) {