c39fb013319815e70db3611f7b3e51a857cd0a9b
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / vestest / TestJsonSchemaValidation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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.dcae.vestest;
22
23 import static java.nio.file.Files.readAllBytes;
24 import static junit.framework.Assert.assertEquals;
25
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import java.io.IOException;
29 import java.nio.file.Paths;
30 import org.junit.Test;
31 import org.onap.dcae.SchemaValidator;
32
33 public class TestJsonSchemaValidation {
34
35     @Test
36     public void shouldValidEventPassSchema_27_2() throws IOException {
37         String result = SchemaValidator.validateAgainstSchema(
38                 readJSONFromFile("src/test/resources/VES_valid.txt").toString(),
39                 readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
40         assertEquals(result, "true");
41     }
42
43
44     @Test
45     public void shouldInvalidEventDoesNotPassSchema_27_2() throws IOException {
46         String result = SchemaValidator.validateAgainstSchema(
47                 readJSONFromFile("src/test/resources/VES_invalid.txt").toString(),
48                 readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
49         assertEquals(result, "false");
50     }
51
52
53     private static JsonObject readJSONFromFile(String path) throws IOException {
54         return (JsonObject) new JsonParser().parse(new String(readAllBytes(Paths.get(path))));
55     }
56 }
57