Fixed test

This commit is contained in:
Luca Frosini 2022-02-08 13:36:22 +01:00
parent 34fe3af013
commit a6153e8f1a
2 changed files with 242 additions and 85 deletions

View File

@ -225,6 +225,7 @@ public class QueryTemplateManagement extends EntityElementManagement<QueryTempla
@Override
protected void reallyDelete() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to delete {} with name {}", accessType.getName(), name);
getElement().delete();
}

View File

@ -1,33 +1,237 @@
package org.gcube.informationsystem.resourceregistry.queries.templates;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.gcube.com.fasterxml.jackson.databind.JavaType;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.queries.templates.impl.entities.QueryTemplateImpl;
import org.gcube.informationsystem.queries.templates.impl.properties.TemplateVariableImpl;
import org.gcube.informationsystem.queries.templates.reference.entities.QueryTemplate;
import org.gcube.informationsystem.queries.templates.reference.properties.TemplateVariable;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.queries.JsonQueryTest;
import org.gcube.informationsystem.utils.ElementMapper;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryTemplateManagementTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(QueryTemplateManagementTest.class);
public static final String QUERY_TEMPLATE_NAME = "Test";
public static final String QUERY_TEMPLATE_DESCRIPTION = "A Test Query Template";
public static final String QUERY_TEMPLATE_DESCRIPTION_UPDATED = "A Test Query Template UPDATED";
public static final String STATE_VARIABLE_NAME = "$state";
public static final String NAME_VARIABLE_NAME = "$name";
public static final String GROUP_VARIABLE_NAME = "$group";
public static String getUser() {
String user = Header.UNKNOWN_USER;
try {
String token = SecurityTokenProvider.instance.get();
if(token != null) {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
if(authorizationEntry != null) {
ClientInfo clientInfo = authorizationEntry.getClientInfo();
String clientId = clientInfo.getId();
if(clientId != null && clientId.compareTo("") != 0) {
user = clientId;
} else {
throw new Exception("Username null or empty");
}
}
}
} catch(Exception e) {
logger.error("Unable to retrieve user. {} will be used", user);
}
return user;
}
public static void checkHeader(IdentifiableElement previous, IdentifiableElement got) {
Header gotHeader = got.getHeader();
Header previousHeader = previous.getHeader();
Assert.assertTrue(gotHeader != null);
Assert.assertTrue(gotHeader.getUUID() != null);
String user = getUser();
Assert.assertTrue(gotHeader.getLastUpdateBy().compareTo(user) == 0);
if(previousHeader != null) {
Assert.assertTrue(gotHeader.getUUID().compareTo(previousHeader.getUUID()) == 0);
Assert.assertTrue(gotHeader.getCreatedBy().compareTo(user) == 0);
Assert.assertTrue(gotHeader.getCreatedBy().compareTo(previousHeader.getCreatedBy()) == 0);
Assert.assertTrue(gotHeader.getCreationTime().compareTo(previousHeader.getCreationTime()) == 0);
}
Assert.assertFalse(gotHeader.getCreationTime().after(gotHeader.getLastUpdateTime()));
}
protected void assertions(QueryTemplate expected, QueryTemplate got) {
checkHeader(expected, got);
Assert.assertTrue(expected.getName().compareTo(got.getName()) == 0);
Assert.assertTrue(expected.getDescription().compareTo(got.getDescription()) == 0);
// Template and TemplateVariable should be compared
}
protected QueryTemplate create(QueryTemplate queryTemplate) throws ResourceRegistryException, IOException {
list();
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
String json = ElementMapper.marshal(queryTemplate);
queryTemplateManagement.setJson(json);
String created = queryTemplateManagement.create();
logger.info("Created {} is {}", QueryTemplate.NAME, created);
QueryTemplate createdQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, created);
assertions(queryTemplate, createdQueryTemplate);
list(createdQueryTemplate);
return createdQueryTemplate;
}
protected QueryTemplate read(QueryTemplate queryTemplate) throws ResourceRegistryException, IOException {
list(queryTemplate);
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
String read = queryTemplateManagement.read();
logger.info("Read {} is {}", QueryTemplate.NAME, read);
QueryTemplate readQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, read);
assertions(queryTemplate, readQueryTemplate);
return queryTemplate;
}
protected void run() throws Exception {
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
queryTemplateManagement.run();
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode params = objectMapper.createObjectNode();
params.put(STATE_VARIABLE_NAME, "running");
params.put(GROUP_VARIABLE_NAME, "DataAccess");
params.put(NAME_VARIABLE_NAME, "StorageHub");
queryTemplateManagement.setParams(params);
queryTemplateManagement.run();
}
protected QueryTemplate update(QueryTemplate queryTemplate) throws ResourceRegistryException, IOException {
list(queryTemplate);
queryTemplate.setDescription(QUERY_TEMPLATE_DESCRIPTION_UPDATED);
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
String json = ElementMapper.marshal(queryTemplate);
queryTemplateManagement.setJson(json);
String updated = queryTemplateManagement.update();
logger.info("Updated {} is {}", QueryTemplate.NAME, updated);
QueryTemplate updatedQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, updated);
assertions(queryTemplate, updatedQueryTemplate);
list(updatedQueryTemplate);
// Restoring Previous description on queryTemplate
queryTemplate.setDescription(QUERY_TEMPLATE_DESCRIPTION);
return updatedQueryTemplate;
}
protected QueryTemplate invalidUpdate(QueryTemplate queryTemplate) throws ResourceRegistryException, Exception {
list(queryTemplate);
queryTemplate.setName("AnotherName");
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
String json = ElementMapper.marshal(queryTemplate);
queryTemplateManagement.setJson(json);
try {
queryTemplateManagement.update();
throw new Exception("It should not be possibile to update the " + QueryTemplate.NAME + " with json name different from the name used to identify the instance");
}catch (ResourceRegistryException e) {
// OK
logger.debug("As expected {}", e.getMessage());
// Restoring Previous name on queryTemplate
queryTemplate.setName(QUERY_TEMPLATE_NAME);
}
list(queryTemplate);
return queryTemplate;
}
protected boolean delete(QueryTemplate queryTemplate) throws ResourceRegistryException {
list(queryTemplate);
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
queryTemplateManagement.delete();
logger.debug("Deleted {} with name {}", QueryTemplate.NAME, queryTemplate.getName());
list();
return true;
}
protected void list(QueryTemplate...queryTemplates) throws ResourceRegistryException {
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
String allString = queryTemplateManagement.all(false);
JavaType type = ElementMapper.getObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, QueryTemplate.class);
List<QueryTemplate> all = null;
try {
all = ElementMapper.getObjectMapper().readValue(allString, type);
}catch (Exception e) {
throw new RuntimeException(e);
}
if(queryTemplates==null) {
Assert.assertTrue(all.size()==0);
return;
}
Assert.assertTrue(all.size()==queryTemplates.length);
for(QueryTemplate expected : queryTemplates) {
String expectedName = expected.getName();
boolean found = false;
for(QueryTemplate got : all) {
String gotName = got.getName();
if(gotName.compareTo(expectedName)==0) {
found = true;
assertions(expected, got);
break;
}
}
Assert.assertTrue(found);
}
}
protected void invalidCreate(QueryTemplate queryTemplate) throws ResourceRegistryException, IOException {
try {
QueryTemplate recreatedQueryTemplate = create(queryTemplate);
throw new RuntimeException(ElementMapper.marshal(recreatedQueryTemplate) + " was created successfully. This is not what we expected");
} catch(AlreadyPresentException e) {
logger.debug("As expected {} cannot be created.", ElementMapper.marshal(queryTemplate));
}
}
public File getQueryTemplatesDirectory() throws Exception {
URL logbackFileURL = JsonQueryTest.class.getClassLoader().getResource("logback-test.xml");
URL logbackFileURL = ContextTest.class.getClassLoader().getResource("logback-test.xml");
File logbackFile = new File(logbackFileURL.toURI());
File resourcesDirectory = logbackFile.getParentFile();
return new File(resourcesDirectory, "queryTemplates");
@ -36,7 +240,7 @@ public class QueryTemplateManagementTest extends ContextTest {
protected QueryTemplate getQueryTemplate(String name, JsonNode jsonNode) {
QueryTemplate queryTemplate = new QueryTemplateImpl();
queryTemplate.setName(name);
queryTemplate.setDescription("A Test Query Template");
queryTemplate.setDescription(QUERY_TEMPLATE_DESCRIPTION);
queryTemplate.setTemplate(jsonNode);
TemplateVariable stateTemplateVariable = new TemplateVariableImpl();
@ -60,105 +264,57 @@ public class QueryTemplateManagementTest extends ContextTest {
return queryTemplate;
}
@Test
public void testMarshallingUnMarshalling() throws Exception {
protected JsonNode getTemplate() throws Exception {
File queryTemplatesDirectory = getQueryTemplatesDirectory();
File jsonQueryTemplateFile = new File(queryTemplatesDirectory, "queryTemplate" + 1 + ".json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryTemplateFile);
String name = "Test";
QueryTemplate queryTemplate = getQueryTemplate(name, jsonNode);
String json = ElementMapper.marshal(queryTemplate);
logger.info("Marshalled {} - {}", QueryTemplate.NAME, ElementMapper.marshal(queryTemplate));
QueryTemplate gotQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, json);
logger.info("Marshalled/UnMarshalled/Marshalled {} - {}", QueryTemplate.NAME, ElementMapper.marshal(gotQueryTemplate));
return jsonNode;
}
protected QueryTemplate getQueryTemplate() throws Exception {
JsonNode jsonNode = getTemplate();
QueryTemplate queryTemplate = getQueryTemplate(QUERY_TEMPLATE_NAME, jsonNode);
String json = ElementMapper.marshal(queryTemplate);
logger.info("Marshalled {} - {}", QueryTemplate.NAME, json);
return queryTemplate;
}
// @Test
public void readTest() throws Exception {
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
String read = queryTemplateManagement.read();
logger.info("Read {} is {}", QueryTemplate.NAME, read);
QueryTemplate readQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, read);
String json = ElementMapper.marshal(readQueryTemplate);
logger.info("Read {}: {}", QueryTemplate.NAME, json);
}
@Test
public void completeTest() throws Exception {
File queryTemplatesDirectory = getQueryTemplatesDirectory();
File jsonQueryTemplateFile = new File(queryTemplatesDirectory, "queryTemplate" + 1 + ".json");
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonQueryTemplateFile);
String name = "Test";
QueryTemplate queryTemplate = getQueryTemplate(name, jsonNode);
String json = ElementMapper.marshal(queryTemplate);
QueryTemplate gotQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, json);
logger.info("Going to create {} - {}", QueryTemplate.NAME, ElementMapper.marshal(gotQueryTemplate));
JsonNode gotTemplate = gotQueryTemplate.getTemplate();
logger.info("Template is {}", gotTemplate.toString());
logger.info("Query template is {}.\nQuery with default values is {}", gotTemplate.toString(), queryTemplate.getJsonQuery().toString());
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(name);
queryTemplateManagement.setJson(json);
try {
String created = queryTemplateManagement.create();
logger.info("Created {} is {}", QueryTemplate.NAME, created);
QueryTemplate queryTemplate = getQueryTemplate();
QueryTemplate createdQueryTemplate = create(queryTemplate);
queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName("Test");
String read = queryTemplateManagement.read();
QueryTemplate readQueryTemplate = read(createdQueryTemplate);
QueryTemplate readQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, read);
logger.info("Read {} is {}", QueryTemplate.NAME, ElementMapper.marshal(readQueryTemplate));
gotTemplate = readQueryTemplate.getTemplate();
logger.info("Read Template is {}", gotTemplate.toString());
QueryTemplate updatedQueryTemplate = update(readQueryTemplate);
try {
readQueryTemplate.setName("AnotheName");
queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(name);
queryTemplateManagement.setJson(ElementMapper.marshal(readQueryTemplate));
queryTemplateManagement.update();
throw new Exception("It should not be possibile to update the " + QueryTemplate.NAME + " with json name different from the name used to identify the instance");
}catch (ResourceRegistryException e) {
// OK
logger.debug("{}", e);
}
updatedQueryTemplate = invalidUpdate(updatedQueryTemplate);
// Restoring correct name
readQueryTemplate.setName(name);
run();
QueryTemplate readUpdatedQueryTemplate = read(updatedQueryTemplate);
jsonQueryTemplateFile = new File(queryTemplatesDirectory, "queryTemplate" + 1 + "-bis.json");
jsonNode = objectMapper.readTree(jsonQueryTemplateFile);
readQueryTemplate.setTemplate(jsonNode);
queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(name);
queryTemplateManagement.setJson(ElementMapper.marshal(readQueryTemplate));
String updatedString = queryTemplateManagement.update();
QueryTemplate updatedQueryTemplate = ElementMapper.unmarshal(QueryTemplate.class, updatedString);
logger.info("Updated {} is {}", QueryTemplate.NAME, ElementMapper.marshal(updatedQueryTemplate));
gotTemplate = updatedQueryTemplate.getTemplate();
logger.info("Updated Template is {}", gotTemplate.toString());
queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(name);
ObjectNode params = objectMapper.createObjectNode();
params.put(STATE_VARIABLE_NAME, "running");
params.put(GROUP_VARIABLE_NAME, "DataAccess");
params.put(NAME_VARIABLE_NAME, "StorageHub");
queryTemplateManagement.setParams(params);
queryTemplateManagement.run();
}finally {
delete(readUpdatedQueryTemplate);
}catch (Throwable e) {
QueryTemplateManagement queryTemplateManagement = new QueryTemplateManagement();
queryTemplateManagement.setName(QUERY_TEMPLATE_NAME);
queryTemplateManagement.delete();
}
logger.debug("The DB should be now clean");
}
}