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