Stop installing librairies during tests
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / service / JsonValidationService.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package it.polito.escape.verify.service;
11
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.util.Iterator;
15 import java.util.Map.Entry;
16
17 import org.apache.commons.lang3.text.WordUtils;
18
19 import com.fasterxml.jackson.databind.JsonNode;
20
21 import it.polito.escape.verify.exception.BadRequestException;
22 import it.polito.escape.verify.model.Graph;
23 import it.polito.escape.verify.model.Node;
24
25 public class JsonValidationService {
26
27         private Graph   graph   = new Graph();
28
29         private Node    node    = new Node();
30
31         public JsonValidationService() {
32
33         }
34
35         public JsonValidationService(Graph graph, Node node) {
36                 this.graph = graph;
37                 this.node = node;
38         }
39
40         public boolean validateFieldAgainstNodeNames(String value) {
41                 for (Node node : this.graph.getNodes().values()) {
42                         if (node.getName().equals(value))
43                                 return true;
44                 }
45                 return false;
46         }
47
48         public void validateFieldsAgainstNodeNames(JsonNode node) {
49                 if (node.isTextual()) {
50                         boolean isValid = validateFieldAgainstNodeNames(node.asText());
51                         if (!isValid) {
52                                 System.out.println(node.asText() + " is not a valid string!");
53                                 throw new BadRequestException("String '"        + node.asText()
54                                                                                                 + "' is not valid for the configuration of node '" + this.node.getName()
55                                                                                                 + "'");
56                         }
57                 }
58                 if (node.isArray()) {
59                         for (JsonNode object : node) {
60                                 validateFieldsAgainstNodeNames(object);
61                         }
62                 }
63                 if (node.isObject()) {
64                         Iterator<Entry<String, JsonNode>> iter = node.fields();
65
66                         while (iter.hasNext()) {
67                                 Entry<String, JsonNode> item = iter.next();
68                                 validateFieldsAgainstNodeNames(item.getValue());
69                         }
70                 }
71
72         }
73
74         public boolean validateNodeConfiguration() {
75                 String className = WordUtils.capitalize(node.getFunctional_type()) + "Validator";
76
77                 Class<?> validator;
78                 try {
79                         validator = Class.forName("it.polito.escape.verify.validation." + className);
80                 }
81                 catch (ClassNotFoundException e) {
82                         System.out.println(className    + " not found, configuration properties of node '" + node.getName()
83                                                                 + "' will be validated against node names");
84                         return false;
85                 }
86
87                 Class<?> graphClass;
88                 Class<?> nodeClass;
89                 Class<?> configurationClass;
90                 try {
91                         graphClass = Class.forName("it.polito.escape.verify.model.Graph");
92                         nodeClass = Class.forName("it.polito.escape.verify.model.Node");
93                         configurationClass = Class.forName("it.polito.escape.verify.model.Configuration");
94                 }
95                 catch (ClassNotFoundException e) {
96                         throw new RuntimeException("Model classes not found");
97                 }
98
99                 Class<?>[] paramTypes = new Class[3];
100                 paramTypes[0] = graphClass;
101                 paramTypes[1] = nodeClass;
102                 paramTypes[2] = configurationClass;
103
104                 String methodName = "validate";
105
106                 Object instance;
107                 try {
108                         instance = validator.newInstance();
109                 }
110                 catch (InstantiationException e) {
111                         throw new RuntimeException("'" + className + "' cannot be instantiated");
112                 }
113                 catch (IllegalAccessException e) {
114                         throw new RuntimeException("Illegal access to '" + className + "' instantiation");
115                 }
116
117                 Method myMethod;
118                 try {
119                         myMethod = validator.getDeclaredMethod(methodName, paramTypes);
120                 }
121                 catch (NoSuchMethodException e) {
122                         throw new RuntimeException("'" + methodName + "' method has to be implemented in " + className + " class");
123                 }
124                 try {
125                         myMethod.invoke(instance, graph, node, node.getConfiguration());
126                 }
127                 catch (IllegalAccessException e) {
128                         throw new RuntimeException("Illegal access to '" + methodName + "' method in " + className + " instance");
129                 }
130                 catch (InvocationTargetException e) {
131                         throw new BadRequestException("Validation failed for node '"    + node.getName() + "': "
132                                                                                         + e.getTargetException().getMessage());
133                 }
134                 return true;
135         }
136 }