Search in sources :

Example 1 with ReflectionEntityInstantiator

use of org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator in project neo4j-ogm by neo4j.

the class SingleUseEntityMapperTest method gh813.

@Test
public void gh813() {
    SingleUseEntityMapper entityMapper = new SingleUseEntityMapper(sessionFactory.metaData(), new ReflectionEntityInstantiator(sessionFactory.metaData()));
    Iterable<Map<String, Object>> results;
    results = sessionFactory.openSession().query("RETURN 'containerName' AS name, [i IN RANGE(0,3) | {a:{id: i, name:'a'+i}, b:{id: i, name:'b'+i}, c:{id: i, name:'c'+i}}] AS rows", EMPTY_MAP).queryResults();
    assertThat(results).hasSize(1);
    Container container = entityMapper.map(Container.class, results.iterator().next());
    assertContainer(container, 4);
    results = sessionFactory.openSession().query("WITH {a: {id: 0, name: 'a0'}, b: {id:0, name:'b0'}, c: {id:0, name: 'c0'}} AS i RETURN 'containerName' as name, collect(i) as rows", EMPTY_MAP).queryResults();
    assertThat(results).hasSize(1);
    container = entityMapper.map(Container.class, results.iterator().next());
    assertContainer(container, 1);
}
Also used : SomeContainer(org.neo4j.ogm.domain.gh391.SomeContainer) Container(org.neo4j.ogm.domain.gh813.Container) HashMap(java.util.HashMap) Map(java.util.Map) ReflectionEntityInstantiator(org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator) Test(org.junit.Test)

Example 2 with ReflectionEntityInstantiator

use of org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator in project neo4j-ogm by neo4j.

the class SingleUseEntityMapperTest method shouldMapAggregatedMapProperties.

// GH-873
@Test
public void shouldMapAggregatedMapProperties() {
    SingleUseEntityMapper entityMapper = new SingleUseEntityMapper(sessionFactory.metaData(), new ReflectionEntityInstantiator(sessionFactory.metaData()));
    Iterable<Map<String, Object>> results = sessionFactory.openSession().query("MATCH (t:MyNode) RETURN t.id AS id, t.type AS type, {b1: 'w1', b2: 'w2'} AS attributes LIMIT 1", EMPTY_MAP).queryResults();
    assertThat(results).hasSize(1);
    MyNode thingResult = entityMapper.map(MyNode.class, results.iterator().next());
    assertThat(thingResult.getId()).isEqualTo("the-id");
    assertThat(thingResult.getType()).isEqualTo("the-type");
    assertThat(thingResult.getAttributes()).containsEntry("b1", "w1");
    assertThat(thingResult.getAttributes()).containsEntry("b2", "w2");
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) MyNode(org.neo4j.ogm.domain.gh873.MyNode) ReflectionEntityInstantiator(org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator) Test(org.junit.Test)

Example 3 with ReflectionEntityInstantiator

use of org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator in project neo4j-ogm by neo4j.

the class SingleUseEntityMapperTest method shouldFailOnIncompatibleProperties.

// GH-748
@Test
public void shouldFailOnIncompatibleProperties() {
    SingleUseEntityMapper entityMapper = new SingleUseEntityMapper(sessionFactory.metaData(), new ReflectionEntityInstantiator(sessionFactory.metaData()));
    Iterable<Map<String, Object>> results = sessionFactory.openSession().query("WITH 'a name' AS something OPTIONAL MATCH (t:ThingEntity) RETURN something, COLLECT(t) as entity", EMPTY_MAP).queryResults();
    assertThat(results).hasSize(1);
    assertThatIllegalArgumentException().isThrownBy(() -> entityMapper.map(ThingResult2.class, results.iterator().next())).withMessageContaining("Can not set org.neo4j.ogm.domain.gh551.ThingEntity field org.neo4j.ogm.domain.gh551.ThingResult2.entity to java.util.ArrayList");
    Condition<String> stringMatches = new Condition<>(s -> s.contains("Cannot map property entity from result set: The result contains more than one entry for the property."), "String matches");
    assertThat(loggerRule.getFormattedMessages()).areAtLeastOne(stringMatches);
}
Also used : Condition(org.assertj.core.api.Condition) HashMap(java.util.HashMap) Map(java.util.Map) ReflectionEntityInstantiator(org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator) Test(org.junit.Test)

Example 4 with ReflectionEntityInstantiator

use of org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator in project neo4j-ogm by neo4j.

the class SingleUseEntityMapperTest method assertThatNullOrEmptyObjectsAreMappedCorrectly.

