Skip to content

Commit

Permalink
NA: UTs can just use assertThrows instead of try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
bucky-boy committed Sep 23, 2024
1 parent e5a6e86 commit c2f3552
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 632 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

import com.yoti.api.client.KeyPairSource;
Expand All @@ -26,40 +27,25 @@ public void setUp() {

@Test
public void build_shouldThrowExceptionWhenSdkIdIsNull() {
try {
DocScanClient.builder()
.build();
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("SDK ID"));
return;
}
fail("Expected an exception");
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> DocScanClient.builder().build());

assertThat(ex.getMessage(), containsString("SDK ID"));
}

@Test
public void build_shouldThrowExceptionWhenSdkIdIsEmpty() {
try {
DocScanClient.builder()
.withClientSdkId("")
.build();
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("SDK ID"));
return;
}
fail("Expected an exception");
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> DocScanClient.builder().withClientSdkId("").build());

assertThat(ex.getMessage(), containsString("SDK ID"));
}

@Test
public void build_shouldThrowExceptionWhenKeyPairSourceIsNull() {
try {
DocScanClient.builder()
.withClientSdkId(SOME_APPLICATION_ID)
.build();
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("Application key Pair"));
return;
}
fail("Expected an exception");
DocScanClient.Builder builder = DocScanClient.builder().withClientSdkId(SOME_APPLICATION_ID);

IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> builder.build());

assertThat(ex.getMessage(), containsString("Application key Pair"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -49,194 +48,142 @@ public void setUp() {
public void constructor_shouldFailWhenStreamExceptionLoadingKeys() {
KeyPairSource badKeyPairSource = new StaticKeyPairSource(true);

try {
new DocScanClient(APP_ID, badKeyPairSource, docScanServiceMock);
} catch (InitialisationException e) {
assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), containsString("Test stream exception"));
return;
}
fail("Expected an Exception");
InitialisationException ex = assertThrows(InitialisationException.class, () -> new DocScanClient(APP_ID, badKeyPairSource, docScanServiceMock));

assertThat(ex.getCause(), is(instanceOf(IOException.class)));
assertThat(ex.getCause().getMessage(), containsString("Test stream exception"));
}

@Test
public void createDocScanSession_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

when(docScanServiceMock.createSession(eq(APP_ID), any(KeyPair.class), eq(sessionSpecMock))).thenThrow(original);
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.createSession(sessionSpecMock));

try {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.createSession(sessionSpecMock);
} catch (DocScanException thrown) {
assertThat(thrown, is(original));
return;
}
fail("Expected an exception");
assertThat(thrown, is(original));
}

@Test
public void getDocScanSession_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

when(docScanServiceMock.retrieveSession(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID))).thenThrow(original);
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

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

try {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.getSession(SOME_SESSION_ID);
} catch (DocScanException thrown) {
assertThat(thrown, is(original));
return;
}
fail("Expected an exception");
assertThat(thrown, is(original));
}

@Test
public void getDocScanMedia_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

when(docScanServiceMock.getMediaContent(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID), eq(SOME_MEDIA_ID))).thenThrow(original);
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

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

try {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.getMediaContent(SOME_SESSION_ID, SOME_MEDIA_ID);
} catch (DocScanException thrown) {
assertThat(thrown, is(original));
return;
}
fail("Expected an exception");
assertThat(thrown, is(original));
}

@Test
public void deleteDocScanMedia_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).deleteMediaContent(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID), eq(SOME_MEDIA_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

try {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.deleteMediaContent(SOME_SESSION_ID, SOME_MEDIA_ID);
} catch (DocScanException thrown) {
assertThat(thrown, is(original));
return;
}
fail("Expected an exception");
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.deleteMediaContent(SOME_SESSION_ID, SOME_MEDIA_ID));

assertThat(thrown, is(original));
}

@Test
public void deleteDocScanSession_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).deleteSession(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

try {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.deleteSession(SOME_SESSION_ID);
} catch (DocScanException thrown) {
assertThat(thrown, is(original));
return;
}
fail("Expected an exception");
DocScanException thrown = assertThrows(DocScanException.class, () -> testObj.deleteSession(SOME_SESSION_ID));

assertThat(thrown, is(original));
}

@Test
public void putIbvInstructions_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).putIbvInstructions(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID), eq(instructionsMock));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException exception = assertThrows(DocScanException.class, () -> {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.putIbvInstructions(SOME_SESSION_ID, instructionsMock);
});
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.putIbvInstructions(SOME_SESSION_ID, instructionsMock));

assertThat(exception, is(original));
}

@Test
public void getIbvInstructions_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).getIbvInstructions(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException exception = assertThrows(DocScanException.class, () -> {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.getIbvInstructions(SOME_SESSION_ID);
});
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.getIbvInstructions(SOME_SESSION_ID));

assertThat(exception, is(original));
}

@Test
public void getIbvInstructionsPdf_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).getIbvInstructionsPdf(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException exception = assertThrows(DocScanException.class, () -> {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.getIbvInstructionsPdf(SOME_SESSION_ID);
});
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.getIbvInstructionsPdf(SOME_SESSION_ID));

assertThat(exception, is(original));
}

@Test
public void fetchInstructionsContactProfile_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).fetchInstructionsContactProfile(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException exception = assertThrows(DocScanException.class, () -> {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.fetchInstructionsContactProfile(SOME_SESSION_ID);
});
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.fetchInstructionsContactProfile(SOME_SESSION_ID));

assertThat(exception, is(original));
}

@Test
public void triggerIbvEmailNotification_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).triggerIbvEmailNotification(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException exception = assertThrows(DocScanException.class, () -> {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.triggerIbvEmailNotification(SOME_SESSION_ID);
});
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.triggerIbvEmailNotification(SOME_SESSION_ID));

assertThat(exception, is(original));
}

@Test
public void getSessionConfiguration_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).fetchSessionConfiguration(eq(APP_ID), any(KeyPair.class), eq(SOME_SESSION_ID));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

DocScanException exception = assertThrows(DocScanException.class, () -> {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.getSessionConfiguration(SOME_SESSION_ID);
});
DocScanException exception = assertThrows(DocScanException.class, () -> testObj.getSessionConfiguration(SOME_SESSION_ID));

assertThat(exception, is(original));
}

@Test
public void getSupportedDocuments_shouldFailWithExceptionFromYotiDocsService() throws Exception {
DocScanException original = new DocScanException("Test exception");

doThrow(original).when(docScanServiceMock).getSupportedDocuments(any(KeyPair.class), any(Boolean.class));
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);

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

try {
DocScanClient testObj = new DocScanClient(APP_ID, validKeyPairSource, docScanServiceMock);
testObj.getSupportedDocuments();
} catch (DocScanException thrown) {
assertThat(thrown, is(original));
return;
}
fail("Expected an exception");
assertThat(thrown, is(original));
}

}
Loading

0 comments on commit c2f3552

Please sign in to comment.