721c9ead6d5597c08dc9cde90eefea383db2052e
[vid.git] / vid-automation / src / test / java / org / onap / vid / api / InstantiationTemplatesApiTest.java
1 package org.onap.vid.api;
2
3 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.arrayWithSize;
6 import static org.hamcrest.Matchers.greaterThan;
7 import static org.onap.vid.api.TestUtils.convertRequest;
8 import static vid.automation.test.services.SimulatorApi.registerExpectationFromPreset;
9
10 import com.fasterxml.jackson.databind.JsonNode;
11 import com.fasterxml.jackson.databind.node.ObjectNode;
12 import java.io.IOException;
13 import java.util.Map.Entry;
14 import java.util.function.Predicate;
15 import java.util.stream.Collectors;
16 import java.util.stream.Stream;
17 import org.onap.sdc.ci.tests.datatypes.UserCredentials;
18 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
19 import org.onap.vid.model.mso.MsoResponseWrapper2;
20 import org.springframework.core.ParameterizedTypeReference;
21 import org.springframework.http.HttpEntity;
22 import org.springframework.http.HttpMethod;
23 import org.testng.annotations.AfterMethod;
24 import org.testng.annotations.Test;
25 import vid.automation.test.Constants.Users;
26 import vid.automation.test.model.User;
27 import vid.automation.test.services.AsyncJobsService;
28 import vid.automation.test.services.SimulatorApi.RegistrationStrategy;
29
30 public class InstantiationTemplatesApiTest extends AsyncInstantiationBase {
31
32     /*
33     Testing the Template Topology API should be very thin, given the following
34     assumptions:
35
36       - Template topology API is relying on Retry's logic.
37
38       - The templates themselves are an actual representation of the initial
39         state in VID's backend. This is all the knowledge that used to create
40         a service in the first time. So if API is fed with same state, it already
41         should be able to reiterate another instance.
42
43
44     The tests below will verify that:
45
46       - A request resulting from Cypress test on "instantiation-templates.e2e.ts"
47         is accepted by API endpoint
48
49       - A valid "regular" (not from template) request, yields a template that a
50         Cypress is able to deploy.
51
52       These two tests are, technically,  cyclic.
53
54       Currently the only test below is shortcutting the both tests, by checking
55       that feeding a Cypress input yields a Template that is the same. This is
56       not perfect, but currently what we have.
57
58      */
59
60     @Override
61     public UserCredentials getUserCredentials() {
62         User user = usersService.getUser(Users.SILVIA_ROBBINS_TYLER_SILVIA);
63         return new UserCredentials(user.credentials.userId, user.credentials.password, Users.SILVIA_ROBBINS_TYLER_SILVIA, "", "");
64     }
65
66     @AfterMethod
67     protected void dropAllFromNameCounter() {
68         AsyncJobsService asyncJobsService = new AsyncJobsService();
69         asyncJobsService.muteAllAsyncJobs();
70         asyncJobsService.dropAllFromNameCounter();
71     }
72
73     protected String templateTopologyUri(String jobId) {
74         return uri.toASCIIString() + "/asyncInstantiation/templateTopology/" + jobId;
75     }
76
77     @Test
78     public void templateTopology_givenDeployFromCypressE2E_getTemplateTopologyDataIsEquivalent() throws IOException {
79         templateTopology_givenDeploy_templateTopologyIsEquivalentToBody(
80             fileAsJsonNode("asyncInstantiation/templates__instance_template.json"));
81     }
82
83     @Test
84     public void templateTopology_givenDeployFromEditedTemplateCypressE2E_getTemplateTopologyDataIsEquivalentToOriginalTemplate() throws IOException {
85         templateTopology_givenDeploy_templateTopologyIsEquivalent(
86             fileAsJsonNode("asyncInstantiation/templates__instance_from_template__set_without_modify.json"),
87             fileAsJsonNode("asyncInstantiation/templates__instance_template.json"));
88     }
89
90     private JsonNode fileAsJsonNode(String fileName) throws IOException {
91         return objectMapper.readValue(
92             convertRequest(objectMapper, fileName),
93             JsonNode.class);
94     }
95
96     public void templateTopology_givenDeploy_templateTopologyIsEquivalentToBody(JsonNode body) {
97         templateTopology_givenDeploy_templateTopologyIsEquivalent(body, body);
98     }
99
100     public void templateTopology_givenDeploy_templateTopologyIsEquivalent(JsonNode body, JsonNode expectedTemplateTopology) {
101         registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), RegistrationStrategy.CLEAR_THEN_SET);
102
103         String uuid1 = postAsyncInstanceRequest(body);
104         JsonNode templateTopology1 = restTemplate.getForObject(templateTopologyUri(uuid1), JsonNode.class);
105
106         assertThat(cleanupTemplate(templateTopology1), jsonEquals(cleanupTemplate(expectedTemplateTopology)));
107     }
108
109     private JsonNode cleanupTemplate(JsonNode templateTopology) {
110         return Stream.of(templateTopology)
111             .map(this::removeTrackById)
112             .map(this::removeNullValues)
113             .findAny().get();
114     }
115
116     private JsonNode removeTrackById(JsonNode node) {
117         return removeAny(node, it -> it.getKey().equals("trackById"));
118     }
119
120     private JsonNode removeNullValues(JsonNode node) {
121         return removeAny(node, it -> it.getValue().isNull());
122     }
123
124     private JsonNode removeAny(JsonNode node, Predicate<Entry<String, JsonNode>> entryPredicate) {
125         if (node.isObject()) {
126             ((ObjectNode) node).remove(
127                 Streams.fromIterator(node.fields())
128                     .filter(entryPredicate)
129                     .map(Entry::getKey)
130                     .collect(Collectors.toList())
131             );
132
133             for (JsonNode child : node) {
134                 removeAny(child, entryPredicate);
135             }
136         }
137
138         return node;
139     }
140
141     private <T> String postAsyncInstanceRequest(T body) {
142         String[] jobsUuids = (String[]) restTemplate.exchange(
143             getCreateBulkUri(),
144             HttpMethod.POST,
145             new HttpEntity<>(body),
146             new ParameterizedTypeReference<MsoResponseWrapper2<String[]>>() {
147             })
148             .getBody().getEntity();
149
150         assertThat(jobsUuids, arrayWithSize(greaterThan(0)));
151         return jobsUuids[0];
152     }
153
154 }