Testing serialization

This commit is contained in:
Fabio Sinibaldi 2022-08-31 18:06:19 +02:00
parent a98d362ccf
commit 49094b73ee
8 changed files with 137 additions and 13 deletions

View File

@ -50,7 +50,7 @@ public class ConcessioniLifeCycleManager extends Default3PhaseManager implements
// STATIC ROUTINES
static final Project setDefaults(Project document) throws IOException {
log.info("Concessione {}, setting defaults..",document.getId());
log.info("Concessione ID {}, setting defaults..",document.getId());
log.debug("Full concessione is {}",document);
ProfiledConcessione c=Serialization.convert(document,ProfiledConcessione.class);
Document doc=c.getTheDocument();

View File

@ -12,6 +12,7 @@ import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import static junit.framework.TestCase.*;
@ -28,6 +29,9 @@ public class ConcessioniPluginTests extends BasicPluginTest {
public void testDefaults() throws IOException {
Project c= TestDocuments.documentMap.get("dummy.json");
LocalDateTime start = LocalDateTime.now();
c.getTheDocument().put(ProfiledConcessione.DATA_INZIO_PROGETTO,start);
c.getTheDocument().put(ProfiledConcessione.DATA_FINE_PROGETTO,start);
c.getTheDocument().put(ProfiledConcessione.POSIZIONAMENTO_SCAVO,new Document());
c.getTheDocument().put(ProfiledConcessione.IMMAGINI_RAPPRESENTATIVE,new Document[]{new Document()});
@ -39,6 +43,12 @@ public class ConcessioniPluginTests extends BasicPluginTest {
assertNotNull(c.getTheDocument().get(ProfiledConcessione.SOGGETTO));
assertNotNull(c.getTheDocument().getString(ProfiledConcessione.DESCRIZIONE_CONTENUTO));
assertNotNull(c.getTheDocument().get(ProfiledConcessione.DATA_INZIO_PROGETTO));
assertEquals(Serialization.convert(c.getTheDocument().get(ProfiledConcessione.DATA_INZIO_PROGETTO),LocalDateTime.class),start);
assertNotNull(c.getTheDocument().get(ProfiledConcessione.DATA_FINE_PROGETTO));
assertEquals(Serialization.convert(c.getTheDocument().get(ProfiledConcessione.DATA_FINE_PROGETTO),LocalDateTime.class),start);
assertNotNull(c.getTheDocument().get(ProfiledConcessione.RELAZIONE_SCAVO));
Document rel = Serialization.convert(c.getTheDocument().get(ProfiledConcessione.RELAZIONE_SCAVO), Document.class);
assertNotNull(rel.get(ProfiledConcessione.Sections.TITOLO));

View File

@ -1,6 +1,10 @@
package org.gcube.application.geoportal.clients;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.gcube.application.geoportal.client.DefaultDocumentsClient;
import org.gcube.application.geoportal.client.utils.Serialization;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.configuration.Configuration;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
@ -10,12 +14,15 @@ import org.junit.Test;
import sun.net.www.content.text.Generic;
import java.rmi.RemoteException;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicLong;
import static junit.framework.TestCase.assertTrue;
import static org.gcube.application.geoportal.client.plugins.GeoportalAbstractPlugin.*;
import static org.gcube.application.geoportal.client.utils.Serialization.write;
import static org.junit.Assume.assumeTrue;
@Slf4j
public class ProfiledDocumentsTest<M extends Project,C extends Projects<M>> extends GenericUseCases {
// protected String getProfileID(){return "basic";}
@ -25,6 +32,22 @@ public class ProfiledDocumentsTest<M extends Project,C extends Projects<M>> exte
// }
@Test
public void registerNew() throws RemoteException, JsonProcessingException {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
Projects<M> client = (Projects<M>) getClient();
Document theDoc= Document.parse("{\n" +
"\"posizionamentoScavo\" :{\n" +
"\t\"titolo\" : \"mio titolo\"}}");
theDoc.put("startTime", LocalDateTime.now());
Project p =client.createNew(theDoc);
log.debug("Registered project (AS JSON) : {}", write(p));
}
@Test
public void getConfiguration() throws Exception {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());

View File

@ -0,0 +1,33 @@
package org.gcube.application.geoportal.clients.serialization;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.client.utils.Serialization;
import org.junit.Test;
import java.io.IOException;
import java.time.LocalDateTime;
@Slf4j
public class SerializationTests {
@Test
public void testDates() throws IOException {
LocalDateTime obj = LocalDateTime.now();
roundTrip(obj);
}
public boolean roundTrip(Object obj) throws IOException {
log.info("Round trip for {}",obj);
Class clazz =Object.class;
if(obj!=null) clazz=obj.getClass();
log.debug("Class is {}",obj.getClass());
String json= Serialization.write(obj);
log.debug("JSON String is : {}",json);
Object unmarshalled = Serialization.read(json,clazz);
log.debug("Unmarshalled as {}",unmarshalled);
return obj.equals(unmarshalled);
}
}

View File

@ -37,6 +37,12 @@ public class GeoPortalService extends ResourceConfig{
super();
//Register interrfaces
log.info("Initializing serialization");
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(Serialization.mapper);
register(provider);
registerClasses(ConcessioniOverMongo.class);
registerClasses(ProfiledDocuments.class);
registerClasses(UseCaseDescriptors.class);
@ -65,10 +71,6 @@ public class GeoPortalService extends ResourceConfig{
ImplementationProvider.get().initEngines();
log.info("Initializing serialization");
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(Serialization.mapper);
register(provider);
}

View File

@ -1,6 +1,7 @@
package org.gcube.application.geoportal.service.profiledDocuments;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.bson.Document;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.cms.tests.TokenSetter;
@ -42,7 +43,13 @@ public abstract class AbstractProfiledDocumentsTests extends BasicServiceTestUni
}
protected abstract WebTarget baseTarget();
protected WebTarget baseTarget(){
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(Serialization.mapper);
WebTarget toReturn = target(InterfaceConstants.Methods.PROJECTS).register(provider);
return toReturn;
};
@ -90,6 +97,8 @@ public abstract class AbstractProfiledDocumentsTests extends BasicServiceTestUni
// @@@@@@@@@@@@@@@ Routines
protected Project createNew(Document content) throws Exception {
Project doc =check(baseTarget().request(MediaType.APPLICATION_JSON).
post(Entity.entity(content, MediaType.APPLICATION_JSON)), Project.class);
BasicTests.validate(doc);

View File

@ -20,6 +20,8 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import java.time.LocalDateTime;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
@ -38,7 +40,7 @@ public class DummyProjectTest extends AbstractProfiledDocumentsTests{
@Override
protected WebTarget baseTarget() {
String testProfileId="basic";
return target(InterfaceConstants.Methods.PROJECTS).path(testProfileId);
return super.baseTarget().path(testProfileId);
}
@ -48,10 +50,13 @@ public class DummyProjectTest extends AbstractProfiledDocumentsTests{
@Test
public void registerNew() throws Exception {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
Project doc = createNew(new Document("field","value"));
assertTrue(doc.getTheDocument().containsKey("field"));
assertTrue(doc.getTheDocument().getString("field").equals("value"));
Document doc = new Document("field","value");
doc.put("startTime", LocalDateTime.now());
System.out.println(Serialization.write(doc));
Project project = createNew(doc);
System.out.println(Serialization.write(project));
assertTrue(project.getTheDocument().containsKey("field"));
assertTrue(project.getTheDocument().getString("field").equals("value"));
}
@ -99,12 +104,26 @@ public class DummyProjectTest extends AbstractProfiledDocumentsTests{
@Test
public void testDelete() throws Exception {
public void testSimpleDelete() throws Exception {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
Document baseDoc=new Document();
baseDoc.put("section",new Document("title","My Title"));
Project doc = createNew(baseDoc);
}
@Test
public void testFullDelete() throws Exception {
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
delete(createWithFileSet().getId(),true);
}
private Project createWithFileSet() throws Exception {
Document baseDoc=new Document();
baseDoc.put("section",new Document("title","My Title"));

View File

@ -1,5 +1,6 @@
package org.gcube.application.geoportal.service.profiledDocuments;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.geoportal.common.model.document.Project;
@ -15,9 +16,12 @@ import org.junit.Test;
import javax.ws.rs.client.WebTarget;
import java.time.LocalDateTime;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@Slf4j
public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{
@ -25,19 +29,27 @@ public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{
@Override
protected WebTarget baseTarget() {
String testProfileId="profiledConcessioni";
return target(InterfaceConstants.Methods.PROJECTS).path(testProfileId);
return super.baseTarget().path(testProfileId);
}
@Test
public void testSDI() throws Exception {
UserUtils.DEFAULT_ROLES.add("Data-Manager");
// Create new
Document theDoc=Document.parse("{\n" +
"\"posizionamentoScavo\" :{\n" +
"\t\"titolo\" : \"mio titolo\"}}");
theDoc.put("startTime", LocalDateTime.now());
log.debug("Sending {}",theDoc);
log.debug("AS JSON : {}",Serialization.write(theDoc));
Project doc = createNew(theDoc);
log.debug("Received : {}",doc);
log.debug("AS JSON : {}",Serialization.write(doc));
// register filesets
@ -71,4 +83,20 @@ public class ProfiledConcessioniTests extends AbstractProfiledDocumentsTests{
assertTrue(doc.getIdentificationReferenceByType(SpatialReference.SPATIAL_REFERENCE_TYPE).size()==1);
System.out.println("Project "+doc.getId()+" published with spatial reference "+doc.getIdentificationReferenceByType(SpatialReference.SPATIAL_REFERENCE_TYPE).get(0));
}
@Test
public void delete() throws Exception {
Document theDoc=Document.parse("{\n" +
"\"posizionamentoScavo\" :{\n" +
"\t\"titolo\" : \"mio titolo\"}}");
theDoc.put("startTime", LocalDateTime.now());
log.debug("sendi");
Project doc = createNew(theDoc);
}
}