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

import org.opencomp.simulator.presetGenerator.presets.BasePresets.BasePreset;
import org.opencomp.simulator.presetGenerator.presets.aai.*;
import org.opencomp.simulator.presetGenerator.presets.ecompportal_att.PresetGetSessionSlotCheckIntervalGet;
import org.opencomp.simulator.presetGenerator.presets.ecompportal_att.PresetGetUserGet;
import org.opencomp.simulator.presetGenerator.presets.mso.PresetActivateServiceInstancePost;
import org.opencomp.simulator.presetGenerator.presets.mso.PresetDeactivateServiceInstancePost;
import org.opencomp.simulator.presetGenerator.presets.mso.PresetMSOCreateServiceInstancePost;
import org.opencomp.simulator.presetGenerator.presets.mso.PresetMSOOrchestrationRequestGet;
import org.opencomp.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceMetadataGet;
import org.opencomp.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGet;
import org.opencomp.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);
    }

}