2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.iaas.provider.operation.impl;
26 import java.util.HashMap;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.mock;
31 import org.onap.appc.Constants;
32 import org.onap.appc.adapter.iaas.ProviderAdapter;
33 import org.onap.appc.adapter.iaas.impl.ProviderCache;
34 import org.onap.appc.adapter.iaas.impl.RequestContext;
35 import org.onap.appc.adapter.iaas.impl.TenantCache;
36 import org.onap.appc.adapter.iaas.impl.VMURL;
37 import org.onap.appc.configuration.Configuration;
38 import org.onap.appc.configuration.ConfigurationFactory;
39 import com.att.cdp.zones.model.Hypervisor;
40 import com.att.cdp.zones.model.Server;
41 import com.att.cdp.zones.model.Server.Status;
42 import com.att.cdp.zones.Provider;
43 import com.att.cdp.zones.ImageService;
44 import com.att.cdp.exceptions.ZoneException;
45 import com.att.cdp.openstack.OpenStackContext;
46 import com.att.cdp.zones.ComputeService;
47 import org.onap.appc.pool.Pool;
48 import org.onap.appc.pool.PoolDrainedException;
49 import org.onap.appc.pool.PoolExtensionException;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
52 public class MockGenerator {
54 private Map<String, ProviderCache> providerCacheMap;
55 private Map<String, String> params;
56 private SvcLogicContext ctx;
57 private Server server;
58 private ImageService imageService;
59 private OpenStackContext context;
60 private ComputeService computeService;
62 public static final String SERVER_ID = "12442";
63 public static final String SERVER_NAME = "Server1";
64 private final Configuration configuration = ConfigurationFactory.getConfiguration();
67 * This method created a mocked up object representing the OpenStack objects which would be
68 * gathered from remote systems during runtime, but which are not available during a unit test.
70 * @param serverStatus Most of the classes in the package we are testing have different actions
71 * depending on the status of the server. This allows a different set of mock data to be
72 * created depending on which status is being tested.
74 public MockGenerator(Status serverStatus) {
75 configuration.setProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT, "2");
76 configuration.setProperty(Constants.PROPERTY_RETRY_LIMIT, "10");
77 ctx = mock(SvcLogicContext.class);
78 RequestContext requestContext = mock(RequestContext.class);
79 server = mock(Server.class);
80 doReturn(SERVER_NAME).when(server).getName();
81 doReturn(SERVER_ID).when(server).getId();
82 Status status = serverStatus;
83 doReturn(status).when(server).getStatus();
84 // the example base image that our fake server was built off of
85 doReturn("linuxBase").when(server).getImage();
86 Hypervisor hypervisor = mock(Hypervisor.class);
87 com.att.cdp.zones.model.Hypervisor.Status hypervisorStatus =
88 com.att.cdp.zones.model.Hypervisor.Status.ENABLED;
89 doReturn(hypervisorStatus).when(hypervisor).getStatus();
90 com.att.cdp.zones.model.Hypervisor.State hypervisorState =
91 com.att.cdp.zones.model.Hypervisor.State.UP;
92 doReturn(hypervisorState).when(hypervisor).getState();
93 doReturn(hypervisor).when(server).getHypervisor();
94 context = mock(OpenStackContext.class);
95 Provider provider = mock(Provider.class);
96 imageService = mock(ImageService.class);
97 computeService = mock(ComputeService.class);
99 doReturn(server).when(computeService).getServer("abc12345-1234-5678-890a-abcdefb12345");
100 } catch (ZoneException e2) {
101 // TODO Auto-generated catch block
102 e2.printStackTrace();
104 doReturn(context).when(server).getContext();
105 doReturn(provider).when(context).getProvider();
106 doReturn(imageService).when(context).getImageService();
107 doReturn(computeService).when(context).getComputeService();
108 doReturn(false).when(requestContext).attempt();
109 doReturn(true).when(requestContext).isFailed();
110 params = new HashMap<String, String>();
111 params.put(ProviderAdapter.PROPERTY_INSTANCE_URL,
112 "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/abc12345-1234-5678-890a-abcdefb12345");
113 params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "provider1");
114 params.put(ProviderAdapter.PROPERTY_IDENTITY_URL,
115 "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3");
116 ProviderCache providerCache = mock(ProviderCache.class);
117 TenantCache tenantCache = mock(TenantCache.class);
118 doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
119 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
120 doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
121 Pool pool = mock(Pool.class);
123 doReturn(context).when(pool).reserve();
124 } catch (PoolExtensionException | PoolDrainedException e1) {
125 // TODO Auto-generated catch block
126 e1.printStackTrace();
129 Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
130 tenantCachePools.put("cloudowner_region", pool);
131 doReturn(tenantCachePools).when(tenantCache).getPools();
132 doReturn(tenantCache).when(providerCache).getTenant("abc12345-1234-5678-890a-abcdefb12345");
133 providerCacheMap = new HashMap<String, ProviderCache>();
134 providerCacheMap.put(
135 "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3",
139 public Map<String, String> getParams() {
143 public Map<String, ProviderCache> getProviderCacheMap() {
144 return providerCacheMap;
147 public SvcLogicContext getSvcLogicContext() {
151 public Server getServer() {
155 public ImageService getImageService() {
159 public OpenStackContext getContext() {
163 public ComputeService getComputeService() {
164 return computeService;