9d4ff3b8ed92be3794bf7fcc7f3ca50e8f98984d
[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 import static java.nio.file.Files.readAllBytes;
25
26 import io.restassured.http.Header;
27 import java.io.File;
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;
34
35 public class SearchInTemplatesTest {
36
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";
41
42     @BeforeClass
43     public static void setUp() throws IOException {
44         for (File file : readFileFromTemplatesFolder()) {
45             byte[] body = readAllBytes(file.toPath());
46
47             given()
48                 .body(body)
49                 .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
50                 .when()
51                 .post(prepareRequestUrl(UPLOAD) + "?override=true")
52                 .then()
53                 .statusCode(201);
54         }
55     }
56
57     @Test
58     public void shouldFindNothingWhenNonexistentValueIsProvided(){
59         given()
60             .body("{\"searchExpr\": { \"child3\": \"nonexistentValue\" }}")
61             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
62             .when()
63             .post(prepareRequestUrl(SEARCH))
64             .then()
65             .statusCode(200)
66             .body("", Matchers.empty());
67     }
68
69     @Test
70     public void shouldFindNothingWhenNonexistentKeyIsProvided(){
71         given()
72             .body("{\"searchExpr\": { \"nonexistentKey\": \"Any value 1\" }}")
73             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
74             .when()
75             .post(prepareRequestUrl(SEARCH))
76             .then()
77             .statusCode(200)
78             .body("", Matchers.empty());
79     }
80
81     @Test
82     public void shouldFindNothingWhenPartOfKeyIsProvided(){
83         given()
84             .body("{\"searchExpr\": { \"child\": \"Any value 1\" }}")
85             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
86             .when()
87             .post(prepareRequestUrl(SEARCH))
88             .then()
89             .statusCode(200)
90             .body("", Matchers.empty());
91     }
92
93     @Test
94     public void shouldFindNothingWhenPartOfValueIsProvided(){
95         given()
96             .body("{\"searchExpr\": { \"child5\": \"Any\" }}")
97             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
98             .when()
99             .post(prepareRequestUrl(SEARCH))
100             .then()
101             .statusCode(200)
102             .body("", Matchers.empty());
103     }
104
105     @Test
106     public void shouldBeAbleToSearchForString(){
107         given()
108             .body("{\"searchExpr\": { \"child1\": \"Any value 1\" }}")
109             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
110             .when()
111             .post(prepareRequestUrl(SEARCH))
112             .then()
113             .statusCode(200)
114             .body("", Matchers.hasItems("template_with_array.json", "complicated_template.json", "simple_template.json"));
115
116         given()
117             .body("{\"searchExpr\": { \"child2\": \"any value 4\" }}")
118             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
119             .when()
120             .post(prepareRequestUrl(SEARCH))
121             .then()
122             .statusCode(200)
123             .body("", Matchers.hasItems("template_with_array.json"));
124     }
125
126     @Test
127     public void shouldBeAbleToSearchForManyStrings(){
128         given()
129             .body("{\"searchExpr\": { \"child1\": \"Any value 1\",  \"child2\": \"any value 2\"}}")
130             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
131             .when()
132             .post(prepareRequestUrl(SEARCH))
133             .then()
134             .statusCode(200)
135             .body("", Matchers.hasItems("simple_template.json", "complicated_template.json"));
136     }
137
138     @Test
139     public void shouldBeAbleToSearchForStarSign(){
140         given()
141             .body("{\"searchExpr\": { \"child2\": \"*\" }}")
142             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
143             .when()
144             .post(prepareRequestUrl(SEARCH))
145             .then()
146             .statusCode(200)
147             .body("", Matchers.hasItems("complicated_template.json"));
148     }
149
150     @Test
151     public void shouldBeAbleToSearchForQuestionMark(){
152         given()
153             .body("{\"searchExpr\": { \"child1\": \"?\" }}")
154             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
155             .when()
156             .post(prepareRequestUrl(SEARCH))
157             .then()
158             .statusCode(200)
159             .body("", Matchers.hasItems("complicated_template.json"));
160     }
161
162     @Test
163     public void shouldBeAbleToSearchForBrackets(){
164         given()
165             .body("{\"searchExpr\": { \"parent2\": \"[]\" }}")
166             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
167             .when()
168             .post(prepareRequestUrl(SEARCH))
169             .then()
170             .statusCode(200)
171             .body("", Matchers.hasItems("template_with_array.json"));
172     }
173
174     @Test
175     public void shouldInformThatSearchForNullsIsProhibited(){
176         given()
177             .body("{\"searchExpr\": { \"child3\":  null }}")
178             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
179             .when()
180             .post(prepareRequestUrl(SEARCH))
181             .then()
182             .statusCode(400);
183     }
184
185     @Test
186     public void shouldBeAbleToSearchForURI(){
187         given()
188             .body("{\"searchExpr\": { \"child3\": \"https://url.com?param1=test&param2=*\" }}")
189             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
190             .when()
191             .post(prepareRequestUrl(SEARCH))
192             .then()
193             .statusCode(200)
194             .body("", Matchers.hasItems("complicated_template.json"));
195     }
196
197     @Test
198     public void shouldBeAbleToSearchForFloats(){
199         given()
200             .body("{\"searchExpr\": { \"child2\": 4.44 }}")
201             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
202             .when()
203             .post(prepareRequestUrl(SEARCH))
204             .then()
205             .statusCode(200)
206             .body("", Matchers.hasItems("template_with_array.json"));
207
208         given()
209             .body("{\"searchExpr\": { \"child5\": 4.4 }}")
210             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
211             .when()
212             .post(prepareRequestUrl(SEARCH))
213             .then()
214             .statusCode(200)
215             .body("", Matchers.hasItems("complicated_template.json", "template_with_floats.json"));
216     }
217
218     @Test
219     public void shouldBeAbleToSearchForIntegers(){
220         given()
221             .body("{\"searchExpr\": { \"child2\": 1 }}")
222             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
223             .when()
224             .post(prepareRequestUrl(SEARCH))
225             .then()
226             .statusCode(200)
227             .body("", Matchers.hasItems("template_with_array.json", "template_with_ints.json"));
228
229         given()
230             .body("{\"searchExpr\": { \"child2\": 4 }}")
231             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
232             .when()
233             .post(prepareRequestUrl(SEARCH))
234             .then()
235             .statusCode(200)
236             .body("", Matchers.hasItems("template_with_array.json"));
237     }
238
239     @Test
240     public void shouldBeAbleToSearchForBooleans(){
241         given()
242             .body("{\"searchExpr\": { \"child4\": true}}")
243             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
244             .when()
245             .post(prepareRequestUrl(SEARCH))
246             .then()
247             .statusCode(200)
248             .body("", Matchers.hasItems("template_with_booleans.json"));
249
250         given()
251             .body("{\"searchExpr\": { \"parent2\": false}}")
252             .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
253             .when()
254             .post(prepareRequestUrl(SEARCH))
255             .then()
256             .statusCode(200)
257             .body("", Matchers.hasItems("template_with_booleans.json"));
258     }
259
260
261     private static String prepareRequestUrl(String action) {
262         return "http://0.0.0.0:5000/template/" + action;
263     }
264
265     private static File[] readFileFromTemplatesFolder() throws FileNotFoundException {
266         return ResourceUtils.getFile("classpath:templates/search").listFiles();
267     }
268
269 }