resource-registry/src/test/java/org/gcube/informationsystem/resourceregistry/queries/JsonQueryTest.java

295 lines
9.9 KiB
Java
Raw Normal View History

2021-10-25 11:00:54 +02:00
package org.gcube.informationsystem.resourceregistry.queries;
2021-09-27 18:00:18 +02:00
2021-10-18 11:48:53 +02:00
import java.io.BufferedReader;
2021-09-27 18:00:18 +02:00
import java.io.File;
2021-10-18 11:48:53 +02:00
import java.io.FileReader;
2022-12-08 14:27:11 +01:00
import java.io.FilenameFilter;
2021-09-27 18:00:18 +02:00
import java.net.URL;
2023-09-27 15:16:58 +02:00
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
2021-09-27 18:00:18 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
2023-09-27 15:16:58 +02:00
import org.gcube.informationsystem.model.reference.entities.Entity;
2021-09-27 18:00:18 +02:00
import org.gcube.informationsystem.resourceregistry.ContextTest;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.queries.json.JsonQuery;
2023-09-27 15:16:58 +02:00
import org.gcube.informationsystem.serialization.ElementMapper;
2021-10-18 11:48:53 +02:00
import org.junit.Assert;
2021-09-27 18:00:18 +02:00
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-10-14 15:01:22 +02:00
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-09-27 18:00:18 +02:00
public class JsonQueryTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(JsonQueryTest.class);
2021-10-18 11:48:53 +02:00
public File getQueriesDirectory() throws Exception {
URL logbackFileURL = JsonQueryTest.class.getClassLoader().getResource("logback-test.xml");
File logbackFile = new File(logbackFileURL.toURI());
File resourcesDirectory = logbackFile.getParentFile();
return new File(resourcesDirectory, "queries");
}
2023-11-20 17:34:48 +01:00
public File getProjectionQueriesDirectory() throws Exception {
URL logbackFileURL = JsonQueryTest.class.getClassLoader().getResource("logback-test.xml");
File logbackFile = new File(logbackFileURL.toURI());
File resourcesDirectory = logbackFile.getParentFile();
return new File(resourcesDirectory, "projection-queries");
}
2023-11-27 17:44:15 +01:00
protected boolean compareQueries(StringBuffer createdSb, StringBuffer expectedSb) {
return compareQueries(createdSb.toString(), expectedSb.toString());
}
protected String normalizeString(String s) {
return s.replaceAll("\n{1,}", "")
.replaceAll("\r{1,}", "")
.replaceAll("\t{1,}", "")
.replaceAll("\\s{2,}", " ")
.replaceAll("\\(\\s{1,}", "(")
.replaceAll("\\s{1,}\\(", "(")
.replaceAll("\\)\\s{1,}", ")")
.replaceAll("\\s{1,}\\)", ")");
}
protected boolean compareQueries(String createdString, String expectedString) {
String created = normalizeString(createdString);
String expected = normalizeString(expectedString);
logger.debug(created);
logger.debug(expected);
return created.compareTo(expected)==0 ? true : false;
}
@Test
public void testCompares() throws Exception {
String a = "))\n\t\r ) ) ) )";
String b = "))))))";
Assert.assertTrue(compareQueries(a, b));
}
2021-09-27 18:00:18 +02:00
@Test
public void testQueries() throws Exception {
ContextTest.setContextByName(DEVVRE);
2021-10-18 11:48:53 +02:00
File queriesDirectory = getQueriesDirectory();
2021-09-27 18:00:18 +02:00
2022-12-08 14:27:11 +01:00
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".json");
}
};
for(File jsonQueryFile : queriesDirectory.listFiles(filenameFilter)) {
2023-11-24 15:36:43 +01:00
logger.info("Going to read JSON query from file {}", jsonQueryFile.getAbsolutePath());
2022-12-08 14:27:11 +01:00
2021-10-18 11:48:53 +02:00
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery();
jsonQuery.setJsonQuery(jsonNode);
StringBuffer createdStringBuffer = jsonQuery.createQuery();
2021-10-18 11:48:53 +02:00
logger.info("Created Query from JSON: {}", createdStringBuffer.toString());
StringBuffer expectedStringBuffer = new StringBuffer();
2023-11-27 17:44:15 +01:00
File expectedQueryFile = new File(queriesDirectory, jsonQueryFile.getName().replace("json", "match.oquery"));
2021-10-18 11:48:53 +02:00
try(BufferedReader br = new BufferedReader(new FileReader(expectedQueryFile))) {
for(String line; (line = br.readLine()) != null; ) {
expectedStringBuffer.append(line);
}
}
logger.info("Expected Query from JSON: {}", expectedStringBuffer.toString());
2023-11-27 17:44:15 +01:00
Assert.assertTrue(compareQueries(createdStringBuffer, expectedStringBuffer));
2021-10-18 11:48:53 +02:00
String result = jsonQuery.query();
logger.info("Result : {}", result);
2021-10-18 11:48:53 +02:00
}
2021-09-27 18:00:18 +02:00
}
@Test
public void testSingleQuery() throws Exception {
File queriesDirectory = getQueriesDirectory();
2023-11-29 13:19:48 +01:00
File jsonQueryFile = new File(queriesDirectory, "query10.json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery();
jsonQuery.setJsonQuery(jsonNode);
StringBuffer createdStringBuffer = jsonQuery.createQuery();
logger.info("Created Query from JSON: {}", createdStringBuffer.toString());
2023-04-20 18:46:39 +02:00
StringBuffer expectedStringBuffer = new StringBuffer();
2023-11-27 17:44:15 +01:00
File expectedQueryFile = new File(queriesDirectory, jsonQueryFile.getName().replace("json", "match.oquery"));
2023-04-20 18:46:39 +02:00
try(BufferedReader br = new BufferedReader(new FileReader(expectedQueryFile))) {
for(String line; (line = br.readLine()) != null; ) {
expectedStringBuffer.append(line);
}
}
logger.info("Expected Query from JSON: {}", expectedStringBuffer.toString());
2023-11-27 17:44:15 +01:00
Assert.assertTrue(compareQueries(createdStringBuffer, expectedStringBuffer));
2023-04-20 18:46:39 +02:00
String result = jsonQuery.query();
logger.info("Result : {}", result);
}
protected List<Entity> testSingleQuery(int offset, int limit) throws Exception {
2021-09-27 18:00:18 +02:00
ContextTest.setContextByName(DEVVRE);
File queriesDirectory = getQueriesDirectory();
2023-11-03 17:56:55 +01:00
File jsonQueryFile = new File(queriesDirectory, "query1.json");
2021-09-27 18:00:18 +02:00
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
2021-09-27 18:00:18 +02:00
logger.info("Going to test the following JSON query {}", jsonNode.toString());
2021-10-14 15:01:22 +02:00
JsonQuery jsonQuery = new JsonQuery();
jsonQuery.setJsonQuery(jsonNode);
String res = jsonQuery.query();
2021-09-27 18:00:18 +02:00
logger.info(res);
2023-09-27 15:16:58 +02:00
List<Entity> ret = ElementMapper.unmarshalList(Entity.class, res);
return ret;
2021-09-27 18:00:18 +02:00
}
2023-09-27 15:16:58 +02:00
@Test
public void testLimitOffset() throws Exception {
int limit = 2;
List<Entity> entities = testSingleQuery(0, limit);
if(entities.size()==0) {
return;
}
Assert.assertTrue(entities.size() <= limit);
if(entities.size()< limit) {
return;
}
Set<UUID> uuids = new HashSet<>();
for(Entity entity : entities) {
UUID uuid = entity.getID();
uuids.add(uuid);
logger.info("Found {} with UUID {}", Entity.NAME, uuid);
}
entities = testSingleQuery(limit, limit);
if(entities.size()>0) {
Assert.assertTrue(entities.size() <= limit);
for(Entity entity : entities) {
UUID uuid = entity.getID();
Assert.assertFalse(uuids.contains(uuid));
uuids.add(uuid);
logger.info("Found {} with UUID {}", Entity.NAME, uuid);
}
if(entities.size()<limit) {
return;
}
int doubleLimit = limit*2;
entities = testSingleQuery(0, doubleLimit);
Assert.assertTrue(entities.size() <= doubleLimit);
for(Entity entity : entities) {
UUID uuid = entity.getID();
logger.info("Checking if {} with UUID {} was contained in the previous queries", Entity.NAME, uuid);
Assert.assertTrue(uuids.contains(uuid));
logger.info("As expected got {} with UUID {} and name {}", Entity.NAME, uuid);
}
}
entities = testSingleQuery(0, -1);
Assert.assertTrue(entities.size()>=uuids.size());
for(Entity entity : entities) {
UUID uuid = entity.getID();
logger.info("No limit listing: Got {} with UUID {}", Entity.NAME, uuid);
}
}
2023-11-17 17:21:45 +01:00
@Test
public void testSingleProjectionQuery() throws Exception {
2023-11-20 17:34:48 +01:00
File queriesDirectory = getProjectionQueriesDirectory();
2023-11-24 14:03:06 +01:00
File jsonQueryFile = new File(queriesDirectory, "HostingNode-query.json");
2023-11-17 17:21:45 +01:00
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery();
jsonQuery.setJsonQuery(jsonNode);
StringBuffer createdStringBuffer = jsonQuery.createMatchQuery();
logger.info("Created Query from JSON:\n{}", createdStringBuffer.toString());
2023-11-24 15:36:43 +01:00
StringBuffer expectedStringBuffer = new StringBuffer();
File expectedQueryFile = new File(queriesDirectory, jsonQueryFile.getName().replace("-query.json", ".match.oquery"));
try(BufferedReader br = new BufferedReader(new FileReader(expectedQueryFile))) {
for(String line; (line = br.readLine()) != null; ) {
expectedStringBuffer.append(line);
}
}
logger.info("Expected Query from JSON: {}", expectedStringBuffer.toString());
2023-11-27 17:44:15 +01:00
Assert.assertTrue(compareQueries(createdStringBuffer, expectedStringBuffer));
2023-11-24 15:36:43 +01:00
2023-11-29 14:26:02 +01:00
String result = jsonQuery.query();
logger.info("Result : {}", result);
2023-11-17 17:21:45 +01:00
}
2023-11-24 15:36:43 +01:00
@Test
public void testProjectionQueries() throws Exception {
2023-11-24 15:36:43 +01:00
File queriesDirectory = getProjectionQueriesDirectory();
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("-query.json");
}
};
for(File jsonQueryFile : queriesDirectory.listFiles(filenameFilter)) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryFile);
logger.info("Going to test the following JSON query {}", jsonNode.toString());
JsonQuery jsonQuery = new JsonQuery();
jsonQuery.setJsonQuery(jsonNode);
StringBuffer createdStringBuffer = jsonQuery.createMatchQuery();
logger.info("Created Query from JSON:\n{}", createdStringBuffer.toString());
StringBuffer expectedStringBuffer = new StringBuffer();
File expectedQueryFile = new File(queriesDirectory, jsonQueryFile.getName().replace("-query.json", ".match.oquery"));
try(BufferedReader br = new BufferedReader(new FileReader(expectedQueryFile))) {
for(String line; (line = br.readLine()) != null; ) {
expectedStringBuffer.append(line);
}
}
logger.info("Expected Query from JSON: {}", expectedStringBuffer.toString());
2023-11-27 17:44:15 +01:00
Assert.assertTrue(compareQueries(createdStringBuffer, expectedStringBuffer));
2023-11-24 15:36:43 +01:00
2023-11-29 14:26:02 +01:00
String result = jsonQuery.query();
logger.info("Result : {}", result);
2023-11-24 15:36:43 +01:00
}
}
2021-09-27 18:00:18 +02:00
}