package org.gcube.application.geoportal.service; import com.fasterxml.jackson.core.JsonProcessingException; import org.bson.Document; import org.gcube.application.cms.Serialization; import org.gcube.application.cms.tests.TokenSetter; import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel; import org.gcube.application.geoportal.common.model.document.ProfiledDocument; import org.gcube.application.geoportal.common.model.legacy.Concessione; import org.gcube.application.geoportal.common.model.rest.Configuration; import org.gcube.application.geoportal.common.model.rest.QueryRequest; import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.common.utils.FileSets; import org.gcube.application.geoportal.common.utils.Files; import org.gcube.application.geoportal.common.utils.StorageUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.internal.runners.TestMethod; 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; 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 addFileSet() throws Exception { ProfiledDocument doc = createNew(); doc = upload( new StorageUtils(), doc.get_id(), "relazioneScavo", "relazioneScavo", Document.parse("{\"titolo\" : \"mio titolo\"}"), "relazione.pdf"); System.out.println(Serialization.write(doc)); } // @@@@@@@@@@@@@@@ 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 fieldPath, Document attributes, String ...files) throws Exception { FileSets.RequestBuilder builder = FileSets.build(path); 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); } }