Renaming vid-automation #6
[vid.git] / vid-automation / src / test / java / org / onap / vid / more / SimulatorLoaderTest.java
1 package org.onap.vid.more;
2
3 import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
4 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIBadBodyForGetServicesGet;
5 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetNetworkZones;
6 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetPNFByRegionErrorPut;
7 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetServicesGet;
8 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
9 import org.onap.simulator.presetGenerator.presets.aai.PresetAAISearchNodeQueryEmptyResult;
10 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIServiceDesignAndCreationPut;
11 import org.onap.simulator.presetGenerator.presets.ecompportal_att.PresetGetSessionSlotCheckIntervalGet;
12 import org.onap.simulator.presetGenerator.presets.ecompportal_att.PresetGetUserGet;
13 import org.onap.simulator.presetGenerator.presets.mso.PresetActivateServiceInstancePost;
14 import org.onap.simulator.presetGenerator.presets.mso.PresetDeactivateServiceInstancePost;
15 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOCreateServiceInstancePost;
16 import org.onap.simulator.presetGenerator.presets.mso.PresetMSOOrchestrationRequestGet;
17 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceMetadataGet;
18 import org.onap.simulator.presetGenerator.presets.sdc.PresetSDCGetServiceToscaModelGet;
19 import org.onap.vid.api.BaseApiTest;
20 import org.springframework.http.HttpMethod;
21 import org.testng.annotations.DataProvider;
22 import org.testng.annotations.Test;
23 import vid.automation.test.services.SimulatorApi;
24 import vid.automation.test.services.SimulatorApi.RegistrationStrategy;
25
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.client.Invocation;
28 import javax.ws.rs.client.WebTarget;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import java.lang.reflect.Method;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Map;
35
36 import static org.testng.Assert.assertEquals;
37 import static vid.automation.test.services.SimulatorApi.registerExpectationFromPreset;
38 import static vid.automation.test.services.SimulatorApi.registerExpectationFromPresets;
39
40 public class SimulatorLoaderTest extends BaseApiTest {
41
42
43     protected Invocation.Builder createSimulatorRequestBuilder(BasePreset preset) {
44         WebTarget webTarget = client.target(SimulatorApi.getSimulationUri() + preset.getReqPath());
45         webTarget = addQueryParamsToWebTarget(preset, webTarget);
46         return webTarget.request()
47                 .accept("application/json");
48     }
49
50     private WebTarget addQueryParamsToWebTarget(BasePreset preset, WebTarget webTarget) {
51         if (preset.getQueryParams() != null) {
52             for (Map.Entry<String, List> entry : preset.getQueryParams().entrySet()) {
53                 webTarget = webTarget.queryParam(entry.getKey(), entry.getValue().toArray());
54             }
55         }
56         return webTarget;
57     }
58
59     @DataProvider
60     public static Object[][] presetClassesWithPutPost(Method test) {
61         return new Object[][]{
62                 {new PresetAAIGetPNFByRegionErrorPut()},
63                 {new PresetAAIServiceDesignAndCreationPut("a","b")},
64                 {new PresetDeactivateServiceInstancePost()},
65                 {new PresetActivateServiceInstancePost()},
66                 {new PresetMSOCreateServiceInstancePost()}
67         };
68     }
69
70     @Test(dataProvider = "presetClassesWithPutPost")
71     public<C extends BasePreset> void presetPutPost_WhenLoaded_SimulatorReturnsFakeValues(C preset) {
72         registerExpectationFromPreset(preset, RegistrationStrategy.CLEAR_THEN_SET);
73
74         Response cres = createSimulatorRequestBuilder(preset)
75                 .method(preset.getReqMethod().name(),
76                         Entity.entity(preset.getRequestBody(), MediaType.APPLICATION_JSON));
77
78         int status = cres.getStatus();
79
80         assertEquals(status, preset.getResponseCode());
81     }
82
83     @DataProvider
84     public static Object[][] presetWithGetInstances(Method test) {
85         return new Object[][]{
86                     {new PresetAAIGetSubscribersGet()},
87                     {new PresetGetSessionSlotCheckIntervalGet()},
88                     {new PresetGetUserGet()},
89                     {new PresetAAIGetServicesGet()},
90                     {new PresetSDCGetServiceMetadataGet("a" , "b", "serviceCreationTest.zip")},
91                     {new PresetSDCGetServiceToscaModelGet( "a", "serviceCreationTest.zip")},
92                     {new PresetMSOOrchestrationRequestGet()},
93                     {new PresetAAIGetNetworkZones()},
94                     {new PresetAAISearchNodeQueryEmptyResult()},
95                     {new PresetAAIBadBodyForGetServicesGet("not a json")},
96                 };
97     }
98
99     @Test(dataProvider = "presetWithGetInstances")
100     public <C extends BasePreset> void assertPresetWithGetMethod(C preset) {
101         registerExpectationFromPreset(preset, RegistrationStrategy.CLEAR_THEN_SET);
102
103         Response cres = createSimulatorRequestBuilder(preset).get();
104
105         int status = cres.getStatus();
106
107         assertEquals(status, preset.getResponseCode());
108     }
109
110     @Test(expectedExceptions = { RuntimeException.class }, expectedExceptionsMessageRegExp = ".*SimulatorLoaderTest.*")
111     public void assertPresetThatThrowException() {
112
113         registerExpectationFromPresets(Collections.singletonList(
114                 new BasePreset() {
115
116             @Override
117             public HttpMethod getReqMethod() {
118                 throw new RuntimeException();
119             }
120
121             @Override
122             public String getReqPath() {
123                 return null;
124             }
125
126             @Override
127             protected String getRootPath() {
128                 return null;
129             }
130         }), RegistrationStrategy.CLEAR_THEN_SET);
131     }
132
133 }