The following functions in PDBStatus are called from PDBStatusTest.java:
1.public static Status getStatus(String pdbId) throws IOException
2.public static Status[] getStatus(String[] pdbIds) throws IOException
3.public static String getCurrent(String oldPdbId) throws IOException
But these function makes network calls, due to latency and network errors, these tests might fail sometimes, introducing flakiness. So, a better way to circumvent this issue is via mocking.
Example: testGetStatus() calls getStatus() in PDBStatus and a call is made to the following URL:
https://data.rcsb.org/rest/v1/holdings/status/1HHB
Assert.assertEquals(Status.REMOVED, PDBStatus.getStatus("1HHB"));
Considering the data returned from the call, the resulting JSON node can be marked as:
String jsonString = "{\"rcsb_repository_holdings_combined\":{\"id_code_replaced_by_latest\":\"4HHB\",\"status\":\"REMOVED\",\"status_code\":\"OBS\"},\"rcsb_repository_holdings_combined_entry_container_identifiers\":{\"entry_id\":\"1HHB\",\"rcsb_id\":\"1HHB\",\"update_id\":\"2020_48\"},\"rcsb_id\":\"1HHB\"}";
ObjectMapper mapper = new ObjectMapper();
JsonNode **actualObj** = mapper.readTree(jsonString);
The getStatus() function is:
/**
* Get the status of a PDB id.
*
* @param pdbId the id
* @return The status.
*/
public static Status getStatus(String pdbId) throws IOException {
URL url = new URL(String.format(STATUS_ENDPOINT, DEFAULT_RCSB_DATA_API_SERVER, pdbId.toUpperCase()));
ObjectMapper objectMapper = new ObjectMapper();
JsonNode node = objectMapper.readValue(url.openStream(), JsonNode.class);
return parseStatusRecord(node);
}
We can use power mockito dependency in POM.xml
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
A few ways to mock the various objects are:
ObjectMapper mockObjectMapper = Mockito.mock(ObjectMapper.class);
InputStream inr = Mockito.mock(InputStream.class);
when(mockObjectMapper.readValue(Mockito.any(InputStream.class), JsonNode.class)).thenReturn(**actualObj**);
when(mockObjectMapper.readValue(Mockito.mock(InputStream.class), JsonNode.class)).thenReturn(**actualObj**);
This can be better achieved by moving parseStatusRecord() logic from getStatus() so that only the output from the network call can be mocked.
The following functions in PDBStatus are called from PDBStatusTest.java:
1.public static Status getStatus(String pdbId) throws IOException
2.public static Status[] getStatus(String[] pdbIds) throws IOException
3.public static String getCurrent(String oldPdbId) throws IOException
But these function makes network calls, due to latency and network errors, these tests might fail sometimes, introducing flakiness. So, a better way to circumvent this issue is via mocking.
Example: testGetStatus() calls getStatus() in PDBStatus and a call is made to the following URL:
https://data.rcsb.org/rest/v1/holdings/status/1HHB
Assert.assertEquals(Status.REMOVED, PDBStatus.getStatus("1HHB"));
Considering the data returned from the call, the resulting JSON node can be marked as:
The getStatus() function is:
We can use power mockito dependency in POM.xml
A few ways to mock the various objects are:
This can be better achieved by moving parseStatusRecord() logic from getStatus() so that only the output from the network call can be mocked.