Skip to content

Commit

Permalink
SDK-2478: Allow the Relying Business to delete tracked device metadat…
Browse files Browse the repository at this point in the history
…a for a given session
  • Loading branch information
MrBurtyyy committed Sep 24, 2024
1 parent d7ff6f7 commit a0484fc
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,26 @@ public SessionConfigurationResponse getSessionConfiguration(String sessionId) th
*
* @param sessionId the session ID
*
* @return the session configuration
* @return the list of tracked devices information
* @throws DocScanException if an error has occurred
*/
public List<MetadataResponse> getTrackedDevices(String sessionId) throws DocScanException {
LOG.debug("Fetching tracked devices for session '{}'", sessionId);
return docScanService.getTrackedDevices(sdkId, keyPair, sessionId);
}

/**
* Deletes the tracked devices metadata for the given sessionID.
*
* @param sessionId the session ID
*
* @throws DocScanException if an error has occurred
*/
public void deleteTrackedDevices(String sessionId) throws DocScanException {
LOG.debug("Deleting tracked devices for session '{}'", sessionId);
docScanService.deleteTrackedDevices(sdkId, keyPair, sessionId);
}

private KeyPair loadKeyPair(KeyPairSource kpSource) throws InitialisationException {
try {
LOG.debug("Loading key pair from '{}'", kpSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,29 @@ public List<MetadataResponse> getTrackedDevices(String sdkId, KeyPair keyPair, S
}
}

public void deleteTrackedDevices(String sdkId, KeyPair keyPair, String sessionId) throws DocScanException {
notNullOrEmpty(sdkId, "SDK ID");
notNull(keyPair, "Application key Pair");
notNullOrEmpty(sessionId, "sessionId");

String path = unsignedPathFactory.createDeleteTrackedDevices(sdkId, sessionId);
LOG.info("Deleting tracked devices at '{}'", path);

try {
signedRequestBuilderFactory.create()
.withKeyPair(keyPair)
.withBaseUrl(apiUrl)
.withEndpoint(path)
.withHttpMethod(HTTP_DELETE)
.build()
.execute();
} catch (GeneralSecurityException | ResourceException ex) {
throw new DocScanException("Error executing the GET: " + ex.getMessage(), ex);
} catch (IOException | URISyntaxException ex) {
throw new DocScanException("Error building the request: " + ex.getMessage(), ex);
}
}

private String findContentType(SignedRequestResponse response) {
List<String> contentTypeValues = null;
for (Map.Entry<String, List<String>> entry : response.getResponseHeaders().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class MetadataResponse {
@JsonProperty("event")
private String event;

@JsonProperty("resource_id")
private String resourceId;

@JsonProperty("created")
private String created;

Expand All @@ -17,6 +20,10 @@ public String getEvent() {
return event;
}

public String getResourceId() {
return resourceId;
}

public String getCreated() {
return created;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class UnsignedPathFactory {
private static final String DOCS_NEW_FACE_CAPTURE_RESOURCE = "/sessions/%s/resources/face-capture?sdkId=%s";
private static final String DOCS_UPLOAD_FACE_CAPTURE_IMAGE = "/sessions/%s/resources/face-capture/%s/image?sdkId=%s";
private static final String DOCS_TRIGGER_IBV_NOTIFICATION = "/sessions/%s/instructions/email?sdkId=%s";
private static final String DOCS_FETCH_TRACKED_DEVICES = "/sessions/%s/tracked-devices?sdkId=%s";
private static final String DOCS_TRACKED_DEVICES = "/sessions/%s/tracked-devices?sdkId=%s";

public String createAmlPath(String appId) {
return format(AML, appId);
Expand Down Expand Up @@ -123,7 +123,11 @@ public String createTriggerIbvEmailNotificationPath(String sdkId, String session
}

public String createFetchTrackedDevices(String sdkId, String sessionId) {
return format(DOCS_FETCH_TRACKED_DEVICES, sessionId, sdkId);
return format(DOCS_TRACKED_DEVICES, sessionId, sdkId);
}

public String createDeleteTrackedDevices(String sdkId, String sessionId) {
return format(DOCS_TRACKED_DEVICES, sessionId, sdkId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,15 @@ public void getTrackedDevices_shouldFailWithExceptionFromYotiDocsService() throw
assertThat(thrown, is(original));
}

@Test
public void deleteTrackedDevices_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");
doThrow(original).when(docScanServiceMock).deleteTrackedDevices(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.deleteTrackedDevices(SOME_SESSION_ID));

assertThat(thrown, is(original));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1538,4 +1538,80 @@ public void getTrackedDevices_shouldReturnTrackedDevices() throws Exception {
assertThat(result.get(0), is(metadataResponseMock));
}

@Test
public void deleteTrackedDevices_shouldThrowExceptionWhenSdkIdIsNull() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> docScanService.deleteTrackedDevices(null, KEY_PAIR, SOME_SESSION_ID));
assertThat(exception.getMessage(), containsString("SDK ID"));
}

@Test
public void deleteTrackedDevices_shouldThrowExceptionWhenSdkIdIsEmpty() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> docScanService.deleteTrackedDevices("", KEY_PAIR, SOME_SESSION_ID));
assertThat(exception.getMessage(), containsString("SDK ID"));
}

@Test
public void deleteTrackedDevices_shouldThrowExceptionWhenKeyPairIsNull() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> docScanService.deleteTrackedDevices(SOME_APP_ID, null, SOME_SESSION_ID));
assertThat(exception.getMessage(), containsString("Application key Pair"));
}

@Test
public void deleteTrackedDevices_shouldThrowExceptionWhenSessionIdIsNull() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> docScanService.deleteTrackedDevices(SOME_APP_ID, KEY_PAIR, null));
assertThat(exception.getMessage(), containsString("sessionId"));
}

@Test
public void deleteTrackedDevices_shouldThrowExceptionWhenSessionIdIsEmpty() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> docScanService.deleteTrackedDevices(SOME_APP_ID, KEY_PAIR, ""));
assertThat(exception.getMessage(), containsString("sessionId"));
}

