aboutsummaryrefslogtreecommitdiffstats
path: root/integration/src/test/java/org/onap/pnfsimulator/integration/SearchInTemplatesTest.java
blob: 9d4ff3b8ed92be3794bf7fcc7f3ca50e8f98984d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*-
 * ============LICENSE_START=======================================================
 * Simulator
 * ================================================================================
 * Copyright (C) 2019 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.pnfsimulator.integration;

import static io.restassured.RestAssured.given;
import static java.nio.file.Files.readAllBytes;

import io.restassured.http.Header;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.hamcrest.Matchers;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.util.ResourceUtils;

public class SearchInTemplatesTest {

    private static final String UPLOAD = "upload";
    private static final String SEARCH = "search";
    private static final String APPLICATION_JSON = "application/json";
    private static final String CONTENT_TYPE = "Content-Type";

    @BeforeClass
    public static void setUp() throws IOException {
        for (File file : readFileFromTemplatesFolder()) {
            byte[] body = readAllBytes(file.toPath());

            given()
                .body(body)
                .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
                .when()
                .post(prepareRequestUrl(UPLOAD) + "?override=true")
                .then()
                .statusCode(201);
        }
    }

    @Test
    public void shouldFindNothingWhenNonexistentValueIsProvided(){
        given()
            .body("{\"searchExpr\": { \"child3\": \"nonexistentValue\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.empty());
    }

    @Test
    public void shouldFindNothingWhenNonexistentKeyIsProvided(){
        given()
            .body("{\"searchExpr\": { \"nonexistentKey\": \"Any value 1\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.empty());
    }

    @Test
    public void shouldFindNothingWhenPartOfKeyIsProvided(){
        given()
            .body("{\"searchExpr\": { \"child\": \"Any value 1\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.empty());
    }

    @Test
    public void shouldFindNothingWhenPartOfValueIsProvided(){
        given()
            .body("{\"searchExpr\": { \"child5\": \"Any\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.empty());
    }

    @Test
    public void shouldBeAbleToSearchForString(){
        given()
            .body("{\"searchExpr\": { \"child1\": \"Any value 1\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_array.json", "complicated_template.json", "simple_template.json"));

        given()
            .body("{\"searchExpr\": { \"child2\": \"any value 4\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_array.json"));
    }

    @Test
    public void shouldBeAbleToSearchForManyStrings(){
        given()
            .body("{\"searchExpr\": { \"child1\": \"Any value 1\",  \"child2\": \"any value 2\"}}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("simple_template.json", "complicated_template.json"));
    }

    @Test
    public void shouldBeAbleToSearchForStarSign(){
        given()
            .body("{\"searchExpr\": { \"child2\": \"*\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("complicated_template.json"));
    }

    @Test
    public void shouldBeAbleToSearchForQuestionMark(){
        given()
            .body("{\"searchExpr\": { \"child1\": \"?\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("complicated_template.json"));
    }

    @Test
    public void shouldBeAbleToSearchForBrackets(){
        given()
            .body("{\"searchExpr\": { \"parent2\": \"[]\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_array.json"));
    }

    @Test
    public void shouldInformThatSearchForNullsIsProhibited(){
        given()
            .body("{\"searchExpr\": { \"child3\":  null }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(400);
    }

    @Test
    public void shouldBeAbleToSearchForURI(){
        given()
            .body("{\"searchExpr\": { \"child3\": \"https://url.com?param1=test&param2=*\" }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("complicated_template.json"));
    }

    @Test
    public void shouldBeAbleToSearchForFloats(){
        given()
            .body("{\"searchExpr\": { \"child2\": 4.44 }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_array.json"));

        given()
            .body("{\"searchExpr\": { \"child5\": 4.4 }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("complicated_template.json", "template_with_floats.json"));
    }

    @Test
    public void shouldBeAbleToSearchForIntegers(){
        given()
            .body("{\"searchExpr\": { \"child2\": 1 }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_array.json", "template_with_ints.json"));

        given()
            .body("{\"searchExpr\": { \"child2\": 4 }}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_array.json"));
    }

    @Test
    public void shouldBeAbleToSearchForBooleans(){
        given()
            .body("{\"searchExpr\": { \"child4\": true}}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_booleans.json"));

        given()
            .body("{\"searchExpr\": { \"parent2\": false}}")
            .header(new Header(CONTENT_TYPE, APPLICATION_JSON))
            .when()
            .post(prepareRequestUrl(SEARCH))
            .then()
            .statusCode(200)
            .body("", Matchers.hasItems("template_with_booleans.json"));
    }


    private static String prepareRequestUrl(String action) {
        return "http://0.0.0.0:5000/template/" + action;
    }

    private static File[] readFileFromTemplatesFolder() throws FileNotFoundException {
        return ResourceUtils.getFile("classpath:templates/search").listFiles();
    }

}