Fix templates_instance_template.json file and InstantiationTemplatesApiTest
[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;
26 import vid.automation.test.Constants.Users;
27 import vid.automation.test.model.User;
28 import vid.automation.test.services.AsyncJobsService;
29 import vid.automation.test.services.SimulatorApi.RegistrationStrategy;
30
31 public class InstantiationTemplatesApiTest extends AsyncInstantiationBase {
32
33     /*
34     Testing the Template Topology API should be very thin, given the following
35     assumptions:
36
37       - Template topology API is relying on Retry's logic.
38
39       - The templates themselves are an actual representation of the initial
40         state in VID's backend. This is all the knowledge that used to create
41         a service in the first time. So if API is fed with same state, it already
42         should be able to reiterate another instance.
43
44
45     The tests below will verify that:
46
47       - A request resulting from Cypress test on "instantiation-templates.e2e.ts"
48         is accepted by API endpoint
49
50       - A valid "regular" (not from template) request, yields a template that a
51         Cypress is able to deploy.
52
53       These two tests are, technically,  cyclic.
54
55       Currently the only test below is shortcutting the both tests, by checking
56       that feeding a Cypress input yields a Template that is the same. This is
57       not perfect, but currently what we have.
58
59      */
60
61     @Override
62     public UserCredentials getUserCredentials() {
63         User user = usersService.getUser(Users.SILVIA_ROBBINS_TYLER_SILVIA);
64         return new UserCredentials(user.credentials.userId, user.credentials.password, Users.SILVIA_ROBBINS_TYLER_SILVIA, "", "");
65     }
66
67     @AfterMethod
68     protected void dropAllFromNameCounter() {
69         AsyncJobsService asyncJobsService = new AsyncJobsService();
70         asyncJobsService.muteAllAsyncJobs();
71         asyncJobsService.dropAllFromNameCounter();
72     }
73
74     protected String templateTopologyUri(String jobId) {
75         return uri.toASCIIString() + "/asyncInstantiation/templateTopology/" + jobId;
76     }
77
78     @Test
79     public void templateTopology_givenDeployFromCypressE2E_getTemplateTopologyDataIsEquivalent() throws IOException {
80         templateTopology_givenDeploy_templateTopologyIsEquivalent(objectMapper.readValue(
81             convertRequest(objectMapper, "asyncInstantiation/templates__instance_template.json"),
82             JsonNode.class));
83     }
84
85     public void templateTopology_givenDeploy_templateTopologyIsEquivalent(JsonNode body) {
86         registerExpectationFromPreset(new PresetAAIGetSubscribersGet(), RegistrationStrategy.CLEAR_THEN_SET);
87
88         String uuid1 = postAsyncInstanceRequest(body);
89         JsonNode templateTopology1 = restTemplate.getForObject(templateTopologyUri(uuid1), JsonNode.class);
90
91         assertThat(cleanupTemplate(templateTopology1), jsonEquals(cleanupTemplate(body)));
92     }
93
94     private JsonNode cleanupTemplate(JsonNode templateTopology) {
95         return Stream.of(templateTopology)
96             .map(this::removeTrackById)
97             .map(this::removeNullValues)
98             .findAny().get();
99     }
100
101     private JsonNode removeTrackById(JsonNode node) {
102         return removeAny(node, it -> it.getKey().equals("trackById"));
103     }
104
105     private JsonNode removeNullValues(JsonNode node) {
106         return removeAny(node, it -> it.getValue().isNull());
107     }
108
109     private JsonNode removeAny(JsonNode node, Predicate<Entry<String, JsonNode>> entryPredicate) {
110         if (node.isObject()) {
111             ((ObjectNode) node).remove(
112                 Streams.fromIterator(node.fields())
113                     .filter(entryPredicate)
114                     .map(Entry::getKey)
115                     .collect(Collectors.toList())
116             );
117
118             for (JsonNode child : node) {
119                 removeAny(child, entryPredicate);
120             }
121         }
122
123         return node;
124     }
125
126     private <T> String postAsyncInstanceRequest(T body) {
127         String[] jobsUuids = (String[]) restTemplate.exchange(
128             getCreateBulkUri(),
129             HttpMethod.POST,
130             new HttpEntity<>(body),
131             new ParameterizedTypeReference<MsoResponseWrapper2<String[]>>() {
132             })
133             .getBody().getEntity();
134
135         assertThat(jobsUuids, arrayWithSize(greaterThan(0)));
136         return jobsUuids[0];
137     }
138
139 }