aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/test/java/org/onap/vid/more/SimulatorLoaderTest.java
blob: bc550ae1997cf291cd16e7181d752f47f64c48de (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
package org.onap.vid.more;

import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAIBadBodyForGetServicesGet;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetNetworkZones;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetPNFByRegionErrorPut;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetServicesGet;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAISearchNodeQueryEmptyResult;
import org.onap.simulator.presetGenerator.presets.aai.PresetAAIServiceDesignAndCreationPut;
import org.onap.simulator.presetGenerator.presets.ecompportal_att.PresetGetSessionSlotCheckIntervalGet;
import org.onap.simulator.presetGenerator.presets.ecompportal_att.PresetGetUserGet;
import org.onap.simulator.presetGenerator.presets.mso.PresetActivateServiceInstancePost;
import org.onap.simulator.presetGenerator.presets.mso.PresetDeactivateServiceInstancePost;
import org.onap.simulator.presetGenerator.presets.mso.PresetMSOCreateServiceInstancePost;
import org.onap.simulator.presetGenerator.presets.mso.PresetMSOOrchestrationRequestGet;
import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceMetadataGet;
import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGet;
import org.onap.vid.api.BaseApiTest;
import org.springframework.http.HttpMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import vid.automation.test.services.SimulatorApi;
import vid.automation.test.services.SimulatorApi.RegistrationStrategy;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.testng.Assert.assertEquals;
import static vid.automation.test.services.SimulatorApi.registerExpectationFromPreset;
import static vid.automation.test.services.SimulatorApi.registerExpectationFromPresets;

public class SimulatorLoaderTest extends BaseApiTest {


    protected Invocation.Builder createSimulatorRequestBuilder(BasePreset preset) {
        WebTarget webTarget = client.target(SimulatorApi.getSimulationUri() + preset.getReqPath());
        webTarget = addQueryParamsToWebTarget(preset, webTarget);
        return webTarget.request()
                .accept("application/json");
    }

    private WebTarget addQueryParamsToWebTarget(BasePreset preset, WebTarget webTarget) {
        if (preset.getQueryParams() != null) {
            for (Map.Entry<String, List> entry : preset.getQueryParams().entrySet()) {
                webTarget = webTarget.queryParam(entry.getKey(), entry.getValue().toArray());
            }
        }
        return webTarget;
    }

    @DataProvider
    public static Object[][] presetClassesWithPutPost(Method test) {
        return new Object[][]{
                {new PresetAAIGetPNFByRegionErrorPut()},
                {new PresetAAIServiceDesignAndCreationPut("a","b")},
                {new PresetDeactivateServiceInstancePost()},
                {new PresetActivateServiceInstancePost()},
                {new PresetMSOCreateServiceInstancePost()}
        };
    }

    @Test(dataProvider = "presetClassesWithPutPost")
    public<C extends BasePreset> void presetPutPost_WhenLoaded_SimulatorReturnsFakeValues(C preset) {
        registerExpectationFromPreset(preset, RegistrationStrategy.CLEAR_THEN_SET);

        Response cres = createSimulatorRequestBuilder(preset)
                .method(preset.getReqMethod().name(),
                        Entity.entity(preset.getRequestBody(), MediaType.APPLICATION_JSON));

        int status = cres.getStatus();

        assertEquals(status, preset.getResponseCode());
    }

    @DataProvider
    public static Object[][] presetWithGetInstances(Method test) {
        return new Object[][]{
                    {new PresetAAIGetSubscribersGet()},
                    {new PresetGetSessionSlotCheckIntervalGet()},
                    {new PresetGetUserGet()},
                    {new PresetAAIGetServicesGet()},
                    {new PresetSDCGetServiceMetadataGet("a" , "b", "serviceCreationTest.zip")},
                    {new PresetSDCGetServiceToscaModelGet( "a", "serviceCreationTest.zip")},
                    {new PresetMSOOrchestrationRequestGet()},
                    {new PresetAAIGetNetworkZones()},
                    {new PresetAAISearchNodeQueryEmptyResult()},
                    {new PresetAAIBadBodyForGetServicesGet("not a json")},
                };
    }

    @Test(dataProvider = "presetWithGetInstances")
    public <C extends BasePreset> void assertPresetWithGetMethod(C preset) {
        registerExpectationFromPreset(preset, RegistrationStrategy.CLEAR_THEN_SET);

        Response cres = createSimulatorRequestBuilder(preset).get();

        int status = cres.getStatus();

        assertEquals(status, preset.getResponseCode());
    }

    @Test(expectedExceptions = { RuntimeException.class }, expectedExceptionsMessageRegExp = ".*SimulatorLoaderTest.*")
    public void assertPresetThatThrowException() {

        registerExpectationFromPresets(Collections.singletonList(
                new BasePreset() {

            @Override
            public HttpMethod getReqMethod() {
                throw new RuntimeException();
            }

            @Override
            public String getReqPath() {
                return null;
            }

            @Override
            protected String getRootPath() {
                return null;
            }
        }), RegistrationStrategy.CLEAR_THEN_SET);
    }

}