Add Junit test for appc-provider bundle
[appc.git] / appc-provider / appc-provider-bundle / src / test / java / org / openecomp / appc / provider / AppcProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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.openecomp.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.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;
61
62 import java.util.concurrent.ExecutorService;
63 import java.util.concurrent.Executors;
64 import java.util.concurrent.Future;
65
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;
72
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({AppcProvider.class})
75 public class AppcProviderTest extends AbstractDataBrokerTest {
76
77     @Mock
78     private CommonRequestHeader commonRequestHeader;
79     @Mock
80     private ConfigPayload configPayload;
81     @Mock
82     private VnfResource vnfResource;
83     @Mock
84     private TopologyService topologyService;
85
86     private AppcProvider provider;
87     private DataBroker dataBroker;
88
89     /**
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.
94      */
95     @Override
96     protected void setupWithDataBroker(DataBroker dataBroker) {
97         super.setupWithDataBroker(dataBroker);
98
99         this.dataBroker = dataBroker;
100     }
101
102     @Before
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());
108
109         provider = spy(new AppcProvider(dataBroker, nps, registry));
110
111         PowerMockito.doReturn(topologyService).when(provider).getTopologyService();
112     }
113
114     @Test
115     public void testConstructor() throws Exception {
116         ExecutorService executorService = Whitebox.getInternalState(provider, "executor");
117         Assert.assertNotNull(executorService);
118         BindingAwareBroker.RpcRegistration internalRpcRegistration = Whitebox.getInternalState(provider,
119             "rpcRegistration");
120         Assert.assertNotNull(internalRpcRegistration);
121     }
122
123     @Test
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);
129         provider.close();
130
131         verify(executorService, times(1)).shutdown();
132         verify(rpcRegistration, times(1)).close();
133     }
134
135     @Test
136     public void testModifyConfig() throws Exception {
137         ModifyConfigInput modifyConfigInput = mock(ModifyConfigInput.class);
138         doReturn(commonRequestHeader).when(modifyConfigInput).getCommonRequestHeader();
139         doReturn(configPayload).when(modifyConfigInput).getConfigPayload();
140         // mock output
141         RpcResult<ModifyConfigOutput> modifyConfigOutput = mock(RpcResult.class);
142         PowerMockito.doReturn(modifyConfigOutput).when(topologyService).modifyConfig(any(), any());
143
144         Future<RpcResult<ModifyConfigOutput>> rpcResultFuture = provider.modifyConfig(modifyConfigInput);
145
146         Assert.assertNotNull(rpcResultFuture);
147     }
148
149     @Test
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());
155
156         Future<RpcResult<RebuildOutput>> rpcResultFuture = provider.rebuild(input);
157
158         Assert.assertNotNull(rpcResultFuture);
159     }
160
161     @Test
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());
167
168         Future<RpcResult<RestartOutput>> rpcResultFuture = provider.restart(input);
169
170         Assert.assertNotNull(rpcResultFuture);
171     }
172
173     @Test
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());
179
180         Future<RpcResult<MigrateOutput>> rpcResultFuture = provider.migrate(input);
181
182         Assert.assertNotNull(rpcResultFuture);
183     }
184
185     @Test
186     public void testEvacuate() throws Exception {
187         EvacuateInput input = mock(EvacuateInput.class);
188         doReturn(vnfResource).when(input).getVnfResource();
189
190         Future<RpcResult<EvacuateOutput>> rpcResultFuture = provider.evacuate(input);
191
192         Assert.assertNull(rpcResultFuture);
193     }
194
195     @Test
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());
201
202         Future<RpcResult<SnapshotOutput>> rpcResultFuture = provider.snapshot(input);
203
204         Assert.assertNotNull(rpcResultFuture);
205     }
206
207     @Test
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());
213
214         Future<RpcResult<VmstatuscheckOutput>> rpcResultFuture = provider.vmstatuscheck(input);
215
216         Assert.assertNotNull(rpcResultFuture);
217     }
218
219     @After
220     public void tearDown() throws Exception {
221         if (provider != null) {
222             provider.close();
223         }
224     }
225 }