2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.onap.appc.adapter.iaas.ProviderAdapter;
33 import org.onap.appc.adapter.iaas.impl.ProviderAdapterImpl;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.powermock.reflect.Whitebox;
37 import com.att.cdp.exceptions.ZoneException;
38 import com.att.cdp.zones.model.Image;
39 import com.att.cdp.zones.model.Server;
40 import com.att.cdp.zones.model.Server.Status;
41 import static org.junit.Assert.assertEquals;
42 import static org.mockito.Mockito.verify;
44 public class TestEvacuateServer {
47 public void evacuateServerRunning() throws ZoneException {
48 MockGenerator mg = new MockGenerator(Status.RUNNING);
49 Server server = mg.getServer();
50 EvacuateServer rbs = new EvacuateServer();
51 rbs.setProviderCache(mg.getProviderCacheMap());
52 mg.getParams().put(ProviderAdapter.PROPERTY_TARGETHOST_ID, "newServer1");
54 rbs.executeProviderOperation(mg.getParams(), mg.getSvcLogicContext());
55 } catch (APPCException e) {
56 Assert.fail("Exception during EvacuateServer.executeProviderOperation");
58 verify(mg.getComputeService()).moveServer(server.getId(), "newServer1");
62 public void evacuateServerPaImpl() throws ZoneException, APPCException {
63 MockGenerator mg = new MockGenerator(Status.RUNNING);
64 EvacuateServer rbs = new EvacuateServer();
65 rbs.setProviderCache(mg.getProviderCacheMap());
66 mg.getParams().put(ProviderAdapter.PROPERTY_TARGETHOST_ID, "newServer1");
67 List<Image> images = new ArrayList<>();
68 images.add(Mockito.mock(Image.class));
69 Mockito.doReturn(images).when(mg.getServer()).getSnapshots();
70 ProviderAdapterImpl paImpl = Mockito.mock(ProviderAdapterImpl.class);
71 Whitebox.setInternalState(rbs, "paImpl", paImpl);
72 SvcLogicContext ctx = new SvcLogicContext();
73 ctx.setAttribute("error_code", "300");
74 rbs.executeProviderOperation(mg.getParams(), ctx);
75 verify(mg.getComputeService()).moveServer("12442", "newServer1");
79 public void evacuateServerAppcException() throws ZoneException, APPCException {
80 MockGenerator mg = new MockGenerator(Status.RUNNING);
81 EvacuateServer rbs = new EvacuateServer();
82 rbs.setProviderCache(mg.getProviderCacheMap());
83 mg.getParams().put(ProviderAdapter.PROPERTY_TARGETHOST_ID, "newServer1");
84 List<Image> images = new ArrayList<>();
85 images.add(Mockito.mock(Image.class));
86 Mockito.doReturn(images).when(mg.getServer()).getSnapshots();
87 ProviderAdapterImpl paImpl = Mockito.mock(ProviderAdapterImpl.class);
88 Mockito.doThrow(new APPCException()).when(paImpl).rebuildServer(Mockito.anyMap(),
89 Mockito.any(SvcLogicContext.class));
90 Whitebox.setInternalState(rbs, "paImpl", paImpl);
91 SvcLogicContext ctx = new SvcLogicContext();
92 ctx.setAttribute("error_code", "300");
93 rbs.executeProviderOperation(mg.getParams(), ctx);
94 assertEquals("Internal Server Error", ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_ERROR_MESSAGE));
98 public void evacuateServerZoneException() throws ZoneException, APPCException {
99 MockGenerator mg = new MockGenerator(Status.RUNNING);
100 EvacuateServer rbs = new EvacuateServer();
101 rbs.setProviderCache(mg.getProviderCacheMap());
102 mg.getParams().put(ProviderAdapter.PROPERTY_TARGETHOST_ID, "newServer1");
103 List<Image> images = new ArrayList<>();
104 images.add(Mockito.mock(Image.class));
105 Mockito.doReturn(images).when(mg.getServer()).getSnapshots();
106 Mockito.doThrow(new ZoneException("TEST")).when(mg.getServer()).refreshAll();
107 SvcLogicContext ctx = new SvcLogicContext();
108 ctx.setAttribute("error_code", "300");
109 rbs.executeProviderOperation(mg.getParams(), ctx);
110 assertEquals("TEST", ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_ERROR_MESSAGE));