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