@Test
public void deleteTrackedDevices_shouldWrapGeneralSecurityException() throws Exception {
GeneralSecurityException gse = new GeneralSecurityException("some gse");
when(signedRequestBuilderMock.build()).thenThrow(gse);

DocScanException ex = assertThrows(DocScanException.class, () -> docScanService.deleteTrackedDevices(SOME_APP_ID, KEY_PAIR, SOME_SESSION_ID));

assertSame(ex.getCause(), gse);
assertThat(ex.getMessage(), containsString("Error executing the GET: some gse"));
}

@Test
public void deleteTrackedDevices_shouldWrapResourceException() throws Exception {
ResourceException resourceException = new ResourceException(400, "Failed Request", "Some response from API");
when(signedRequestBuilderMock.build()).thenReturn(signedRequestMock);
when(signedRequestMock.execute()).thenThrow(resourceException);

DocScanException ex = assertThrows(DocScanException.class, () -> docScanService.deleteTrackedDevices(SOME_APP_ID, KEY_PAIR, SOME_SESSION_ID));

assertSame(ex.getCause(), resourceException);
assertThat(ex.getMessage(), containsString("Error executing the GET: Failed Request"));
}

@Test
public void deleteTrackedDevices_shouldWrapIOException() throws Exception {
IOException ioException = new IOException("Some io exception");
when(signedRequestBuilderMock.build()).thenReturn(signedRequestMock);
when(signedRequestMock.execute()).thenThrow(ioException);

DocScanException ex = assertThrows(DocScanException.class, () -> docScanService.deleteTrackedDevices(SOME_APP_ID, KEY_PAIR, SOME_SESSION_ID));

assertSame(ex.getCause(), ioException);
assertThat(ex.getMessage(), containsString("Error building the request: Some io exception"));
}

@Test
public void deleteTrackedDevices_shouldWrapURISyntaxException() throws Exception {
URISyntaxException uriSyntaxException = new URISyntaxException("someUrl", "Failed to build URI");
when(signedRequestBuilderMock.build()).thenThrow(uriSyntaxException);

DocScanException ex = assertThrows(DocScanException.class, () -> docScanService.deleteTrackedDevices(SOME_APP_ID, KEY_PAIR, SOME_SESSION_ID));

assertSame(ex.getCause(), uriSyntaxException);
assertThat(ex.getMessage(), containsString("Error building the request: Failed to build URI: someUrl"));
}

}

0 comments on commit a0484fc

Please sign in to comment.