package org.gcube.application.geoportal.service; import com.fasterxml.jackson.core.JsonProcessingException; import org.bson.Document; import org.gcube.application.cms.serialization.Serialization; import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel; import org.gcube.application.geoportal.common.model.JSONPathWrapper; import org.gcube.application.geoportal.common.model.document.ProfiledDocument; import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet; import org.gcube.application.geoportal.common.model.rest.Configuration; import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest; import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.common.utils.FileSets; import org.gcube.application.geoportal.common.utils.StorageUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.File; import java.util.Collections; import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class ProfiledDocumentsTests extends BasicServiceTestUnit{ String testProfileId="profiledConcessioni"; @Before public void setContext(){ TokenSetter.set(scope); } protected WebTarget baseTarget(){ return target(InterfaceConstants.Methods.PROJECTS).path(testProfileId); } // GET @Test public void testMissingProfile(){ Response resp = target(InterfaceConstants.Methods.PROJECTS) .path("non-existent-profile").request().get(); assertEquals(404,resp.getStatus()); } @Test public void getAll() { System.out.println(baseTarget().request(MediaType.APPLICATION_JSON).get(List.class)); } @Test public void getByID(){ baseTarget().request(MediaType.APPLICATION_JSON).get(List.class).forEach(d ->{ try { check(baseTarget().path((Serialization.convert(d,ProfiledDocument.class)).get_id()) .request(MediaType.APPLICATION_JSON).get(),ProfiledDocument.class); } catch (Exception e) { e.printStackTrace(System.err); Assert.fail(e.getMessage()); } }); } @Test public void getConfiguration() { System.out.println(baseTarget().path(InterfaceConstants.Methods.CONFIGURATION_PATH).request(MediaType.APPLICATION_JSON).get(Configuration.class)); } // 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))); } // CREATE / edit / delete @Test public void registerNew() throws Exception { createNew(); } @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()); } @Test public void testAddSingleFileSet() throws Exception { ProfiledDocument doc = createNew(); // Try set releazione scavo String fileSetPath="relazioneScavo"; String fieldPath="relazioneScavo"; String filename = "relazione.pdf"; // INSERT ONE, MERGE INFO (NB default values already exist) doc = upload( new StorageUtils(), doc.get_id(), fileSetPath, fieldPath, Document.parse("{\"titolo\" : \"mio titolo\",\"some\" : \"something\" }"), RegisterFileSetRequest.ClashOptions.MERGE_EXISTING, filename); assertTrue("Relazione exists",doc.getTheDocument().containsKey(fileSetPath)); RegisteredFileSet set = Serialization.convert(doc.getTheDocument().get(fileSetPath), RegisteredFileSet.class); 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")); } @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")); } // @@@@@@@@@@@@@@@ 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); } private ProfiledDocument upload(StorageUtils storage, String id, String path, String fieldDefinitionPath, Document attributes, RegisterFileSetRequest.ClashOptions clashPolicy, String ...files) throws Exception { FileSets.RequestBuilder builder = FileSets.build(path); builder.setFieldDescriptionPath(fieldDefinitionPath).setClashPolicy(clashPolicy).setAttributes(attributes); for(String file:files) builder.add(storage.putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),file),file)); 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); } }