OrganisationUnitSupplier extends AbstractSupplier<Map<String, OrganisationUnit>> { @Override public Map<String, OrganisationUnit> get( ImportOptions importOptions, List<Event> events ) { IdScheme idScheme = importOptions.getIdSchemes().getOrgUnitIdScheme(); if ( events == null ) { return new HashMap<>(); } final Set<String> orgUnitUids = events.stream() .filter( e -> e.getOrgUnit() != null ).map( Event::getOrgUnit ) .collect( Collectors.toSet() ); if ( isEmpty( orgUnitUids ) ) { return new HashMap<>(); } Multimap<String, String> orgUnitToEvent = HashMultimap.create(); for ( Event event : events ) { orgUnitToEvent.put( event.getOrgUnit(), event.getUid() ); } return fetchOu( idScheme, orgUnitUids, orgUnitToEvent ); } OrganisationUnitSupplier( NamedParameterJdbcTemplate jdbcTemplate ); @Override Map<String, OrganisationUnit> get( ImportOptions importOptions, List<Event> events );  }
@Test public void handleNullEvents() { assertNotNull( subject.get( ImportOptions.getDefaultImportOptions(), null ) ); }
@Test public void verifyOuWith4LevelHierarchyIsHandledCorrectly() throws SQLException { when( mockResultSet.getLong( "organisationunitid" ) ).thenReturn( 100L ); when( mockResultSet.getString( "uid" ) ).thenReturn( "abcded" ); when( mockResultSet.getString( "code" ) ).thenReturn( "ALFA" ); when( mockResultSet.getString( "path" ) ).thenReturn( "/aaaa/bbbb/cccc/abcded" ); when( mockResultSet.getInt( "hierarchylevel" ) ).thenReturn( 4 ); Event event = new Event(); event.setUid( CodeGenerator.generateUid() ); event.setOrgUnit( "abcded" ); mockResultSetExtractor( mockResultSet ); Map<String, OrganisationUnit> map = subject.get( ImportOptions.getDefaultImportOptions(), Collections.singletonList( event ) ); OrganisationUnit organisationUnit = map.get( event.getUid() ); assertThat( organisationUnit, is( notNullValue() ) ); assertThat( organisationUnit.getId(), is( 100L ) ); assertThat( organisationUnit.getUid(), is( "abcded" ) ); assertThat( organisationUnit.getCode(), is( "ALFA" ) ); assertThat( organisationUnit.getPath(), is( "/aaaa/bbbb/cccc/abcded" ) ); assertThat( organisationUnit.getHierarchyLevel(), is( 4 ) ); }
@Test public void verifyOuWith1LevelHierarchyIsHandledCorrectly() throws SQLException { when( mockResultSet.getLong( "organisationunitid" ) ).thenReturn( 100L ); when( mockResultSet.getString( "uid" ) ).thenReturn( "abcded" ); when( mockResultSet.getString( "code" ) ).thenReturn( "ALFA" ); when( mockResultSet.getString( "path" ) ).thenReturn( "/abcded" ); when( mockResultSet.getInt( "hierarchylevel" ) ).thenReturn( 1 ); Event event = new Event(); event.setUid( CodeGenerator.generateUid() ); event.setOrgUnit( "abcded" ); mockResultSetExtractor( mockResultSet ); Map<String, OrganisationUnit> map = subject.get( ImportOptions.getDefaultImportOptions(), Collections.singletonList( event ) ); OrganisationUnit organisationUnit = map.get( event.getUid() ); assertThat( organisationUnit, is( notNullValue() ) ); assertThat( organisationUnit.getId(), is( 100L ) ); assertThat( organisationUnit.getUid(), is( "abcded" ) ); assertThat( organisationUnit.getCode(), is( "ALFA" ) ); assertThat( organisationUnit.getPath(), is( "/abcded" ) ); assertThat( organisationUnit.getHierarchyLevel(), is( 1 ) ); }
@Test public void verifyCodeSchemaForOu() throws SQLException { when( mockResultSet.getLong( "organisationunitid" ) ).thenReturn( 100L ); when( mockResultSet.getString( "uid" ) ).thenReturn( "abcded" ); when( mockResultSet.getString( "code" ) ).thenReturn( "CODE1" ); when( mockResultSet.getString( "path" ) ).thenReturn( "/abcded" ); when( mockResultSet.getInt( "hierarchylevel" ) ).thenReturn( 1 ); Event event = new Event(); event.setUid( CodeGenerator.generateUid() ); event.setOrgUnit( "CODE1" ); mockResultSetExtractor( mockResultSet ); ImportOptions importOptions = ImportOptions.getDefaultImportOptions(); importOptions.setOrgUnitIdScheme( IdScheme.CODE.name() ); Map<String, OrganisationUnit> map = subject.get( importOptions, Collections.singletonList( event ) ); OrganisationUnit organisationUnit = map.get( event.getUid() ); assertThat( organisationUnit, is( notNullValue() ) ); assertThat( organisationUnit.getId(), is( 100L ) ); assertThat( organisationUnit.getUid(), is( "abcded" ) ); assertThat( organisationUnit.getCode(), is( "CODE1" ) ); assertThat( organisationUnit.getPath(), is( "/abcded" ) ); assertThat( organisationUnit.getHierarchyLevel(), is( 1 ) ); }
@Test public void verifyAttributeSchemaForOu() throws SQLException { when( mockResultSet.getLong( "organisationunitid" ) ).thenReturn( 100L ); when( mockResultSet.getString( "uid" ) ).thenReturn( "abcded" ); when( mockResultSet.getString( "code" ) ).thenReturn( "CODE1" ); when( mockResultSet.getString( "path" ) ).thenReturn( "/abcded" ); when( mockResultSet.getInt( "hierarchylevel" ) ).thenReturn( 1 ); when( mockResultSet.getString( "attributevalues" ) ).thenReturn( "someattributevalue" ); Event event = new Event(); event.setUid( CodeGenerator.generateUid() ); event.setOrgUnit( "someattributevalue" ); mockResultSetExtractor( mockResultSet ); final String attributeId = CodeGenerator.generateUid(); ImportOptions importOptions = ImportOptions.getDefaultImportOptions(); importOptions.setOrgUnitIdScheme( IdScheme.ATTR_ID_SCHEME_PREFIX + attributeId ); Map<String, OrganisationUnit> map = subject.get( importOptions, Collections.singletonList( event ) ); final String executedSql = sql.getValue(); OrganisationUnit organisationUnit = map.get( event.getUid() ); assertThat( organisationUnit, is( notNullValue() ) ); assertThat( organisationUnit.getId(), is( 100L ) ); assertThat( organisationUnit.getUid(), is( "abcded" ) ); assertThat( organisationUnit.getCode(), is( "CODE1" ) ); assertThat( organisationUnit.getPath(), is( "/abcded" ) ); assertThat( organisationUnit.getHierarchyLevel(), is( 1 ) ); assertThat( executedSql, is( "select ou.organisationunitid, ou.uid, ou.code, ou.name, ou.path, ou.hierarchylevel ,attributevalues->'" + attributeId + "'->>'value' as attributevalues from organisationunit ou where ou.attributevalues#>>'{" + attributeId + ",value}' in (:ids)" ) ); }