migrate powermock to mockito appc provider
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / onap / appc / provider / AppcProviderTest.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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.provider;
26
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;
63
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;
70
71 @RunWith(MockitoJUnitRunner.class)
72 public class AppcProviderTest extends AbstractDataBrokerTest {
73
74     @Mock
75     private CommonRequestHeader commonRequestHeader;
76     @Mock
77     private ConfigPayload configPayload;
78     @Mock
79     private VnfResource vnfResource;
80     @Mock
81     private TopologyService topologyService;
82
83     private AppcProvider provider;
84     private DataBroker dataBroker;
85
86
87     /**
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.
92      */
93     @Override
94     protected void setupWithDataBroker(DataBroker dataBroker) {
95         super.setupWithDataBroker(dataBroker);
96
97         this.dataBroker = dataBroker;
98     }
99
100     @Before
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);
105         
106         doReturn(rpcRegistration).when(registry).addRpcImplementation(any(), any());
107
108         provider = spy(new AppcProvider(dataBroker, nps, registry));
109
110         doReturn(topologyService).when(provider).getTopologyService();
111     }
112
113     @Test
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);
119     }
120
121     @Test
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);
127         provider.close();
128
129         verify(executorService, times(1)).shutdown();
130         verify(rpcRegistration, times(1)).close();
131     }
132
133     @Test
134     public void testModifyConfig() throws Exception {
135         ModifyConfigInput modifyConfigInput = mock(ModifyConfigInput.class);
136         doReturn(commonRequestHeader).when(modifyConfigInput).getCommonRequestHeader();
137         doReturn(configPayload).when(modifyConfigInput).getConfigPayload();
138         // mock output
139         RpcResult<ModifyConfigOutput> modifyConfigOutput = mock(RpcResult.class);
140         doReturn(modifyConfigOutput).when(topologyService).modifyConfig(any(), any());
141
142         Future<RpcResult<ModifyConfigOutput>> rpcResultFuture = provider.modifyConfig(modifyConfigInput);
143
144         Assert.assertNotNull(rpcResultFuture);
145     }
146
147     @Test
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());
153
154         Future<RpcResult<RebuildOutput>> rpcResultFuture = provider.rebuild(input);
155
156         Assert.assertNotNull(rpcResultFuture);
157     }
158
159     @Test
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());
165
166         Future<RpcResult<RestartOutput>> rpcResultFuture = provider.restart(input);
167
168         Assert.assertNotNull(rpcResultFuture);
169     }
170
171     @Test
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());
177
178         Future<RpcResult<MigrateOutput>> rpcResultFuture = provider.migrate(input);
179
180         Assert.assertNotNull(rpcResultFuture);
181     }
182
183     @Test
184     public void testEvacuate() throws Exception {
185         EvacuateInput input = mock(EvacuateInput.class);
186         doReturn(vnfResource).when(input).getVnfResource();
187
188         Future<RpcResult<EvacuateOutput>> rpcResultFuture = provider.evacuate(input);
189
190         Assert.assertNull(rpcResultFuture);
191     }
192
193     @Test
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());
199
200         Future<RpcResult<SnapshotOutput>> rpcResultFuture = provider.snapshot(input);
201
202         Assert.assertNotNull(rpcResultFuture);
203     }
204
205     @Test
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());
211
212         Future<RpcResult<VmstatuscheckOutput>> rpcResultFuture = provider.vmstatuscheck(input);
213
214         Assert.assertNotNull(rpcResultFuture);
215     }
216
217     @After
218     public void tearDown() throws Exception {
219         if (provider != null) {
220             provider.close();
221         }
222     }
223 }