SchemaToDataFetcher { public List<? extends IdentifiableObject> fetch( Schema schema ) { if ( schema == null ) { return Collections.emptyList(); } return mapUniqueFields(schema); } SchemaToDataFetcher( SessionFactory sessionFactory ); List<? extends IdentifiableObject> fetch( Schema schema );  }
@Test public void verifyInput() { assertThat(subject.fetch( null ), hasSize(0)); }
@Test public void verifyUniqueFieldsAreMappedToHibernateObject() { Schema schema = createSchema( DataElement.class, "dataElement", Stream.of( createUniqueProperty( Integer.class, "id", true, true ), createProperty( String.class, "name", true, true ), createUniqueProperty( String.class, "code", true, true ), createProperty( Date.class, "created", true, true ), createProperty( Date.class, "lastUpdated", true, true ), createProperty( Integer.class, "int", true, true )).collect(toList()) ); mockSession( "SELECT code,id from " + schema.getKlass().getSimpleName() ); List<Object[]> l = new ArrayList<>(); l.add( new Object[] { "abc", 123456 } ); l.add( new Object[] { "bce", 123888 } ); l.add( new Object[] { "def", 123999 } ); when( query.getResultList() ).thenReturn( l ); List<DataElement> result = (List<DataElement>) subject.fetch( schema ); assertThat( result, hasSize( 3 ) ); assertThat( result, IsIterableContainingInAnyOrder.containsInAnyOrder( allOf( hasProperty( "code", is( "abc" ) ), hasProperty( "id", is( 123456L ) ) ), allOf( hasProperty( "code", is( "bce" ) ), hasProperty( "id", is( 123888L ) ) ), allOf( hasProperty( "code", is( "def" ) ), hasProperty( "id", is( 123999L ) ) ) ) ); }
@Test public void verifyUniqueFieldsAreSkippedOnReflectionError() { Schema schema = createSchema( DummyDataElement.class, "dummyDataElement", Stream.of( createUniqueProperty( String.class, "url", true, true ), createUniqueProperty( String.class, "code", true, true )).collect(toList()) ); mockSession( "SELECT code,url from " + schema.getKlass().getSimpleName() ); List<Object[]> l = new ArrayList(); l.add( new Object[] { "abc", "http: l.add( new Object[] { "bce", "http: l.add( new Object[] { "def", "http: when( query.getResultList() ).thenReturn( l ); List<DataElement> result = (List<DataElement>) subject.fetch( schema ); assertThat( result, hasSize( 2 ) ); assertThat( result, IsIterableContainingInAnyOrder.containsInAnyOrder( allOf( hasProperty( "code", is( "def" ) ), hasProperty( "url", is( "http: ), allOf( hasProperty( "code", is( "abc" ) ), hasProperty( "url", is( "http: ) ) ); }
@Test public void verifyUniqueFieldsAre() { Schema schema = createSchema( DummyDataElement.class, "dummyDataElement", Stream.of( createProperty( String.class, "name", true, true ), createUniqueProperty( String.class, "url", true, true ), createProperty( String.class, "code", true, true )).collect(toList()) ); mockSession( "SELECT url from " + schema.getKlass().getSimpleName() ); List<Object> l = new ArrayList(); l.add( "http: l.add( "http: l.add( "http: when( query.getResultList() ).thenReturn( l ); List<DataElement> result = (List<DataElement>) subject.fetch( schema ); assertThat( result, hasSize( 3 ) ); assertThat( result, IsIterableContainingInAnyOrder.containsInAnyOrder( allOf( hasProperty( "url", is( "http: ), allOf( hasProperty( "url", is( "http: ), allOf( hasProperty( "url", is( "http: ) ) ); }
@Test public void verifyNoSqlWhenUniquePropertiesListIsEmpty() { Schema schema = createSchema( SMSCommand.class, "smsCommand", Lists.newArrayList() ); subject.fetch( schema ); verify( sessionFactory, times( 0 ) ).getCurrentSession(); }
@Test public void verifyNoSqlWhenNoUniquePropertyExist() { Schema schema = createSchema( SMSCommand.class, "smsCommand", Stream.of( createProperty( String.class, "name", true, true ), createProperty( String.class, "id", true, true ) ).collect( toList() ) ); subject.fetch( schema ); verify( sessionFactory, times( 0 ) ).getCurrentSession(); }