VES 7.0.1 updates
[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 =
38         SchemaValidator.validateAgainstSchema(
39             readJSONFromFile("src/test/resources/ves4_valid.json").toString(),
40             readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
41     assertEquals(result, "true");
42   }
43
44   @Test
45   public void shouldInvalidEventDoesNotPassSchema_27_2() throws IOException {
46     String result =
47         SchemaValidator.validateAgainstSchema(
48             readJSONFromFile("src/test/resources/ves4_invalid.json").toString(),
49             readJSONFromFile("etc/CommonEventFormat_27.2.json").toString());
50     assertEquals(result, "false");
51   }
52
53   @Test
54   public void shouldValidEventPassSchema_30_0_1() throws IOException {
55     String result =
56         SchemaValidator.validateAgainstSchema(
57             readJSONFromFile("src/test/resources/ves7_valid.json").toString(),
58             readJSONFromFile("etc/CommonEventFormat_30.0.1.json").toString());
59     assertEquals(result, "true");
60   }
61
62   @Test
63   public void shouldValidEventBatchPassSchema_30_0_1() throws IOException {
64     String result =
65         SchemaValidator.validateAgainstSchema(
66             readJSONFromFile("src/test/resources/ves7_batch_valid.json").toString(),
67             readJSONFromFile("etc/CommonEventFormat_30.0.1.json").toString());
68     assertEquals(result, "true");
69   }
70
71   @Test
72   public void shouldInvalidEventDoesNotPassSchema_30_0_1() throws IOException {
73     String result =
74         SchemaValidator.validateAgainstSchema(
75             readJSONFromFile("src/test/resources/ves7_invalid.json").toString(),
76             readJSONFromFile("etc/CommonEventFormat_30.0.1.json").toString());
77     assertEquals(result, "false");
78   }
79
80   private static JsonObject readJSONFromFile(String path) throws IOException {
81     return (JsonObject) new JsonParser().parse(new String(readAllBytes(Paths.get(path))));
82   }
83 }