// GH-777
@Test
public void assertThatNullOrEmptyObjectsAreMappedCorrectly() {
    SingleUseEntityMapper entityMapper = new SingleUseEntityMapper(sessionFactory.metaData(), new ReflectionEntityInstantiator(sessionFactory.metaData()));
    Iterable<Map<String, Object>> results = sessionFactory.openSession().query("OPTIONAL MATCH (ui:UserInfo {email: 'idontexists@here.com'}) RETURN 4711 AS id, 'infoIsNull' AS status, coalesce(ui, null) as info\n" + "UNION\n" + "OPTIONAL MATCH (ui:UserInfo {email: 'idontexists@either.com'}) RETURN 4712 AS id, 'infoIsEmptyStringApocWhen' AS status, coalesce(ui, '') as info\n" + "UNION\n" + "OPTIONAL MATCH (ui:UserInfo {email: 'i@do.com'}) RETURN 4713 AS id, 'existingMatch' AS status, coalesce(ui, '') as info\n", EMPTY_MAP).queryResults();
    assertThat(results).hasSize(3);
    UserSearchDto userSearchDto;
    Iterator<Map<String, Object>> iterator = results.iterator();
    userSearchDto = entityMapper.map(UserSearchDto.class, iterator.next());
    assertThat(userSearchDto.getId()).isEqualTo(4711L);
    assertThat(userSearchDto.getStatus()).isEqualTo("infoIsNull");
    assertThat(userSearchDto.getInfo()).isNull();
    userSearchDto = entityMapper.map(UserSearchDto.class, iterator.next());
    assertThat(userSearchDto.getId()).isEqualTo(4712L);
    assertThat(userSearchDto.getStatus()).isEqualTo("infoIsEmptyStringApocWhen");
    assertThat(userSearchDto.getInfo()).isNull();
    userSearchDto = entityMapper.map(UserSearchDto.class, iterator.next());
    assertThat(userSearchDto.getId()).isEqualTo(4713L);
    assertThat(userSearchDto.getStatus()).isEqualTo("existingMatch");
    assertThat(userSearchDto.getInfo()).isNotNull();
    assertThat(userSearchDto.getInfo().getFirstName()).isEqualTo("Foo");
    assertThat(userSearchDto.getInfo().getLastName()).isEqualTo("Bar");
}
Also used : UserSearchDto(org.neo4j.ogm.domain.gh777.UserSearchDto) HashMap(java.util.HashMap) Map(java.util.Map) ReflectionEntityInstantiator(org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator) Test(org.junit.Test)

Example 5 with ReflectionEntityInstantiator

use of org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator in project neo4j-ogm by neo4j.

the class SingleUseEntityMapperTest method shouldWorkWithCustomConvertersOnListProperty.

// GH-750
@Test
public void shouldWorkWithCustomConvertersOnListProperty() {
    SingleUseEntityMapper entityMapper = new SingleUseEntityMapper(sessionFactory.metaData(), new ReflectionEntityInstantiator(sessionFactory.metaData()));
    Iterable<Map<String, Object>> results = sessionFactory.openSession().query("RETURN ['foo', 'bar'] AS foobars", EMPTY_MAP).queryResults();
    assertThat(results).hasSize(1);
    ThingResult3 thingResult = entityMapper.map(ThingResult3.class, results.iterator().next());
    assertThat(thingResult.getFoobars()).extracting(ThingResult3.FooBar::getValue).containsExactlyInAnyOrder("foo", "bar");
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) ReflectionEntityInstantiator(org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator) ThingResult3(org.neo4j.ogm.domain.gh750.ThingResult3) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 ReflectionEntityInstantiator (org.neo4j.ogm.metadata.reflect.ReflectionEntityInstantiator)18 HashMap (java.util.HashMap)17 Map (java.util.Map)17 ArrayList (java.util.ArrayList)3 ThingResult2 (org.neo4j.ogm.domain.gh551.ThingResult2)3 List (java.util.List)2 SomeContainer (org.neo4j.ogm.domain.gh391.SomeContainer)2 GenericQueryResultWrapper (org.neo4j.ogm.domain.gh551.GenericQueryResultWrapper)2 ThingResult3 (org.neo4j.ogm.domain.gh750.ThingResult3)2 MyNode (org.neo4j.ogm.domain.gh873.MyNode)2 MetaData (org.neo4j.ogm.metadata.MetaData)2 Condition (org.assertj.core.api.Condition)1 SomeQueryResult (org.neo4j.ogm.domain.cineasts.minimum.SomeQueryResult)1 AnotherThing (org.neo4j.ogm.domain.gh551.AnotherThing)1 ThingResult (org.neo4j.ogm.domain.gh551.ThingResult)1 ThingWIthId (org.neo4j.ogm.domain.gh551.ThingWIthId)1 Thing (org.neo4j.ogm.domain.gh552.Thing)1 UserSearchDto (org.neo4j.ogm.domain.gh777.UserSearchDto)1 Container (org.neo4j.ogm.domain.gh813.Container)1