2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.integration;
23 import static io.restassured.RestAssured.given;
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;
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;
43 @RunWith(SpringRunner.class)
44 @SpringBootTest(classes = {Main.class, TestConfiguration.class}, webEnvironment = WebEnvironment.DEFINED_PORT)
45 public class TemplatesManagementTest {
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";
63 public void whenCallingGetShouldReceiveNotificationTemplate() throws IOException {
66 .get(prepareRequestUrl(GET_URL) + NOTIFICATION_JSON)
69 .body(ID, Matchers.equalTo(NOTIFICATION_JSON))
70 .body(CONTENT, Matchers.equalTo(readTemplateFromResources(NOTIFICATION_JSON).getMap(CONTENT)));
74 public void whenCallingGetShouldReceiveRegistrationTemplate() throws IOException {
77 .get(prepareRequestUrl(GET_URL) + REGISTRATION_JSON)
80 .body(ID, Matchers.equalTo(REGISTRATION_JSON))
81 .body(CONTENT, Matchers.equalTo(readTemplateFromResources(REGISTRATION_JSON).getMap(CONTENT)));
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);
91 .get(prepareRequestUrl(LIST_URL))
94 .body(CONTENT, Matchers.<Map>hasItems(
101 public void whenCallingUploadAndGetShouldReceiveNewTemplate() throws IOException {
102 byte[] body = Files.readAllBytes(readFileFromTemplatesFolder(UPLOAD_TEMPLATE_JSON));
106 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
108 .post(prepareRequestUrl(UPLOAD))
114 .get(prepareRequestUrl(GET_URL) + UPLOAD_TEMPLATE_JSON)
117 .body(ID, Matchers.equalTo(UPLOAD_TEMPLATE_JSON))
118 .body(CONTENT, Matchers.equalTo(readTemplateFromResources(UPLOAD_TEMPLATE_JSON).getMap(TEMPLATE)));
122 public void whenCallingOverrideAndGetShouldReceiveNewTemplate() throws IOException, JSONException {
123 byte[] body = Files.readAllBytes(readFileFromTemplatesFolder(OVERWRITE_TEMPLATE_JSON));
127 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
129 .post(prepareRequestUrl(UPLOAD))
133 JSONObject overwrittenBody = new JSONObject(new String(body));
134 JSONObject overwrittenTemplate = new JSONObject("{\"field1\": \"overwritten_field1\"}");
135 overwrittenBody.put(TEMPLATE, overwrittenTemplate);
138 .body(overwrittenBody.toString().getBytes())
139 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
141 .post(prepareRequestUrl(UPLOAD))
146 .body(overwrittenBody.toString().getBytes())
147 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
149 .post(prepareRequestUrl(UPLOAD + FORCE_FLAG))
155 .get(prepareRequestUrl(GET_URL) + OVERWRITE_TEMPLATE_JSON)
158 .body(ID, Matchers.equalTo(OVERWRITE_TEMPLATE_JSON))
159 .body(CONTENT, Matchers.equalTo(readTemplateFromResources(OVERWRITTEN_TEMPLATE_JSON).getMap(CONTENT)));
162 private String prepareRequestUrl(String action) {
163 return "http://0.0.0.0:5000/template/" + action;
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"));
171 private Path readFileFromTemplatesFolder(String templateName) throws FileNotFoundException {
172 return ResourceUtils.getFile("classpath:templates/"+templateName).toPath();