From 53d83244c1bf36af86e90ce5fe758a369f73563e Mon Sep 17 00:00:00 2001 From: "serena.spinoso" Date: Sat, 25 Feb 2017 12:00:55 +0100 Subject: Add verigraph code base JIRA: PARSER-111 Change-Id: Ie76e14fabbb6c388ebc89d9a15dd3021b25fa892 Signed-off-by: serena.spinoso --- .../verify/service/JsonValidationService.java | 136 +++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java (limited to 'verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java') diff --git a/verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java b/verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java new file mode 100644 index 0000000..1ac6c1f --- /dev/null +++ b/verigraph/src/main/java/it/polito/escape/verify/service/JsonValidationService.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (c) 2017 Politecnico di Torino and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +package it.polito.escape.verify.service; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Iterator; +import java.util.Map.Entry; + +import org.apache.commons.lang3.text.WordUtils; + +import com.fasterxml.jackson.databind.JsonNode; + +import it.polito.escape.verify.exception.BadRequestException; +import it.polito.escape.verify.model.Graph; +import it.polito.escape.verify.model.Node; + +public class JsonValidationService { + + private Graph graph = new Graph(); + + private Node node = new Node(); + + public JsonValidationService() { + + } + + public JsonValidationService(Graph graph, Node node) { + this.graph = graph; + this.node = node; + } + + public boolean validateFieldAgainstNodeNames(String value) { + for (Node node : this.graph.getNodes().values()) { + if (node.getName().equals(value)) + return true; + } + return false; + } + + public void validateFieldsAgainstNodeNames(JsonNode node) { + if (node.isTextual()) { + boolean isValid = validateFieldAgainstNodeNames(node.asText()); + if (!isValid) { + System.out.println(node.asText() + " is not a valid string!"); + throw new BadRequestException("String '" + node.asText() + + "' is not valid for the configuration of node '" + this.node.getName() + + "'"); + } + } + if (node.isArray()) { + for (JsonNode object : node) { + validateFieldsAgainstNodeNames(object); + } + } + if (node.isObject()) { + Iterator> iter = node.fields(); + + while (iter.hasNext()) { + Entry item = iter.next(); + validateFieldsAgainstNodeNames(item.getValue()); + } + } + + } + + public boolean validateNodeConfiguration() { + String className = WordUtils.capitalize(node.getFunctional_type()) + "Validator"; + + Class validator; + try { + validator = Class.forName("it.polito.escape.verify.validation." + className); + } + catch (ClassNotFoundException e) { + System.out.println(className + " not found, configuration properties of node '" + node.getName() + + "' will be validated against node names"); + return false; + } + + Class graphClass; + Class nodeClass; + Class configurationClass; + try { + graphClass = Class.forName("it.polito.escape.verify.model.Graph"); + nodeClass = Class.forName("it.polito.escape.verify.model.Node"); + configurationClass = Class.forName("it.polito.escape.verify.model.Configuration"); + } + catch (ClassNotFoundException e) { + throw new RuntimeException("Model classes not found"); + } + + Class[] paramTypes = new Class[3]; + paramTypes[0] = graphClass; + paramTypes[1] = nodeClass; + paramTypes[2] = configurationClass; + + String methodName = "validate"; + + Object instance; + try { + instance = validator.newInstance(); + } + catch (InstantiationException e) { + throw new RuntimeException("'" + className + "' cannot be instantiated"); + } + catch (IllegalAccessException e) { + throw new RuntimeException("Illegal access to '" + className + "' instantiation"); + } + + Method myMethod; + try { + myMethod = validator.getDeclaredMethod(methodName, paramTypes); + } + catch (NoSuchMethodException e) { + throw new RuntimeException("'" + methodName + "' method has to be implemented in " + className + " class"); + } + try { + myMethod.invoke(instance, graph, node, node.getConfiguration()); + } + catch (IllegalAccessException e) { + throw new RuntimeException("Illegal access to '" + methodName + "' method in " + className + " instance"); + } + catch (InvocationTargetException e) { + throw new BadRequestException("Validation failed for node '" + node.getName() + "': " + + e.getTargetException().getMessage()); + } + return true; + } +} -- cgit 1.2.3-korg