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

140 lines
4.6 KiB
Java
Raw Normal View History

2021-09-20 16:47:35 +02:00
package org.gcube.application.geoportal.service;
2022-01-18 14:25:49 +01:00
import com.fasterxml.jackson.core.JsonProcessingException;
2021-09-20 16:47:35 +02:00
import org.bson.Document;
2022-01-17 13:30:21 +01:00
import org.gcube.application.cms.Serialization;
2021-12-07 11:16:26 +01:00
import org.gcube.application.cms.tests.TokenSetter;
2022-01-27 15:02:53 +01:00
import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel;
2022-01-17 13:30:21 +01:00
import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
2022-01-27 15:02:53 +01:00
import org.gcube.application.geoportal.common.model.legacy.Concessione;
2022-01-18 14:25:49 +01:00
import org.gcube.application.geoportal.common.model.rest.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
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.Files;
import org.gcube.application.geoportal.common.utils.StorageUtils;
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-01-27 15:02:53 +01:00
import org.junit.internal.runners.TestMethod;
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;
2021-12-07 11:16:26 +01:00
import javax.ws.rs.core.Response;
2022-01-27 15:02:53 +01:00
import java.io.File;
2021-09-20 16:47:35 +02:00
import java.util.Collections;
import java.util.List;
2021-12-07 11:16:26 +01:00
import static org.junit.Assert.assertEquals;
2022-01-17 18:19:40 +01:00
public class ProfiledDocumentsTests extends BasicServiceTestUnit{
2021-09-20 16:47:35 +02:00
2022-01-12 18:42:22 +01:00
String testProfileId="profiledConcessioni";
2021-12-07 11:16:26 +01:00
@Before
public void setContext(){
TokenSetter.set(scope);
2021-09-20 16:47:35 +02:00
}
2021-12-07 11:16:26 +01:00
2022-01-18 14:25:49 +01:00
protected WebTarget baseTarget(){
return target(InterfaceConstants.Methods.PROJECTS).path(testProfileId);
}
// GET
2021-09-20 16:47:35 +02:00
@Test
2021-12-07 11:16:26 +01:00
public void testMissingProfile(){
2022-01-27 15:02:53 +01:00
Response resp = target(InterfaceConstants.Methods.PROJECTS)
2021-12-07 11:16:26 +01:00
.path("non-existent-profile").request().get();
2022-01-27 15:02:53 +01:00
assertEquals(404,resp.getStatus());
2021-09-20 16:47:35 +02:00
}
2021-12-07 11:16:26 +01:00
2021-09-20 16:47:35 +02:00
@Test
2021-12-07 11:16:26 +01:00
public void getAll() {
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
public void getByID(){
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-01-18 14:25:49 +01:00
check(baseTarget().path((Serialization.convert(d,ProfiledDocument.class)).get_id())
2022-01-17 13:30:21 +01:00
.request(MediaType.APPLICATION_JSON).get(),ProfiledDocument.class);
} catch (Exception e) {
e.printStackTrace(System.err);
Assert.fail(e.getMessage());
}
});
}
2022-01-12 18:42:22 +01:00
@Test
2022-01-18 14:25:49 +01:00
public void getConfiguration() {
2022-01-27 15:02:53 +01:00
System.out.println(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 {
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
@Test
public void registerNew() throws Exception {
createNew();
2022-01-17 18:19:40 +01:00
}
2022-01-18 14:25:49 +01:00
@Test
public void edit() throws Exception {
ProfiledDocument doc=createNew();
String beforeJson=doc.getTheDocument().toJson();
doc.getTheDocument().put("someStrangeField","someOtherRandomValue");
String edited=doc.getTheDocument().toJson();
Assert.assertNotEquals(beforeJson,doc.getTheDocument().toJson());
doc=check(baseTarget().path(doc.get_id()).request(MediaType.APPLICATION_JSON).
put(Entity.entity(doc.getTheDocument(), MediaType.APPLICATION_JSON)),ProfiledDocument.class);
Assert.assertEquals(edited,doc.getTheDocument().toJson());
}
2022-01-17 18:19:40 +01:00
@Test
public void addFileSet() throws Exception {
2022-01-18 14:25:49 +01:00
ProfiledDocument doc = createNew();
2022-01-27 15:02:53 +01:00
doc = upload(
new StorageUtils(),
doc.get_id(),
"relazioneScavo",
"relazioneScavo",
Document.parse("{\"titolo\" : \"mio titolo\"}"),
"relazione.pdf");
System.out.println(Serialization.write(doc));
}
2022-01-17 18:19:40 +01:00
2022-01-27 15:02:53 +01:00
// @@@@@@@@@@@@@@@ Routines
private ProfiledDocument createNew() throws Exception {
Document document =new Document(Collections.singletonMap("dumbKey","dumbValue"));
return check(baseTarget().request(MediaType.APPLICATION_JSON).
post(Entity.entity(document, MediaType.APPLICATION_JSON)),ProfiledDocument.class);
2022-01-12 18:42:22 +01:00
}
2022-01-27 15:02:53 +01:00
private ProfiledDocument upload(StorageUtils storage, String id, String path, String fieldPath, Document attributes, String ...files) throws Exception {
FileSets.RequestBuilder builder = FileSets.build(path);
2022-01-12 18:42:22 +01:00
2022-01-27 15:02:53 +01:00
for(String file:files)
builder.add(storage.putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),file),file));
2022-01-12 18:42:22 +01:00
2022-01-27 15:02:53 +01:00
return check(baseTarget().path(InterfaceConstants.Methods.REGISTER_FILES_PATH).path(id).request(MediaType.APPLICATION_JSON).
post(Entity.entity(Serialization.write(builder.getTheRequest()),
MediaType.APPLICATION_JSON)),ProfiledDocument.class);
}
2021-09-20 16:47:35 +02:00
}