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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.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.mockito.runners.MockitoJUnitRunner;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
36 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
37 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
38 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.EvacuateInput;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.EvacuateOutput;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateInput;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateOutput;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigInput;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildInput;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartInput;
48 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotInput;
50 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotOutput;
51 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckInput;
52 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckOutput;
53 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.common.request.header.CommonRequestHeader;
54 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.config.payload.ConfigPayload;
55 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.vnf.resource.VnfResource;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import org.onap.appc.provider.topology.TopologyService;
58 import java.util.concurrent.ExecutorService;
59 import java.util.concurrent.Executors;
60 import java.util.concurrent.Future;
61 import java.lang.reflect.Field;
62 import org.onap.appc.provider.Whitebox;
64 import static org.mockito.Matchers.any;
65 import static org.mockito.Mockito.doReturn;
66 import static org.mockito.Mockito.mock;
67 import static org.mockito.Mockito.spy;
68 import static org.mockito.Mockito.times;
69 import static org.mockito.Mockito.verify;
71 @RunWith(MockitoJUnitRunner.class)
72 public class AppcProviderTest extends AbstractDataBrokerTest {
75 private CommonRequestHeader commonRequestHeader;
77 private ConfigPayload configPayload;
79 private VnfResource vnfResource;
81 private TopologyService topologyService;
83 private AppcProvider provider;
84 private DataBroker dataBroker;
88 * The @Before annotation is defined in the AbstractDataBrokerTest class. The method setupWithDataBroker is invoked
89 * from inside the @Before method and is used to initialize the databroker with objects for a test runs. In our case
90 * we use this oportunity to create an instance of our provider and initialize it (which registers it as a listener
91 * etc). This method runs before every @Test method below.
94 protected void setupWithDataBroker(DataBroker dataBroker) {
95 super.setupWithDataBroker(dataBroker);
97 this.dataBroker = dataBroker;
101 public void setUp() throws Exception {
102 NotificationProviderService nps = mock(NotificationProviderService.class);
103 RpcProviderRegistry registry = mock(RpcProviderRegistry.class);
104 BindingAwareBroker.RpcRegistration rpcRegistration = mock(BindingAwareBroker.RpcRegistration.class);
106 doReturn(rpcRegistration).when(registry).addRpcImplementation(any(), any());
108 provider = spy(new AppcProvider(dataBroker, nps, registry));
110 doReturn(topologyService).when(provider).getTopologyService();
114 public void testConstructor() throws Exception {
115 Object executorService = Whitebox.getInternalState(provider, "executor");
116 Assert.assertNotNull(executorService);
117 Object internalRpcRegistration = Whitebox.getInternalState(provider,"rpcRegistration");
118 Assert.assertNotNull(internalRpcRegistration);
122 public void testClose() throws Exception {
123 ExecutorService executorService = spy(Executors.newFixedThreadPool(1));
124 Whitebox.setInternalState(provider, "executor", executorService);
125 BindingAwareBroker.RpcRegistration rpcRegistration = mock(BindingAwareBroker.RpcRegistration.class);
126 Whitebox.setInternalState(provider, "rpcRegistration", rpcRegistration);
129 verify(executorService, times(1)).shutdown();
130 verify(rpcRegistration, times(1)).close();
134 public void testModifyConfig() throws Exception {
135 ModifyConfigInput modifyConfigInput = mock(ModifyConfigInput.class);
136 doReturn(commonRequestHeader).when(modifyConfigInput).getCommonRequestHeader();
137 doReturn(configPayload).when(modifyConfigInput).getConfigPayload();
139 RpcResult<ModifyConfigOutput> modifyConfigOutput = mock(RpcResult.class);
140 doReturn(modifyConfigOutput).when(topologyService).modifyConfig(any(), any());
142 Future<RpcResult<ModifyConfigOutput>> rpcResultFuture = provider.modifyConfig(modifyConfigInput);
144 Assert.assertNotNull(rpcResultFuture);
148 public void testRebuild() throws Exception {
149 RebuildInput input = mock(RebuildInput.class);
150 RpcResult<RebuildOutput> output = mock(RpcResult.class);
151 doReturn(vnfResource).when(input).getVnfResource();
152 doReturn(output).when(topologyService).rebuild(any(), any());
154 Future<RpcResult<RebuildOutput>> rpcResultFuture = provider.rebuild(input);
156 Assert.assertNotNull(rpcResultFuture);
160 public void testRestart() throws Exception {
161 RestartInput input = mock(RestartInput.class);
162 RpcResult<RestartOutput> output = mock(RpcResult.class);
163 doReturn(vnfResource).when(input).getVnfResource();
164 doReturn(output).when(topologyService).restart(any(), any());
166 Future<RpcResult<RestartOutput>> rpcResultFuture = provider.restart(input);
168 Assert.assertNotNull(rpcResultFuture);
172 public void testMigrate() throws Exception {
173 MigrateInput input = mock(MigrateInput.class);
174 RpcResult<MigrateOutput> output = mock(RpcResult.class);
175 doReturn(vnfResource).when(input).getVnfResource();
176 doReturn(output).when(topologyService).migrate(any(), any());
178 Future<RpcResult<MigrateOutput>> rpcResultFuture = provider.migrate(input);
180 Assert.assertNotNull(rpcResultFuture);
184 public void testEvacuate() throws Exception {
185 EvacuateInput input = mock(EvacuateInput.class);
186 doReturn(vnfResource).when(input).getVnfResource();
188 Future<RpcResult<EvacuateOutput>> rpcResultFuture = provider.evacuate(input);
190 Assert.assertNull(rpcResultFuture);
194 public void testSnapshot() throws Exception {
195 SnapshotInput input = mock(SnapshotInput.class);
196 RpcResult<SnapshotOutput> output = mock(RpcResult.class);
197 doReturn(vnfResource).when(input).getVnfResource();
198 doReturn(output).when(topologyService).snapshot(any(), any());
200 Future<RpcResult<SnapshotOutput>> rpcResultFuture = provider.snapshot(input);
202 Assert.assertNotNull(rpcResultFuture);
206 public void testVmstatuscheck() throws Exception {
207 VmstatuscheckInput input = mock(VmstatuscheckInput.class);
208 RpcResult<VmstatuscheckOutput> output = mock(RpcResult.class);
209 doReturn(vnfResource).when(input).getVnfResource();
210 doReturn(output).when(topologyService).vmstatuscheck(any(), any());
212 Future<RpcResult<VmstatuscheckOutput>> rpcResultFuture = provider.vmstatuscheck(input);
214 Assert.assertNotNull(rpcResultFuture);
218 public void tearDown() throws Exception {
219 if (provider != null) {