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