gcube-cms-suite/geoportal-service/src/test/java/org/gcube/application/geoportal/service/profiledDocuments/AbstractProfiledDocumentsTe...

195 lines
7.1 KiB
Java
Raw Normal View History

2022-02-24 18:09:30 +01:00
package org.gcube.application.geoportal.service.profiledDocuments;
2021-09-20 16:47:35 +02:00
2022-01-18 14:25:49 +01:00
import com.fasterxml.jackson.core.JsonProcessingException;
2022-08-31 18:06:19 +02:00
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
2021-09-20 16:47:35 +02:00
import org.bson.Document;
2022-02-01 15:24:39 +01:00
import org.gcube.application.cms.serialization.Serialization;
2022-09-26 16:11:20 +02:00
import org.gcube.application.cms.tests.Tests;
2021-12-07 11:16:26 +01:00
import org.gcube.application.cms.tests.TokenSetter;
2022-04-01 19:11:11 +02:00
import org.gcube.application.cms.tests.model.BasicTests;
2022-09-26 16:11:20 +02:00
import org.gcube.application.geoportal.common.model.configuration.Configuration;
2022-03-04 14:23:20 +01:00
import org.gcube.application.geoportal.common.model.document.Project;
2023-01-10 11:42:09 +01:00
import org.gcube.application.geoportal.common.model.document.access.Access;
2022-02-16 17:12:54 +01:00
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
2022-01-18 14:25:49 +01:00
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
2022-01-31 13:09:54 +01:00
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
2022-02-14 17:06:32 +01:00
import org.gcube.application.geoportal.common.model.rest.StepExecutionRequest;
2021-09-20 16:47:35 +02:00
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
2022-01-27 15:02:53 +01:00
import org.gcube.application.geoportal.common.utils.FileSets;
import org.gcube.application.geoportal.common.utils.StorageUtils;
2022-02-24 18:09:30 +01:00
import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
import org.gcube.application.geoportal.service.BasicServiceTestUnit;
2022-01-17 13:30:21 +01:00
import org.junit.Assert;
2021-12-07 11:16:26 +01:00
import org.junit.Before;
2021-09-20 16:47:35 +02:00
import org.junit.Test;
2022-10-25 16:57:41 +02:00
import org.opengis.service.Interface;
2021-09-20 16:47:35 +02:00
2023-01-10 15:57:40 +01:00
import javax.ws.rs.WebApplicationException;
2021-09-20 16:47:35 +02:00
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
2022-01-27 15:02:53 +01:00
import java.io.File;
2021-09-20 16:47:35 +02:00
import java.util.List;
2023-01-10 11:42:09 +01:00
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
2022-02-24 18:09:30 +01:00
import static org.junit.Assume.assumeTrue;
2021-12-07 11:16:26 +01:00
2022-02-24 18:09:30 +01:00
public abstract class AbstractProfiledDocumentsTests extends BasicServiceTestUnit {
2022-02-01 15:24:39 +01:00
2021-12-07 11:16:26 +01:00
@Before
public void setContext(){
2022-02-24 18:09:30 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
TokenSetter.set(GCubeTest.getContext());
2021-09-20 16:47:35 +02:00
}
2021-12-07 11:16:26 +01:00
2022-01-18 14:25:49 +01:00
2022-08-31 18:06:19 +02:00
protected WebTarget baseTarget(){
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(Serialization.mapper);
WebTarget toReturn = target(InterfaceConstants.Methods.PROJECTS).register(provider);
return toReturn;
};
2022-03-11 16:42:33 +01:00
2021-12-07 11:16:26 +01:00
2022-05-03 18:07:18 +02:00
2021-09-20 16:47:35 +02:00
@Test
2021-12-07 11:16:26 +01:00
public void getAll() {
2022-02-24 18:09:30 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
2022-01-18 14:25:49 +01:00
System.out.println(baseTarget().request(MediaType.APPLICATION_JSON).get(List.class));
2021-09-20 16:47:35 +02:00
}
2022-01-12 18:42:22 +01:00
2022-01-17 13:30:21 +01:00
@Test
2022-10-25 16:57:41 +02:00
public void scanAllById(){
2022-02-24 18:09:30 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
2022-01-18 14:25:49 +01:00
baseTarget().request(MediaType.APPLICATION_JSON).get(List.class).forEach(d ->{
2022-01-17 13:30:21 +01:00
try {
2022-10-25 16:57:41 +02:00
getById((Serialization.convert(d, Project.class)).getId());
2022-01-17 13:30:21 +01:00
} catch (Exception e) {
e.printStackTrace(System.err);
Assert.fail(e.getMessage());
}
});
}
2022-01-12 18:42:22 +01:00
@Test
2022-03-11 16:42:33 +01:00
public void getConfiguration() throws Exception {
2022-02-24 18:09:30 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
2022-03-11 16:42:33 +01:00
System.out.println(check(
baseTarget().path(InterfaceConstants.Methods.CONFIGURATION_PATH).request(MediaType.APPLICATION_JSON).get(),Configuration.class));
2022-01-18 14:25:49 +01:00
}
2022-01-12 18:42:22 +01:00
2022-01-18 14:25:49 +01:00
// Queries
@Test
public void query() throws JsonProcessingException {
2022-02-24 18:09:30 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
2022-01-18 14:25:49 +01:00
System.out.println(baseTarget().path(InterfaceConstants.Methods.QUERY_PATH).
request(MediaType.APPLICATION_JSON).
post(Entity.entity(Serialization.write(new QueryRequest()),MediaType.APPLICATION_JSON)));
}
2022-01-17 18:19:40 +01:00
2022-01-18 14:25:49 +01:00
// CREATE / edit / delete
2022-02-01 15:24:39 +01:00
2022-02-16 17:12:54 +01:00
// @@@@@@@@@@@@@@@ Routines
2022-02-01 15:24:39 +01:00
2022-03-04 14:23:20 +01:00
protected Project createNew(Document content) throws Exception {
2022-08-31 18:06:19 +02:00
2022-03-04 14:23:20 +01:00
Project doc =check(baseTarget().request(MediaType.APPLICATION_JSON).
post(Entity.entity(content, MediaType.APPLICATION_JSON)), Project.class);
2022-04-01 19:11:11 +02:00
BasicTests.validate(doc);
2022-03-30 18:39:10 +02:00
assertTrue(doc.getLifecycleInformation().getPhase().equals(LifecycleInformation.CommonPhases.DRAFT_PHASE));
2022-02-17 16:01:39 +01:00
return doc;
2022-01-27 15:02:53 +01:00
}
2022-10-25 16:57:41 +02:00
protected Project getById(String id ) throws Exception {
Project doc =check(baseTarget().path(id).request(MediaType.APPLICATION_JSON).get(), Project.class);
BasicTests.validate(doc);
assertTrue(doc.getLifecycleInformation().getPhase().equals(LifecycleInformation.CommonPhases.DRAFT_PHASE));
return doc;
}
protected Project setRelation(Project current, String targetId, String targetUCD, String relationId) throws Exception {
Project doc =check(baseTarget().path(InterfaceConstants.Methods.RELATIONSHIP).
path(current.getId()).path(relationId).
queryParam(InterfaceConstants.Parameters.TARGET_ID,targetId).
queryParam(InterfaceConstants.Parameters.TARGET_UCD,targetUCD).
request(MediaType.APPLICATION_JSON).
put(Entity.json("")), Project.class);
BasicTests.validate(doc);
return doc;
}
protected Project deleteRelation(Project current, String targetId, String targetUCD, String relationId) throws Exception {
Project doc =check(baseTarget().path(InterfaceConstants.Methods.RELATIONSHIP).
path(current.getId()).path(relationId).
queryParam(InterfaceConstants.Parameters.TARGET_ID,targetId).
queryParam(InterfaceConstants.Parameters.TARGET_UCD,targetUCD).
request(MediaType.APPLICATION_JSON).
delete(), Project.class);
BasicTests.validate(doc);
return doc;
}
2022-05-17 11:43:50 +02:00
protected void delete(String id) throws Exception {
2022-05-31 16:10:41 +02:00
delete(id,false);
}
protected void delete(String id,Boolean force) throws Exception {
check(baseTarget().path(id).queryParam("force",force).request(MediaType.APPLICATION_JSON).
2022-05-17 11:43:50 +02:00
delete(), null);
}
2022-05-31 16:10:41 +02:00
2022-02-17 16:01:39 +01:00
2022-03-04 14:23:20 +01:00
protected Project update(String id, Document newContent)throws Exception {
Project doc = check(baseTarget().path(id).request(MediaType.APPLICATION_JSON).
put(Entity.entity(newContent, MediaType.APPLICATION_JSON)), Project.class);
2022-04-01 19:11:11 +02:00
BasicTests.validate(doc);
2022-02-17 16:01:39 +01:00
return doc;
2022-01-12 18:42:22 +01:00
}
2022-03-04 14:23:20 +01:00
protected Project upload(StorageUtils storage,
String id,
String parentPath,
String fieldName,
String fieldDefinitionPath,
Document attributes,
RegisterFileSetRequest.ClashOptions clashPolicy,
2023-01-10 11:42:09 +01:00
Access access,
String ...files) throws Exception {
2022-02-16 17:12:54 +01:00
FileSets.RequestBuilder builder = FileSets.build(parentPath,fieldName,fieldDefinitionPath);
2022-01-12 18:42:22 +01:00
2023-01-10 11:42:09 +01:00
builder.setAccess(access);
2022-02-16 17:12:54 +01:00
builder.setClashPolicy(clashPolicy).setAttributes(attributes);
2022-01-31 13:09:54 +01:00
2022-01-27 15:02:53 +01:00
for(String file:files)
2022-09-26 16:11:20 +02:00
builder.add(storage.putOntoStorage(new File(Tests.BASE_FOLDER,file),file));
2022-01-12 18:42:22 +01:00
2022-03-04 14:23:20 +01:00
Project doc = check(baseTarget().path(InterfaceConstants.Methods.REGISTER_FILES_PATH).path(id).request(MediaType.APPLICATION_JSON).
2022-01-27 15:02:53 +01:00
post(Entity.entity(Serialization.write(builder.getTheRequest()),
2022-03-04 14:23:20 +01:00
MediaType.APPLICATION_JSON)), Project.class);
2023-01-10 11:42:09 +01:00
2022-04-01 19:11:11 +02:00
BasicTests.validate(doc);
2022-02-17 16:01:39 +01:00
return doc;
2022-01-27 15:02:53 +01:00
}
2022-02-14 17:06:32 +01:00
2022-03-04 14:23:20 +01:00
protected Project step(String id, StepExecutionRequest request) throws Exception {
Project toReturn= check(baseTarget().
2022-02-14 17:06:32 +01:00
path(InterfaceConstants.Methods.STEP).path(id).request(MediaType.APPLICATION_JSON).
post(Entity.entity(Serialization.write(request),
2022-03-04 14:23:20 +01:00
MediaType.APPLICATION_JSON)), Project.class);
2022-04-01 19:11:11 +02:00
BasicTests.validate(toReturn);
2022-02-14 17:06:32 +01:00
assertTrue(toReturn.getLifecycleInformation().getLastInvokedStep().equals(request.getStepID()));
return toReturn;
}
2022-02-16 17:12:54 +01:00
2021-09-20 16:47:35 +02:00
}