7e74dd493e5b3db3b9a166912947fb82eff5ac79
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.integration;
22
23 import static io.restassured.RestAssured.given;
24
25 import io.restassured.http.Header;
26 import io.restassured.path.json.JsonPath;
27 import io.restassured.path.json.config.JsonPathConfig;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.Map;
33 import org.hamcrest.Matchers;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
40 import org.springframework.test.context.junit4.SpringRunner;
41 import org.springframework.util.ResourceUtils;
42
43 @RunWith(SpringRunner.class)
44 @SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = WebEnvironment.DEFINED_PORT)
45 public class TemplatesManagementTest {
46
47     private static final String LIST_URL = "list";
48     private static final String GET_URL = "get/";
49     private static final String UPLOAD = "upload";
50     private static final String NOTIFICATION_JSON = "notification.json";
51     private static final String REGISTRATION_JSON = "registration.json";
52     private static final String UPLOAD_TEMPLATE_JSON = "upload_template.json";
53     private static final String OVERWRITE_TEMPLATE_JSON = "overwrite_template.json";
54     private static final String OVERWRITTEN_TEMPLATE_JSON = "overwritten_template.json";
55     private static final String APPLICATION_JSON = "application/json";
56     private static final String CONTENT_TYPE = "Content-Type";
57     private static final String FORCE_FLAG = "?override=true";
58     private static final String CONTENT = "content";
59     private static final String TEMPLATE = "template";
60     private static final String ID = "id";
61
62     @Test
63     public void whenCallingGetShouldReceiveNotificationTemplate() throws IOException {
64         given()
65             .when()
66             .get(prepareRequestUrl(GET_URL) + NOTIFICATION_JSON)
67             .then()
68             .statusCode(200)
69             .body(ID, Matchers.equalTo(NOTIFICATION_JSON))
70             .body(CONTENT, Matchers.equalTo(readTemplateFromResources(NOTIFICATION_JSON).getMap(CONTENT)));
71     }
72
73     @Test
74     public void whenCallingGetShouldReceiveRegistrationTemplate() throws IOException {
75         given()
76             .when()
77             .get(prepareRequestUrl(GET_URL) + REGISTRATION_JSON)
78             .then()
79             .statusCode(200)
80             .body(ID, Matchers.equalTo(REGISTRATION_JSON))
81             .body(CONTENT, Matchers.equalTo(readTemplateFromResources(REGISTRATION_JSON).getMap(CONTENT)));
82     }
83
84     @Test
85     public void whenCallingListShouldReceiveAllPredefinedTemplates() throws IOException {
86         Map<Object, Object> registration = readTemplateFromResources(REGISTRATION_JSON).getMap(CONTENT);
87         Map<Object, Object> notification = readTemplateFromResources(NOTIFICATION_JSON).getMap(CONTENT);
88
89         given()
90             .when()
91             .get(prepareRequestUrl(LIST_URL))
92             .then()
93             .statusCode(200)
94             .body(CONTENT, Matchers.<Map>hasItems(
95                 registration,
96                 notification
97             ));
98     }
99
100     @Test
101     public void whenCallingUploadAndGetShouldReceiveNewTemplate() throws IOException {
102         byte[] body = Files.readAllBytes(readFileFromTemplatesFolder(UPLOAD_TEMPLATE_JSON));
103
104         given()
105             .body(body)
106             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
107             .when()
108             .post(prepareRequestUrl(UPLOAD))
109             .then()
110             .statusCode(201);
111
112         given()
113             .when()
114             .get(prepareRequestUrl(GET_URL) + UPLOAD_TEMPLATE_JSON)
115             .then()
116             .statusCode(200)
117             .body(ID, Matchers.equalTo(UPLOAD_TEMPLATE_JSON))
118             .body(CONTENT, Matchers.equalTo(readTemplateFromResources(UPLOAD_TEMPLATE_JSON).getMap(TEMPLATE)));
119     }
120
121     @Test
122     public void whenCallingOverrideAndGetShouldReceiveNewTemplate() throws IOException, JSONException {
123         byte[] body = Files.readAllBytes(readFileFromTemplatesFolder(OVERWRITE_TEMPLATE_JSON));
124
125         given()
126             .body(body)
127             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
128             .when()
129             .post(prepareRequestUrl(UPLOAD))
130             .then()
131             .statusCode(201);
132
133         JSONObject overwrittenBody = new JSONObject(new String(body));
134         JSONObject overwrittenTemplate = new JSONObject("{\"field1\": \"overwritten_field1\"}");
135         overwrittenBody.put(TEMPLATE, overwrittenTemplate);
136
137         given()
138             .body(overwrittenBody.toString().getBytes())
139             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
140             .when()
141             .post(prepareRequestUrl(UPLOAD))
142             .then()
143             .statusCode(409);
144
145         given()
146             .body(overwrittenBody.toString().getBytes())
147             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
148             .when()
149             .post(prepareRequestUrl(UPLOAD + FORCE_FLAG))
150             .then()
151             .statusCode(201);
152
153         given()
154             .when()
155             .get(prepareRequestUrl(GET_URL) + OVERWRITE_TEMPLATE_JSON)
156             .then()
157             .statusCode(200)
158             .body(ID, Matchers.equalTo(OVERWRITE_TEMPLATE_JSON))
159             .body(CONTENT, Matchers.equalTo(readTemplateFromResources(OVERWRITTEN_TEMPLATE_JSON).getMap(CONTENT)));
160     }
161
162     private String prepareRequestUrl(String action) {
163         return "http://0.0.0.0:5000/template/" + action;
164     }
165
166     private JsonPath readTemplateFromResources(String templateName) throws IOException {
167         byte[] content = Files.readAllBytes(readFileFromTemplatesFolder(templateName));
168         return new JsonPath(new String(content)).using(new JsonPathConfig("UTF-8"));
169     }
170
171     private Path readFileFromTemplatesFolder(String templateName) throws FileNotFoundException {
172         return ResourceUtils.getFile("classpath:templates/"+templateName).toPath();
173     }
174
175 }