aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/test/java/org/onap/appc/adapter/iaas/provider/operation/impl/MockGenerator.java
blob: e34c3da1f1592a09edcc5163177c4033ff77193c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package org.onap.appc.adapter.iaas.provider.operation.impl;

import java.util.HashMap;
import java.util.Map;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import org.onap.appc.Constants;
import org.onap.appc.adapter.iaas.ProviderAdapter;
import org.onap.appc.adapter.iaas.impl.ProviderCache;
import org.onap.appc.adapter.iaas.impl.RequestContext;
import org.onap.appc.adapter.iaas.impl.TenantCache;
import org.onap.appc.adapter.iaas.impl.VMURL;
import org.onap.appc.configuration.Configuration;
import org.onap.appc.configuration.ConfigurationFactory;
import com.att.cdp.zones.model.Hypervisor;
import com.att.cdp.zones.model.Server;
import com.att.cdp.zones.model.Server.Status;
import com.att.cdp.zones.Provider;
import com.att.cdp.zones.ImageService;
import com.att.cdp.exceptions.ZoneException;
import com.att.cdp.openstack.OpenStackContext;
import com.att.cdp.zones.ComputeService;
import org.onap.appc.pool.Pool;
import org.onap.appc.pool.PoolDrainedException;
import org.onap.appc.pool.PoolExtensionException;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;

public class MockGenerator {

    private Map<String, ProviderCache> providerCacheMap;
    private Map<String, String> params;
    private SvcLogicContext ctx;
    private Server server;
    private ImageService imageService;
    private OpenStackContext context;
    private ComputeService computeService;

    public static final String SERVER_ID = "12442";
    public static final String SERVER_NAME = "Server1";
    private final Configuration configuration = ConfigurationFactory.getConfiguration();

    /**
     * This method created a mocked up object representing the OpenStack objects which would be
     * gathered from remote systems during runtime, but which are not available during a unit test.
     * 
     * @param serverStatus Most of the classes in the package we are testing have different actions
     *        depending on the status of the server. This allows a different set of mock data to be
     *        created depending on which status is being tested.
     */
    public MockGenerator(Status serverStatus) {
        configuration.setProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT, "2");
        configuration.setProperty(Constants.PROPERTY_RETRY_LIMIT, "10");
        ctx = mock(SvcLogicContext.class);
        RequestContext requestContext = mock(RequestContext.class);
        server = mock(Server.class);
        doReturn(SERVER_NAME).when(server).getName();
        doReturn(SERVER_ID).when(server).getId();
        Status status = serverStatus;
        doReturn(status).when(server).getStatus();
        // the example base image that our fake server was built off of
        doReturn("linuxBase").when(server).getImage();
        Hypervisor hypervisor = mock(Hypervisor.class);
        com.att.cdp.zones.model.Hypervisor.Status hypervisorStatus =
                com.att.cdp.zones.model.Hypervisor.Status.ENABLED;
        doReturn(hypervisorStatus).when(hypervisor).getStatus();
        com.att.cdp.zones.model.Hypervisor.State hypervisorState =
                com.att.cdp.zones.model.Hypervisor.State.UP;
        doReturn(hypervisorState).when(hypervisor).getState();
        doReturn(hypervisor).when(server).getHypervisor();
        context = mock(OpenStackContext.class);
        Provider provider = mock(Provider.class);
        imageService = mock(ImageService.class);
        computeService = mock(ComputeService.class);
        try {
            doReturn(server).when(computeService).getServer("abc12345-1234-5678-890a-abcdefb12345");
        } catch (ZoneException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        doReturn(context).when(server).getContext();
        doReturn(provider).when(context).getProvider();
        doReturn(imageService).when(context).getImageService();
        doReturn(computeService).when(context).getComputeService();
        doReturn(false).when(requestContext).attempt();
        doReturn(true).when(requestContext).isFailed();
        params = new HashMap<String, String>();
        params.put(ProviderAdapter.PROPERTY_INSTANCE_URL,
                "http://10.1.1.2:5000/v2/abc12345-1234-5678-890a-abcdefb12345/servers/abc12345-1234-5678-890a-abcdefb12345");
        params.put(ProviderAdapter.PROPERTY_PROVIDER_NAME, "provider1");
        params.put(ProviderAdapter.PROPERTY_IDENTITY_URL,
                "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3");
        ProviderCache providerCache = mock(ProviderCache.class);
        TenantCache tenantCache = mock(TenantCache.class);
        doReturn("cloudowner_region").when(tenantCache).determineRegion(any(VMURL.class));
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantId();
        doReturn("abc12345-1234-5678-890a-abcdefb12345").when(tenantCache).getTenantName();
        Pool pool = mock(Pool.class);
        try {
            doReturn(context).when(pool).reserve();
        } catch (PoolExtensionException | PoolDrainedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        Map<String, Pool> tenantCachePools = new HashMap<String, Pool>();
        tenantCachePools.put("cloudowner_region", pool);
        doReturn(tenantCachePools).when(tenantCache).getPools();
        doReturn(tenantCache).when(providerCache).getTenant("abc12345-1234-5678-890a-abcdefb12345");
        providerCacheMap = new HashMap<String, ProviderCache>();
        providerCacheMap.put(
                "http://msb.onap.org:80/api/multicloud/v0/cloudowner_region/identity/v3",
                providerCache);
    }

    public Map<String, String> getParams() {
        return params;
    }

    public Map<String, ProviderCache> getProviderCacheMap() {
        return providerCacheMap;
    }

    public SvcLogicContext getSvcLogicContext() {
        return ctx;
    }

    public Server getServer() {
        return server;
    }

    public ImageService getImageService() {
        return imageService;
    }

    public OpenStackContext getContext() {
        return context;
    }

    public ComputeService getComputeService() {
        return computeService;
    }

}