added shema descriptor
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -90,6 +90,11 @@
|
||||
<artifactId>ST4</artifactId>
|
||||
<version>4.3.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.8.8</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.olympus.hermione.controllers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Ne;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.olympus.hermione.services.Neo4JUitilityService;
|
||||
import com.olympus.hermione.services.ScenarioExecutionService;
|
||||
|
||||
@RestController
|
||||
@@ -12,10 +14,20 @@ public class TestController {
|
||||
@Autowired
|
||||
ScenarioExecutionService scenarioExecutionService;
|
||||
|
||||
@Autowired
|
||||
Neo4JUitilityService neo4JUitilityService;
|
||||
|
||||
@GetMapping("/test/scenario_execution")
|
||||
public String testScenarioExecution(){
|
||||
|
||||
String result = scenarioExecutionService.executeScenario("66aa162debe80dfcd17f0ef4","How i can change the path where uploaded documents are stored ?");
|
||||
return result;
|
||||
}
|
||||
|
||||
@GetMapping("/test/generate_schema_description")
|
||||
public String testGenerateSchemaDescription(){
|
||||
return neo4JUitilityService.generateSchemaDescription().toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -72,7 +72,8 @@ public class SecurityConfig {
|
||||
http.csrf(csrf -> csrf.disable())
|
||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||
.authorizeHttpRequests(auth -> auth.requestMatchers("/api/auth/**").permitAll().requestMatchers("/api/test/**")
|
||||
.permitAll().anyRequest().authenticated());
|
||||
.permitAll().requestMatchers("/test/**")
|
||||
.permitAll().anyRequest().authenticated());
|
||||
|
||||
http.authenticationProvider(authenticationProvider());
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.olympus.hermione.services;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.neo4j.driver.Driver;
|
||||
import org.neo4j.driver.Session;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.neo4j.driver.Record;
|
||||
import org.neo4j.driver.Value;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
|
||||
@Service
|
||||
public class Neo4JUitilityService {
|
||||
|
||||
@Autowired
|
||||
Driver graphDriver;
|
||||
|
||||
|
||||
public JsonObject generateSchemaDescription() {
|
||||
try (Session session = graphDriver.session()) {
|
||||
JsonArray nodesDescription = describeNodes(session);
|
||||
JsonArray relationshipsDescription = describeRelationships(session);
|
||||
|
||||
JsonObject schemaDescription = new JsonObject();
|
||||
schemaDescription.add("nodes", nodesDescription);
|
||||
schemaDescription.add("relationships", relationshipsDescription);
|
||||
|
||||
return schemaDescription;
|
||||
}
|
||||
}
|
||||
|
||||
private JsonArray describeNodes(Session session) {
|
||||
String query = "CALL db.schema.nodeTypeProperties()";
|
||||
List<Record> records = session.run(query).list();
|
||||
|
||||
Map<String, JsonObject> nodesMap = new HashMap<>();
|
||||
|
||||
for (Record record : records) {
|
||||
String nodeType = record.get("nodeType").asString();
|
||||
String propertyName = record.get("propertyName").asString();
|
||||
|
||||
nodesMap.putIfAbsent(nodeType, new JsonObject());
|
||||
JsonObject nodeObject = nodesMap.get(nodeType);
|
||||
|
||||
JsonArray labels = nodeObject.has("labels") ? nodeObject.get("labels").getAsJsonArray() : new JsonArray();
|
||||
if (labels.size() == 0) {
|
||||
labels.add(new JsonPrimitive(nodeType));
|
||||
nodeObject.add("labels", labels);
|
||||
}
|
||||
|
||||
JsonArray properties = nodeObject.has("properties") ? nodeObject.get("properties").getAsJsonArray() : new JsonArray();
|
||||
properties.add(new JsonPrimitive(propertyName));
|
||||
nodeObject.add("properties", properties);
|
||||
}
|
||||
|
||||
JsonArray nodesArray = new JsonArray();
|
||||
nodesMap.values().forEach(nodesArray::add);
|
||||
|
||||
return nodesArray;
|
||||
}
|
||||
|
||||
private JsonArray describeRelationships(Session session) {
|
||||
String query = "CALL db.schema.relTypeProperties()";
|
||||
List<Record> records = session.run(query).list();
|
||||
|
||||
Map<String, JsonObject> relationshipsMap = new HashMap<>();
|
||||
|
||||
for (Record record : records) {
|
||||
String relType = record.get("relType").asString();
|
||||
String propertyName = record.get("propertyName").asString();
|
||||
|
||||
relationshipsMap.putIfAbsent(relType, new JsonObject());
|
||||
JsonObject relationshipObject = relationshipsMap.get(relType);
|
||||
|
||||
relationshipObject.addProperty("type", relType);
|
||||
|
||||
JsonArray properties = relationshipObject.has("properties") ? relationshipObject.get("properties").getAsJsonArray() : new JsonArray();
|
||||
properties.add(new JsonPrimitive(propertyName));
|
||||
relationshipObject.add("properties", properties);
|
||||
}
|
||||
|
||||
JsonArray relationshipsArray = new JsonArray();
|
||||
relationshipsMap.values().forEach(relationshipsArray::add);
|
||||
|
||||
return relationshipsArray;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.olympus.hermione;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.olympus.hermione.services.Neo4JUitilityService;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
|
||||
public class Neo4jSchemaDescriptionTest {
|
||||
|
||||
@Autowired
|
||||
private Neo4JUitilityService neo4JUitilityService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void testGenerateSchemaDescription() {
|
||||
// Esegui il metodo da testare
|
||||
JsonObject schema = neo4JUitilityService.generateSchemaDescription();
|
||||
|
||||
// Verifica che il JSON contenga le chiavi "nodes" e "relationships"
|
||||
assertTrue(schema.has("nodes"), "Il JSON dovrebbe contenere la chiave 'nodes'");
|
||||
assertTrue(schema.has("relationships"), "Il JSON dovrebbe contenere la chiave 'relationships'");
|
||||
|
||||
// Verifica che "nodes" e "relationships" siano degli array
|
||||
assertTrue(schema.get("nodes").isJsonArray(), "'nodes' dovrebbe essere un array");
|
||||
assertTrue(schema.get("relationships").isJsonArray(), "'relationships' dovrebbe essere un array");
|
||||
|
||||
// Verifica che ogni nodo abbia le chiavi "labels" e "properties"
|
||||
JsonArray nodes = schema.get("nodes").getAsJsonArray();
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
JsonObject node = nodes.get(i).getAsJsonObject();
|
||||
assertTrue(node.has("labels"), "Ogni nodo dovrebbe avere la chiave 'labels'");
|
||||
assertTrue(node.has("properties"), "Ogni nodo dovrebbe avere la chiave 'properties'");
|
||||
}
|
||||
|
||||
// Verifica che ogni relazione abbia le chiavi "type" e "properties"
|
||||
JsonArray relationships = schema.get("relationships").getAsJsonArray();
|
||||
for (int i = 0; i < relationships.size(); i++) {
|
||||
JsonObject relationship = relationships.get(i).getAsJsonObject();
|
||||
assertTrue(relationship.has("type"), "Ogni relazione dovrebbe avere la chiave 'type'");
|
||||
assertTrue(relationship.has("properties"), "Ogni relazione dovrebbe avere la chiave 'properties'");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user