Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-inbound / appc-design-services / provider / src / main / java / org / onap / appc / design / validator / ValidatorService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.design.validator;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
30 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.introspector.BeanAccess;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.io.Reader;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38 import javax.xml.parsers.ParserConfigurationException;
39 import org.apache.velocity.app.Velocity;
40 import org.apache.velocity.app.VelocityEngine;
41 import org.apache.velocity.exception.MethodInvocationException;
42 import org.apache.velocity.exception.ParseErrorException;
43 import org.apache.velocity.exception.ResourceNotFoundException;
44 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
45 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
46 import org.onap.appc.design.services.util.DesignServiceConstants;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.xml.sax.InputSource;
50 import org.xml.sax.SAXException;
51
52 public class ValidatorService {
53
54     private static final Logger log = LoggerFactory.getLogger(ValidatorService.class);
55
56     public String execute(String action, String payload, String dataType) throws ValidatorException {
57
58         log.info("Received validation for action= " + action + "Data :" + payload + " dataType = " + dataType);
59         String validateResponse  = null;
60
61         try{
62             switch (dataType) {
63                 case DesignServiceConstants.DATA_TYPE_XML:
64                     validateResponse = validateXML(payload);
65                     break;
66                 case DesignServiceConstants.DATA_TYPE_JSON:
67                     validateResponse = validateJSON(payload);
68                     break;
69                 case DesignServiceConstants.DATA_TYPE_VELOCITY:
70                     validateResponse = validateVelocity(payload);
71                     break;
72                 case DesignServiceConstants.DATA_TYPE_YAML:
73                     validateResponse = validateYAML(payload);
74                     break;
75                 default:
76                     break;
77             }
78         }
79         catch (ParserConfigurationException | SAXException | IOException e){
80             log.info("An error occurred while executing validator", e);
81             throw new ValidatorException("An error occurred while executing validator", e);
82         }
83         return validateResponse;
84     }
85
86     private String validateYAML(String payload) throws IOException {
87         try {
88             InputStream is = new ByteArrayInputStream(payload.getBytes());
89
90             Reader in = new InputStreamReader(is);
91             Yaml yaml = new Yaml();
92             yaml.setBeanAccess(BeanAccess.FIELD);
93             yaml.load(in);
94             return DesignServiceConstants.SUCCESS;
95         } catch (Exception e) {
96             log.error("Not a Valid YAML Format", e);
97             throw e;
98         }
99
100     }
101
102     private String validateVelocity(String payload) {
103
104         try {
105             VelocityEngine engine = new VelocityEngine();
106             engine.setProperty(Velocity.RESOURCE_LOADER, "string");
107             engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
108             engine.addProperty("string.resource.loader.repository.static", "false");
109             engine.init();
110             StringResourceRepository repo = (StringResourceRepository) engine
111                 .getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
112             repo.putStringResource("TestTemplate", payload);
113
114             return DesignServiceConstants.SUCCESS;
115         } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
116             log.error("Not a Valid Velocity Template", e);
117             throw e;
118         }
119     }
120
121     private String validateJSON(String payload) throws IOException {
122
123         try {
124             ObjectMapper objectMapper = new ObjectMapper();
125             objectMapper.readTree(payload);
126             return DesignServiceConstants.SUCCESS;
127         } catch (JsonProcessingException e) {
128             log.error("Not a Valid JSON file", e);
129             throw e;
130         }
131
132     }
133
134     private String validateXML(String payload) throws IOException, SAXException, ParserConfigurationException {
135
136         try {
137             DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
138             DocumentBuilder builder = dBF.newDocumentBuilder();
139             builder.parse(new InputSource(new ByteArrayInputStream(payload.getBytes("utf-8"))));
140             return DesignServiceConstants.SUCCESS;
141
142         } catch (ParserConfigurationException | SAXException | IOException e) {
143             log.info("Error While parsing Payload", e);
144             throw e;
145         }
146     }
147 }
148