Remove outdated doc for A1 Adaptor
[integration.git] / test / mocks / masspnfsim / pnf-sim-lightweight / src / main / java / org / onap / pnfsimulator / simulator / validation / JSONValidator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA 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.pnfsimulator.simulator.validation;
22
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.github.fge.jackson.JsonLoader;
25 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
26 import com.github.fge.jsonschema.core.report.LogLevel;
27 import com.github.fge.jsonschema.core.report.ProcessingMessage;
28 import com.github.fge.jsonschema.core.report.ProcessingReport;
29 import com.github.fge.jsonschema.main.JsonSchema;
30 import com.github.fge.jsonschema.main.JsonSchemaFactory;
31 import com.google.gson.JsonParser;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.util.stream.Collectors;
35 import java.util.stream.StreamSupport;
36
37 public class JSONValidator {
38
39     public void validate(String data, String jsonSchemaPath)
40         throws ValidationException, ProcessingException, IOException {
41         String jsonSchema = readJsonSchemaAsString(jsonSchemaPath);
42         JsonNode jsonData = JsonLoader.fromString(data);
43         ProcessingReport report = createJsonSchema(jsonSchema).validate(jsonData);
44
45         if (!report.isSuccess()) {
46             throw new ValidationException(constructValidationErrors(report));
47         }
48     }
49
50     private String readJsonSchemaAsString(String schemaPath) throws IOException {
51         try (FileReader reader = new FileReader(schemaPath)) {
52             return new JsonParser().parse(reader).toString();
53         }
54     }
55
56     private JsonSchema createJsonSchema(String schema) throws ProcessingException, IOException {
57         return JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(schema));
58     }
59
60     private String constructValidationErrors(ProcessingReport report) {
61         return StreamSupport.stream(report.spliterator(), false)
62             .filter(entry -> entry.getLogLevel() == LogLevel.ERROR)
63             .map(ProcessingMessage::getMessage)
64             .collect(Collectors.joining("\n"));
65     }
66 }