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-02-01 15:24:39 +01:00
|
|
|
import org.gcube.application.cms.serialization.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-02-01 15:24:39 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.JSONPathWrapper;
|
2022-01-17 13:30:21 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
|
2022-02-14 12:23:13 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet;
|
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;
|
2022-01-31 13:09:54 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
|
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-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;
|
|
|
|
|
|
|
|
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-31 17:56:47 +01:00
|
|
|
import static org.junit.Assert.assertTrue;
|
2021-12-07 11:16:26 +01:00
|
|
|
|
2022-01-17 18:19:40 +01:00
|
|
|
public class ProfiledDocumentsTests extends BasicServiceTestUnit{
|
2022-02-01 15:24:39 +01: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(){
|
2022-02-01 15:24:39 +01:00
|
|
|
return target(InterfaceConstants.Methods.PROJECTS).path(testProfileId);
|
2022-01-18 14:25:49 +01:00
|
|
|
}
|
|
|
|
// 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
|
2022-02-01 15:24:39 +01:00
|
|
|
public void testAddSingleFileSet() throws Exception {
|
2022-01-18 14:25:49 +01:00
|
|
|
|
|
|
|
ProfiledDocument doc = createNew();
|
2022-01-31 17:56:47 +01:00
|
|
|
// Try set releazione scavo
|
|
|
|
String fileSetPath="relazioneScavo";
|
|
|
|
String fieldPath="relazioneScavo";
|
|
|
|
String filename = "relazione.pdf";
|
|
|
|
|
2022-02-01 15:24:39 +01:00
|
|
|
// INSERT ONE, MERGE INFO (NB default values already exist)
|
2022-01-27 15:02:53 +01:00
|
|
|
doc = upload(
|
|
|
|
new StorageUtils(),
|
|
|
|
doc.get_id(),
|
2022-01-31 17:56:47 +01:00
|
|
|
fileSetPath,
|
|
|
|
fieldPath,
|
2022-02-01 15:24:39 +01:00
|
|
|
Document.parse("{\"titolo\" : \"mio titolo\",\"some\" : \"something\" }"),
|
2022-01-31 13:09:54 +01:00
|
|
|
RegisterFileSetRequest.ClashOptions.MERGE_EXISTING,
|
2022-01-31 17:56:47 +01:00
|
|
|
filename);
|
|
|
|
assertTrue("Relazione exists",doc.getTheDocument().containsKey(fileSetPath));
|
|
|
|
RegisteredFileSet set = Serialization.convert(doc.getTheDocument().get(fileSetPath), RegisteredFileSet.class);
|
2022-02-01 15:24:39 +01:00
|
|
|
assertTrue("Correctly merged attributes ",set.getString("titolo").equals("mio titolo"));
|
|
|
|
assertTrue("Correctly merged attributes ",set.getString("some").equals("something"));
|
|
|
|
|
|
|
|
// REPLACE SAME, CHANGE TITLE
|
|
|
|
doc = upload(
|
|
|
|
new StorageUtils(),
|
|
|
|
doc.get_id(),
|
|
|
|
fileSetPath,
|
|
|
|
fieldPath,
|
|
|
|
Document.parse("{\"titolo\" : \"mio altro titolo\"}"),
|
|
|
|
RegisterFileSetRequest.ClashOptions.REPLACE_EXISTING,
|
|
|
|
filename);
|
|
|
|
set = Serialization.convert(doc.getTheDocument().get(fileSetPath), RegisteredFileSet.class);
|
|
|
|
assertTrue("Correctly merged attributes ",set.getString("titolo").equals("mio altro titolo"));
|
|
|
|
assertTrue("Correctly merged attributes ",!set.containsKey("some"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-27 15:02:53 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 15:24:39 +01:00
|
|
|
@Test
|
|
|
|
public void testMutlipleFileSet() throws Exception {
|
|
|
|
|
|
|
|
ProfiledDocument doc = createNew();
|
|
|
|
// ADD 4 IMGS
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
doc = upload(
|
|
|
|
new StorageUtils(),
|
|
|
|
doc.get_id(),
|
|
|
|
"immagini",
|
|
|
|
"imgs",
|
|
|
|
null,
|
|
|
|
RegisterFileSetRequest.ClashOptions.APPEND,
|
|
|
|
"relazione.pdf");
|
|
|
|
}
|
|
|
|
assertTrue(doc.getTheDocument().containsKey("immagini"));
|
|
|
|
assertTrue("Expected 4 imgs registered",Serialization.convert(doc.getTheDocument().get("immagini"), List.class).size()==4);
|
|
|
|
|
|
|
|
|
|
|
|
// Replace img [3]
|
|
|
|
doc = upload(
|
|
|
|
new StorageUtils(),
|
|
|
|
doc.get_id(),
|
|
|
|
"immagini[3]",
|
|
|
|
"imgs",
|
|
|
|
Document.parse("{\"titolo\" : \"mia immagine\"}"),
|
|
|
|
RegisterFileSetRequest.ClashOptions.REPLACE_EXISTING,
|
|
|
|
"relazione.pdf");
|
|
|
|
|
|
|
|
String title = new JSONPathWrapper(doc.getTheDocument().toJson()).
|
|
|
|
getByPath("immagini[3].titolo",String.class).get(0);
|
|
|
|
|
|
|
|
assertTrue("Changed Title",title.equals("mia immagine"));
|
|
|
|
}
|
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-31 13:09:54 +01:00
|
|
|
private ProfiledDocument upload(StorageUtils storage,
|
|
|
|
String id,
|
|
|
|
String path,
|
|
|
|
String fieldDefinitionPath,
|
|
|
|
Document attributes,
|
|
|
|
RegisterFileSetRequest.ClashOptions clashPolicy,
|
|
|
|
String ...files) throws Exception {
|
2022-01-27 15:02:53 +01:00
|
|
|
FileSets.RequestBuilder builder = FileSets.build(path);
|
2022-01-12 18:42:22 +01:00
|
|
|
|
2022-01-31 13:09:54 +01:00
|
|
|
builder.setFieldDescriptionPath(fieldDefinitionPath).setClashPolicy(clashPolicy).setAttributes(attributes);
|
|
|
|
|
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
|
|
|
}
|