Test coverage for DetachVolumeServer
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / onap / appc / adapter / iaas / provider / operation / impl / DettachVolumeServerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 Ericsson
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.adapter.iaas.provider.operation.impl;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.mock;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.mockito.Mockito;
35 import org.onap.appc.adapter.iaas.impl.RequestContext;
36 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
37 import org.onap.appc.exceptions.APPCException;
38 import com.att.cdp.exceptions.TimeoutException;
39 import com.att.cdp.exceptions.ZoneException;
40 import com.att.cdp.zones.ComputeService;
41 import com.att.cdp.zones.VolumeService;
42 import com.att.cdp.zones.model.ModelObject;
43 import com.att.cdp.zones.model.Server;
44 import com.att.cdp.zones.model.Server.Status;
45 import com.att.cdp.zones.model.Volume;
46
47
48
49 public class DettachVolumeServerTest {
50
51     @Rule
52     public ExpectedException expectedEx = ExpectedException.none();
53
54     @Test
55     public void detachVolumeTest() throws ZoneException, APPCException {
56         MockGenerator mg = new MockGenerator(Status.RUNNING);
57         Server server = mg.getServer();
58         VolumeService volumeService = mock(VolumeService.class);
59         List<Volume> volumeList = new ArrayList<>();
60         doReturn(volumeList).when(volumeService).getVolumes();
61         doReturn(mg.getContext()).when(server).getContext();
62         doReturn(volumeService).when(mg.getContext()).getVolumeService();
63         DettachVolumeServer rbs = new DettachVolumeServer();
64         rbs.setProviderCache(mg.getProviderCacheMap());
65         assertTrue(rbs.executeProviderOperation(mg.getParams(), mg.getSvcLogicContext()) instanceof ModelObject);
66     }
67
68     @Test
69     public void validateDetachTest() throws RequestFailedException, ZoneException {
70         DettachVolumeServer rbs = Mockito.spy(new DettachVolumeServer());
71         RequestContext rc = Mockito.mock(RequestContext.class);
72         ComputeService ser = Mockito.mock(ComputeService.class);
73         Mockito.doReturn(true).doReturn(false).when(rc).attempt();
74         Map<String, String> attachments = new HashMap<>();
75         attachments.put("VOLUME_ID", "VOLUME_ID");
76         Mockito.doReturn(attachments).when(ser).getAttachments(Mockito.anyString());
77         assertTrue(rbs.validateDetach(rc, ser, "VM", "VOLUME_ID"));
78     }
79
80     @Test
81     public void validateDetachTestTimeoutException() throws RequestFailedException, ZoneException {
82         DettachVolumeServer rbs = Mockito.spy(new DettachVolumeServer());
83         RequestContext rc = Mockito.mock(RequestContext.class);
84         ComputeService ser = Mockito.mock(ComputeService.class);
85         Mockito.doReturn(true).doReturn(false).when(rc).attempt();
86         Mockito.doReturn(30).when(rc).getAttempts();
87         expectedEx.expect(TimeoutException.class);
88         rbs.validateDetach(rc, ser, "VM", "VOLUME_ID");
89     }
90
91     @Test
92     public void validateDetachTest3Arg() throws RequestFailedException, ZoneException {
93         DettachVolumeServer rbs = Mockito.spy(new DettachVolumeServer());
94         ComputeService ser = Mockito.mock(ComputeService.class);
95         Map<String, String> attachments = new HashMap<>();
96         attachments.put("VOLUME_ID", "VOLUME_ID");
97         Mockito.doReturn(attachments).when(ser).getAttachments(Mockito.anyString());
98         assertTrue(rbs.validateDetach(ser, "VM", "VOLUME_ID"));
99     }
100 }