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;
24 import static java.nio.file.Files.readAllBytes;
26 import io.restassured.http.Header;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import org.hamcrest.Matchers;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.springframework.util.ResourceUtils;
35 public class SearchInTemplatesTest {
37 private static final String UPLOAD = "upload";
38 private static final String SEARCH = "search";
39 private static final String APPLICATION_JSON = "application/json";
40 private static final String CONTENT_TYPE = "Content-Type";
43 public static void setUp() throws IOException {
44 for (File file : readFileFromTemplatesFolder()) {
45 byte[] body = readAllBytes(file.toPath());
49 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
51 .post(prepareRequestUrl(UPLOAD) + "?override=true")
58 public void shouldFindNothingWhenNonexistentValueIsProvided(){
60 .body("{\"searchExpr\": { \"child3\": \"nonexistentValue\" }}")
61 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
63 .post(prepareRequestUrl(SEARCH))
66 .body("", Matchers.empty());
70 public void shouldFindNothingWhenNonexistentKeyIsProvided(){
72 .body("{\"searchExpr\": { \"nonexistentKey\": \"Any value 1\" }}")
73 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
75 .post(prepareRequestUrl(SEARCH))
78 .body("", Matchers.empty());
82 public void shouldFindNothingWhenPartOfKeyIsProvided(){
84 .body("{\"searchExpr\": { \"child\": \"Any value 1\" }}")
85 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
87 .post(prepareRequestUrl(SEARCH))
90 .body("", Matchers.empty());
94 public void shouldFindNothingWhenPartOfValueIsProvided(){
96 .body("{\"searchExpr\": { \"child5\": \"Any\" }}")
97 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
99 .post(prepareRequestUrl(SEARCH))
102 .body("", Matchers.empty());
106 public void shouldBeAbleToSearchForString(){
108 .body("{\"searchExpr\": { \"child1\": \"Any value 1\" }}")
109 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
111 .post(prepareRequestUrl(SEARCH))
114 .body("", Matchers.hasItems("template_with_array.json", "complicated_template.json", "simple_template.json"));
117 .body("{\"searchExpr\": { \"child2\": \"any value 4\" }}")
118 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
120 .post(prepareRequestUrl(SEARCH))
123 .body("", Matchers.hasItems("template_with_array.json"));
127 public void shouldBeAbleToSearchForManyStrings(){
129 .body("{\"searchExpr\": { \"child1\": \"Any value 1\", \"child2\": \"any value 2\"}}")
130 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
132 .post(prepareRequestUrl(SEARCH))
135 .body("", Matchers.hasItems("simple_template.json", "complicated_template.json"));
139 public void shouldBeAbleToSearchForStarSign(){
141 .body("{\"searchExpr\": { \"child2\": \"*\" }}")
142 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
144 .post(prepareRequestUrl(SEARCH))
147 .body("", Matchers.hasItems("complicated_template.json"));
151 public void shouldBeAbleToSearchForQuestionMark(){
153 .body("{\"searchExpr\": { \"child1\": \"?\" }}")
154 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
156 .post(prepareRequestUrl(SEARCH))
159 .body("", Matchers.hasItems("complicated_template.json"));
163 public void shouldBeAbleToSearchForBrackets(){
165 .body("{\"searchExpr\": { \"parent2\": \"[]\" }}")
166 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
168 .post(prepareRequestUrl(SEARCH))
171 .body("", Matchers.hasItems("template_with_array.json"));
175 public void shouldInformThatSearchForNullsIsProhibited(){
177 .body("{\"searchExpr\": { \"child3\": null }}")
178 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
180 .post(prepareRequestUrl(SEARCH))
186 public void shouldBeAbleToSearchForURI(){
188 .body("{\"searchExpr\": { \"child3\": \"https://url.com?param1=test¶m2=*\" }}")
189 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
191 .post(prepareRequestUrl(SEARCH))
194 .body("", Matchers.hasItems("complicated_template.json"));
198 public void shouldBeAbleToSearchForFloats(){
200 .body("{\"searchExpr\": { \"child2\": 4.44 }}")
201 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
203 .post(prepareRequestUrl(SEARCH))
206 .body("", Matchers.hasItems("template_with_array.json"));
209 .body("{\"searchExpr\": { \"child5\": 4.4 }}")
210 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
212 .post(prepareRequestUrl(SEARCH))
215 .body("", Matchers.hasItems("complicated_template.json", "template_with_floats.json"));
219 public void shouldBeAbleToSearchForIntegers(){
221 .body("{\"searchExpr\": { \"child2\": 1 }}")
222 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
224 .post(prepareRequestUrl(SEARCH))
227 .body("", Matchers.hasItems("template_with_array.json", "template_with_ints.json"));
230 .body("{\"searchExpr\": { \"child2\": 4 }}")
231 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
233 .post(prepareRequestUrl(SEARCH))
236 .body("", Matchers.hasItems("template_with_array.json"));
240 public void shouldBeAbleToSearchForBooleans(){
242 .body("{\"searchExpr\": { \"child4\": true}}")
243 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
245 .post(prepareRequestUrl(SEARCH))
248 .body("", Matchers.hasItems("template_with_booleans.json"));
251 .body("{\"searchExpr\": { \"parent2\": false}}")
252 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
254 .post(prepareRequestUrl(SEARCH))
257 .body("", Matchers.hasItems("template_with_booleans.json"));
261 private static String prepareRequestUrl(String action) {
262 return "http://0.0.0.0:5000/template/" + action;
265 private static File[] readFileFromTemplatesFolder() throws FileNotFoundException {
266 return ResourceUtils.getFile("classpath:templates/search").listFiles();