DeduplicationController { @PostMapping public PotentialDuplicate postPotentialDuplicate( @RequestBody PotentialDuplicate potentialDuplicate ) throws WebMessageException { validatePotentialDuplicate( potentialDuplicate ); deduplicationService.addPotentialDuplicate( potentialDuplicate); return potentialDuplicate; } DeduplicationController( DeduplicationService deduplicationService,
        TrackedEntityInstanceService trackedEntityInstanceService,
        TrackerAccessManager trackerAccessManager, CurrentUserService currentUserService,
        FieldFilterService fieldFilterService,
        ContextService contextService ); @GetMapping Node getAll(
        PotentialDuplicateQuery query,
        HttpServletResponse response
    ); @GetMapping( value = "/{id}" ) PotentialDuplicate getPotentialDuplicate(
        @PathVariable String id
    ); @PostMapping PotentialDuplicate postPotentialDuplicate(
        @RequestBody PotentialDuplicate potentialDuplicate
    ); @RequestMapping( method = { RequestMethod.PUT, RequestMethod.POST }, value = "/{id}/invalidation" ) void markPotentialDuplicateInvalid(
        @PathVariable String id
    ); @DeleteMapping( value = "/{id}" ) void deletePotentialDuplicate(
        @PathVariable String id
    );  }
