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;
26 import org.junit.After;
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.mockito.runners.MockitoJUnitRunner;
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.onap.appc.rev160104.EvacuateInput;
39 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.EvacuateOutput;
40 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateInput;
41 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.MigrateOutput;
42 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigInput;
43 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.ModifyConfigOutput;
44 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildInput;
45 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RebuildOutput;
46 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartInput;
47 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.RestartOutput;
48 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotInput;
49 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.SnapshotOutput;
50 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckInput;
51 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.VmstatuscheckOutput;
52 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.common.request.header.CommonRequestHeader;
53 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.config.payload.ConfigPayload;
54 import org.opendaylight.yang.gen.v1.org.onap.appc.rev160104.vnf.resource.VnfResource;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.onap.appc.provider.topology.TopologyService;
57 import java.util.concurrent.ExecutorService;
58 import java.util.concurrent.Executors;
59 import java.util.concurrent.Future;
60 import java.lang.reflect.Field;
61 import org.onap.appc.provider.Whitebox;
63 import static org.mockito.Matchers.any;
64 import static org.mockito.Mockito.doReturn;
65 import static org.mockito.Mockito.mock;
66 import static org.mockito.Mockito.spy;
67 import static org.mockito.Mockito.times;
68 import static org.mockito.Mockito.verify;
70 @RunWith(MockitoJUnitRunner.class)
71 public class AppcProviderTest extends AbstractDataBrokerTest {
74 private CommonRequestHeader commonRequestHeader;
76 private ConfigPayload configPayload;
78 private VnfResource vnfResource;
80 private TopologyService topologyService;
82 private AppcProvider provider;
83 private DataBroker dataBroker;
87 * The @Before annotation is defined in the AbstractDataBrokerTest class. The method setupWithDataBroker is invoked
88 * from inside the @Before method and is used to initialize the databroker with objects for a test runs. In our case
89 * we use this oportunity to create an instance of our provider and initialize it (which registers it as a listener
90 * etc). This method runs before every @Test method below.
93 protected void setupWithDataBroker(DataBroker dataBroker) {
94 super.setupWithDataBroker(dataBroker);
96 this.dataBroker = dataBroker;
100 public void setUp() throws Exception {
101 NotificationProviderService nps = mock(NotificationProviderService.class);
102 RpcProviderRegistry registry = mock(RpcProviderRegistry.class);
103 BindingAwareBroker.RpcRegistration rpcRegistration = mock(BindingAwareBroker.RpcRegistration.class);
105 doReturn(rpcRegistration).when(registry).addRpcImplementation(any(), any());
107 provider = spy(new AppcProvider(dataBroker, nps, registry));
109 doReturn(topologyService).when(provider).getTopologyService();
113 public void testConstructor() throws Exception {
114 Object executorService = Whitebox.getInternalState(provider, "executor");
115 Assert.assertNotNull(executorService);
116 Object internalRpcRegistration = Whitebox.getInternalState(provider,"rpcRegistration");
117 Assert.assertNotNull(internalRpcRegistration);
121 public void testClose() throws Exception {
122 ExecutorService executorService = spy(Executors.newFixedThreadPool(1));
123 Whitebox.setInternalState(provider, "executor", executorService);
124 BindingAwareBroker.RpcRegistration rpcRegistration = mock(BindingAwareBroker.RpcRegistration.class);
125 Whitebox.setInternalState(provider, "rpcRegistration", rpcRegistration);
128 verify(executorService, times(1)).shutdown();
129 verify(rpcRegistration, times(1)).close();
133 public void testModifyConfig() throws Exception {
134 ModifyConfigInput modifyConfigInput = mock(ModifyConfigInput.class);
135 doReturn(commonRequestHeader).when(modifyConfigInput).getCommonRequestHeader();
136 doReturn(configPayload).when(modifyConfigInput).getConfigPayload();
138 RpcResult<ModifyConfigOutput> modifyConfigOutput = mock(RpcResult.class);
139 doReturn(modifyConfigOutput).when(topologyService).modifyConfig(any(), any());
141 Future<RpcResult<ModifyConfigOutput>> rpcResultFuture = provider.modifyConfig(modifyConfigInput);
143 Assert.assertNotNull(rpcResultFuture);
147 public void testRebuild() throws Exception {
148 RebuildInput input = mock(RebuildInput.class);
149 RpcResult<RebuildOutput> output = mock(RpcResult.class);
150 doReturn(vnfResource).when(input).getVnfResource();
151 doReturn(output).when(topologyService).rebuild(any(), any());
153 Future<RpcResult<RebuildOutput>> rpcResultFuture = provider.rebuild(input);
155 Assert.assertNotNull(rpcResultFuture);
159 public void testRestart() throws Exception {
160 RestartInput input = mock(RestartInput.class);
161 RpcResult<RestartOutput> output = mock(RpcResult.class);
162 doReturn(vnfResource).when(input).getVnfResource();
163 doReturn(output).when(topologyService).restart(any(), any());
165 Future<RpcResult<RestartOutput>> rpcResultFuture = provider.restart(input);
167 Assert.assertNotNull(rpcResultFuture);
171 public void testMigrate() throws Exception {
172 MigrateInput input = mock(MigrateInput.class);
173 RpcResult<MigrateOutput> output = mock(RpcResult.class);
174 doReturn(vnfResource).when(input).getVnfResource();
175 doReturn(output).when(topologyService).migrate(any(), any());
177 Future<RpcResult<MigrateOutput>> rpcResultFuture = provider.migrate(input);
179 Assert.assertNotNull(rpcResultFuture);
183 public void testEvacuate() throws Exception {
184 EvacuateInput input = mock(EvacuateInput.class);
185 doReturn(vnfResource).when(input).getVnfResource();
187 Future<RpcResult<EvacuateOutput>> rpcResultFuture = provider.evacuate(input);
189 Assert.assertNull(rpcResultFuture);
193 public void testSnapshot() throws Exception {
194 SnapshotInput input = mock(SnapshotInput.class);
195 RpcResult<SnapshotOutput> output = mock(RpcResult.class);
196 doReturn(vnfResource).when(input).getVnfResource();
197 doReturn(output).when(topologyService).snapshot(any(), any());
199 Future<RpcResult<SnapshotOutput>> rpcResultFuture = provider.snapshot(input);
201 Assert.assertNotNull(rpcResultFuture);
205 public void testVmstatuscheck() throws Exception {
206 VmstatuscheckInput input = mock(VmstatuscheckInput.class);
207 RpcResult<VmstatuscheckOutput> output = mock(RpcResult.class);
208 doReturn(vnfResource).when(input).getVnfResource();
209 doReturn(output).when(topologyService).vmstatuscheck(any(), any());
211 Future<RpcResult<VmstatuscheckOutput>> rpcResultFuture = provider.vmstatuscheck(input);
213 Assert.assertNotNull(rpcResultFuture);
217 public void tearDown() throws Exception {
218 if (provider != null) {