Search in sources :

Example 1 with EntityToJsonMapper

use of nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper in project timbuctoo by HuygensING.

the class EntityToJsonMapperTest method mapEntityMapsTheTypeAndId.

@Test
public void mapEntityMapsTheTypeAndId() throws Exception {
    ReadEntityImpl readEntity = new ReadEntityImpl();
    UUID id = UUID.randomUUID();
    readEntity.setId(id);
    Change change = new Change(Instant.now().toEpochMilli(), USER_ID, null);
    readEntity.setCreated(change);
    readEntity.setModified(change);
    String type = "otherType";
    readEntity.setTypes(Lists.newArrayList("type", type));
    readEntity.setDeleted(false);
    readEntity.setRev(1);
    readEntity.setPid("pid");
    readEntity.setProperties(Lists.newArrayList());
    EntityToJsonMapper instance = new EntityToJsonMapper(userValidator, (collection, id1, rev) -> URI.create("www.example.com"));
    Collection collection = mock(Collection.class);
    when(collection.getEntityTypeName()).thenReturn(type);
    ObjectNode resutlJson = instance.mapEntity(collection, readEntity, false, (readEntity1, resultJson) -> {
    }, (relationRef, resultJson) -> {
    });
    assertThat(resutlJson.toString(), sameJSONAs(jsnO("_id", jsn(id.toString()), "@type", jsn(type)).toString()).allowingExtraUnexpectedFields());
}
Also used : ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) EntityToJsonMapper(nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Change(nl.knaw.huygens.timbuctoo.model.Change) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with EntityToJsonMapper

use of nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper in project timbuctoo by HuygensING.

the class EntityToJsonMapperTest method setUp.

@Before
public void setUp() throws Exception {
    userValidator = mock(UserValidator.class);
    when(userValidator.getUserFromUserId(USER_ID)).thenReturn(Optional.of(User.create(USER_NAME, "")));
    instance = new EntityToJsonMapper(userValidator, (collection, id1, rev) -> URI.create("www.example.com"));
}
Also used : UserValidator(nl.knaw.huygens.timbuctoo.v5.security.UserValidator) JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) Matchers.not(org.hamcrest.Matchers.not) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) Lists(com.google.common.collect.Lists) StringProperty(nl.knaw.huygens.timbuctoo.core.dto.property.StringProperty) EntityToJsonMapper(nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) URI(java.net.URI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) Before(org.junit.Before) HyperLinksProperty(nl.knaw.huygens.timbuctoo.core.dto.property.HyperLinksProperty) RelationRef(nl.knaw.huygens.timbuctoo.core.dto.RelationRef) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) Test(org.junit.Test) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Optional(java.util.Optional) Change(nl.knaw.huygens.timbuctoo.model.Change) Matchers.is(org.hamcrest.Matchers.is) SameJSONAs.sameJSONAs(uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs) Mockito.mock(org.mockito.Mockito.mock) EntityToJsonMapper(nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper) UserValidator(nl.knaw.huygens.timbuctoo.v5.security.UserValidator) Before(org.junit.Before)

Example 3 with EntityToJsonMapper

use of nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper in project timbuctoo by HuygensING.

the class EntityToJsonMapperTest method mapEntityMapsTheProperties.

@Test
public void mapEntityMapsTheProperties() throws Exception {
    ReadEntityImpl readEntity = new ReadEntityImpl();
    readEntity.setId(UUID.randomUUID());
    String userId = "userId";
    Change change = new Change(Instant.now().toEpochMilli(), userId, null);
    readEntity.setCreated(change);
    readEntity.setModified(change);
    String type = "otherType";
    readEntity.setTypes(Lists.newArrayList("type", type));
    readEntity.setDeleted(false);
    readEntity.setRev(1);
    readEntity.setPid("pid");
    ArrayList<TimProperty<?>> properties = Lists.newArrayList();
    properties.add(new StringProperty("name", "Name"));
    readEntity.setProperties(properties);
    EntityToJsonMapper instance = new EntityToJsonMapper(userValidator, (collection, id1, rev) -> URI.create("www.example.com"));
    Collection collection = mock(Collection.class);
    when(collection.getEntityTypeName()).thenReturn(type);
    ObjectNode resutlJson = instance.mapEntity(collection, readEntity, false, (readEntity1, resultJson) -> {
    }, (relationRef, resultJson) -> {
    });
    assertThat(resutlJson.toString(), sameJSONAs(jsnO("name", jsn("Name")).toString()).allowingExtraUnexpectedFields());
}
Also used : ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) EntityToJsonMapper(nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) StringProperty(nl.knaw.huygens.timbuctoo.core.dto.property.StringProperty) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Change(nl.knaw.huygens.timbuctoo.model.Change) Test(org.junit.Test)

Example 4 with EntityToJsonMapper

use of nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper in project timbuctoo by HuygensING.

the class EntityToJsonMapperTest method doesNotAddNonConvertableProperties.

@Test
public void doesNotAddNonConvertableProperties() {
    ReadEntityImpl readEntity = new ReadEntityImpl();
    readEntity.setId(UUID.randomUUID());
    String userId = "userId";
    Change change = new Change(Instant.now().toEpochMilli(), userId, null);
    readEntity.setCreated(change);
    readEntity.setModified(change);
    String type = "otherType";
    readEntity.setTypes(Lists.newArrayList("type", type));
    readEntity.setDeleted(false);
    readEntity.setRev(1);
    readEntity.setPid("pid");
    ArrayList<TimProperty<?>> properties = Lists.newArrayList();
    properties.add(new HyperLinksProperty("nonParsableProp", "Name"));
    readEntity.setProperties(properties);
    EntityToJsonMapper instance = new EntityToJsonMapper(userValidator, (collection, id1, rev) -> URI.create("www.example.com"));
    Collection collection = mock(Collection.class);
    when(collection.getEntityTypeName()).thenReturn(type);
    ObjectNode resutlJson = instance.mapEntity(collection, readEntity, false, (readEntity1, resultJson) -> {
    }, (relationRef, resultJson) -> {
    });
    assertThat(Lists.newArrayList(resutlJson.fieldNames()), not(hasItem("nonParsableProp")));
}
Also used : ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HyperLinksProperty(nl.knaw.huygens.timbuctoo.core.dto.property.HyperLinksProperty) EntityToJsonMapper(nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Change(nl.knaw.huygens.timbuctoo.model.Change) Test(org.junit.Test)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ReadEntityImpl (nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl)4 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)4 EntityToJsonMapper (nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper)4 Change (nl.knaw.huygens.timbuctoo.model.Change)4 Test (org.junit.Test)4 TimProperty (nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty)3 UUID (java.util.UUID)2 HyperLinksProperty (nl.knaw.huygens.timbuctoo.core.dto.property.HyperLinksProperty)2 StringProperty (nl.knaw.huygens.timbuctoo.core.dto.property.StringProperty)2 Lists (com.google.common.collect.Lists)1 URI (java.net.URI)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 RelationRef (nl.knaw.huygens.timbuctoo.core.dto.RelationRef)1 JsonBuilder.jsn (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn)1 JsonBuilder.jsnA (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA)1 JsonBuilder.jsnO (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO)1 UserValidator (nl.knaw.huygens.timbuctoo.v5.security.UserValidator)1