@Test public void postPotentialDuplicate() throws WebMessageException { PotentialDuplicate pd = new PotentialDuplicate( "ABCDEFGHIJ1", "ABCDEFGHIJ2" ); TrackedEntityInstance teiA = new TrackedEntityInstance(); TrackedEntityInstance teiB = new TrackedEntityInstance(); teiA.setUid( "ABCDEFGHIJ1" ); teiB.setUid( "ABCDEFGHIJ2" ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ1" ) ) ) .thenReturn( teiA ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ2" ) ) ) .thenReturn( teiB ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiA ) ) ).thenReturn( Lists.newArrayList() ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiB ) ) ).thenReturn( Lists.newArrayList() ); Mockito.when( deduplicationService.exists( pd ) ).thenReturn( false ); Mockito.when( deduplicationService.addPotentialDuplicate( Mockito.any( PotentialDuplicate.class ) ) ) .thenReturn( 1L ); controller.postPotentialDuplicate( new PotentialDuplicate( "ABCDEFGHIJ1" ) ); }
@Test public void postPotentialDuplicateMissingRequiredPropertyTeiA() { try { controller.postPotentialDuplicate( new PotentialDuplicate() ); } catch ( WebMessageException e ) { WebMessage wm = e.getWebMessage(); assertEquals( "Conflict", wm.getHttpStatus() ); assertEquals( 409, wm.getHttpStatusCode().intValue() ); assertEquals( Status.ERROR, wm.getStatus() ); assertEquals( "Missing required property 'teiA'", wm.getMessage() ); } }
@Test public void postPotentialDuplicateInvalidUid() { try { controller.postPotentialDuplicate( new PotentialDuplicate( "invalid" ) ); } catch ( WebMessageException e ) { WebMessage wm = e.getWebMessage(); assertEquals( "Conflict", wm.getHttpStatus() ); assertEquals( 409, wm.getHttpStatusCode().intValue() ); assertEquals( Status.ERROR, wm.getStatus() ); assertEquals( "'invalid' is not valid value for property 'teiA'", wm.getMessage() ); } }
@Test public void postPotentialDuplicateInvalidTei() { try { controller.postPotentialDuplicate( new PotentialDuplicate( "ABCDEFGHIJ0" ) ); } catch ( WebMessageException e ) { WebMessage wm = e.getWebMessage(); assertEquals( "Conflict", wm.getHttpStatus() ); assertEquals( 409, wm.getHttpStatusCode().intValue() ); assertEquals( Status.ERROR, wm.getStatus() ); assertEquals( "No tracked entity instance found with id 'ABCDEFGHIJ0'.", wm.getMessage() ); } }
@Test public void postPotentialDuplicateNoAccessToTeiA() { TrackedEntityInstance teiA = new TrackedEntityInstance(); teiA.setUid( "ABCDEFGHIJ1" ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ1" ) ) ) .thenReturn( teiA ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiA ) ) ).thenReturn( Lists.newArrayList( "Error" ) ); try { controller.postPotentialDuplicate( new PotentialDuplicate( "ABCDEFGHIJ1" ) ); } catch ( WebMessageException e ) { WebMessage wm = e.getWebMessage(); assertEquals( "Forbidden", wm.getHttpStatus() ); assertEquals( 403, wm.getHttpStatusCode().intValue() ); assertEquals( Status.ERROR, wm.getStatus() ); assertEquals( "You don't have read access to 'ABCDEFGHIJ1'.", wm.getMessage() ); } }
@Test public void postPotentialDuplicateInvalidUidTeiB() { TrackedEntityInstance teiA = new TrackedEntityInstance(); teiA.setUid( "ABCDEFGHIJ1" ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ1" ) ) ) .thenReturn( teiA ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiA ) ) ).thenReturn( Lists.newArrayList() ); try { controller.postPotentialDuplicate( new PotentialDuplicate( "ABCDEFGHIJ1", "invalid" ) ); } catch ( WebMessageException e ) { WebMessage wm = e.getWebMessage(); assertEquals( "Conflict", wm.getHttpStatus() ); assertEquals( 409, wm.getHttpStatusCode().intValue() ); assertEquals( Status.ERROR, wm.getStatus() ); assertEquals( "'invalid' is not valid value for property 'teiB'", wm.getMessage() ); } }
@Test public void postPotentialDuplicateNoAccessToTeiB() { TrackedEntityInstance teiA = new TrackedEntityInstance(); TrackedEntityInstance teiB = new TrackedEntityInstance(); teiA.setUid( "ABCDEFGHIJ1" ); teiB.setUid( "ABCDEFGHIJ2" ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ1" ) ) ) .thenReturn( teiA ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ2" ) ) ) .thenReturn( teiB ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiA ) ) ).thenReturn( Lists.newArrayList() ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiB ) ) ).thenReturn( Lists.newArrayList( "Error" ) ); try { controller.postPotentialDuplicate( new PotentialDuplicate( "ABCDEFGHIJ1", "ABCDEFGHIJ2" ) ); } catch ( WebMessageException e ) { WebMessage wm = e.getWebMessage(); assertEquals( "Forbidden", wm.getHttpStatus() ); assertEquals( 403, wm.getHttpStatusCode().intValue() ); assertEquals( Status.ERROR, wm.getStatus() ); assertEquals( "You don't have read access to 'ABCDEFGHIJ2'.", wm.getMessage() ); } }
@Test public void postPotentialDuplicateAlreadyExists() { PotentialDuplicate pd = new PotentialDuplicate( "ABCDEFGHIJ1", "ABCDEFGHIJ2" ); TrackedEntityInstance teiA = new TrackedEntityInstance(); TrackedEntityInstance teiB = new TrackedEntityInstance(); teiA.setUid( "ABCDEFGHIJ1" ); teiB.setUid( "ABCDEFGHIJ2" ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ1" ) ) ) .thenReturn( teiA ); Mockito.when( trackedEntityInstanceService.getTrackedEntityInstance( Mockito.eq( "ABCDEFGHIJ2" ) ) ) .thenReturn( teiB ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiA ) ) ).thenReturn( Lists.newArrayList() ); Mockito.when( trackerAccessManager.canRead( Mockito.any(), Mockito.eq( teiB ) ) ).thenReturn( Lists.newArrayList() ); Mockito.when( deduplicationService.exists( pd ) ).thenReturn( true ); try { controller.postPotentialDuplicate( pd ); } catch ( WebMessageException e ) { WebMessage wm = e.getWebMessage(); assertEquals( "Conflict", wm.getHttpStatus() ); assertEquals( 409, wm.getHttpStatusCode().intValue() ); assertEquals( Status.ERROR, wm.getStatus() ); assertEquals( "'ABCDEFGHIJ1' and 'ABCDEFGHIJ2' is already marked as a potential duplicate", wm.getMessage() ); } }