Skip to content

Commit

Permalink
Merge branch 'sprint6-93-renewals' into sprint6-93-renewals-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jsavell committed Mar 5, 2021
2 parents 0f244d0 + 782ee60 commit 1a93444
Showing 1 changed file with 32 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class PatronControllerTest {
private static final String LOANS_ENDPOINT = "loans";
private static final String FINES_ENDPOINT = "fines";
private static final String RENEWAL_ENDPOINT = "renew";
private static final String DOC_PREFIX = "patron/";
private static final String RENEW_MVC_PATH = RENEWAL_ENDPOINT+"/{itemId}";


@Value("classpath:mock/patron/account.json")
private Resource patronAccountResource;
Expand Down Expand Up @@ -158,18 +161,25 @@ public void testLoanItemRenewalMockMVC() throws Exception {
fieldWithPath("title").description("The title of the loan item."),
fieldWithPath("author").description("The author of the loan item.")
);
String renewalEndpoint = RENEWAL_ENDPOINT+"/{itemId}";
postEndpointWithMockMVC(getRenewalUrl(), RENEWAL_ENDPOINT, renewalEndpoint, pathParameters, requestParameters, responseFields, patronAccountRenewalResource);
}

@Test
public void testFinesMockMVCWithCatalogName() throws Exception {
getEndpointWithCatalogName(getFinesUrl(), FINES_ENDPOINT);
}
expectPostResponse(getRenewalUrl(), once(), okResponse(patronAccountRenewalResource));
mockMvc.perform(
post("/patron/{uin}/" + RENEW_MVC_PATH, UIN, ITEM_ID)
.param("catalogName", FOLIO_CATALOG)
.contentType(MediaType.APPLICATION_JSON)
.content(getMockJson(patronAccountRenewalResource))
)
.andExpect(status().isOk())
.andDo(
document(
DOC_PREFIX + RENEWAL_ENDPOINT,
pathParameters,
requestParameters,
responseFields
)
);

@Test
public void testLoansMockMVCWithCatalogName() throws Exception {
getEndpointWithCatalogName(getLoansUrl(), LOANS_ENDPOINT);
restServer.verify();
}

@Test
Expand Down Expand Up @@ -228,7 +238,7 @@ private void getEndpointWithMockMVC(String sourceUrl, String catalogEndpoint, Pa
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andDo(
document(
"patron/" + catalogEndpoint,
DOC_PREFIX + catalogEndpoint,
pathParameters,
requestParameters,
responseFields
Expand All @@ -238,30 +248,6 @@ private void getEndpointWithMockMVC(String sourceUrl, String catalogEndpoint, Pa
restServer.verify();
}

private void postEndpointWithMockMVC(String sourceUrl, String docsEndpoint, String catalogEndpoint, PathParametersSnippet pathParameters, RequestParametersSnippet requestParameters, ResponseFieldsSnippet responseFields, Resource successResource) throws Exception {
performPostWithCatalogName(sourceUrl, catalogEndpoint, once(), successResponse(successResource), FOLIO_CATALOG)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andDo(
document(
"patron/" + docsEndpoint,
pathParameters,
requestParameters,
responseFields
)
);

restServer.verify();
}

private void getEndpointWithCatalogName(String sourceUrl, String catalogEndpoint) throws Exception {
performGetWithCatalogName(sourceUrl, catalogEndpoint, once(), successResponse(patronAccountResource), FOLIO_CATALOG)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));

restServer.verify();
}

private void getEndpointInternalServerError(String sourceUrl, String catalogEndpoint) throws Exception {
performGet(sourceUrl, catalogEndpoint, once(), withStatus(HttpStatus.INTERNAL_SERVER_ERROR))
.andExpect(status().isInternalServerError());
Expand Down Expand Up @@ -298,28 +284,28 @@ private void getEndpointNotSupportedForCatalog(String sourceUrl, String catalogE
}

private ResultActions performGet(String sourceUrl, String catalogEndpoint, ExpectedCount count, ResponseCreator response) throws Exception {
expectResponse(HttpMethod.GET, sourceUrl, count, response);
expectGetResponse(sourceUrl, count, response);

return mockMvc.perform(get("/patron/{uin}/" + catalogEndpoint, UIN)
.contentType(MediaType.APPLICATION_JSON)
);
}

private ResultActions performGetWithCatalogName(String sourceUrl, String catalogEndpoint, ExpectedCount count, ResponseCreator response, String catalogName) throws Exception {
expectResponse(HttpMethod.GET, sourceUrl, count, response);
expectGetResponse(sourceUrl, count, response);

return mockMvc.perform(get("/patron/{uin}/" + catalogEndpoint, UIN)
.param("catalogName", catalogName)
.contentType(MediaType.APPLICATION_JSON)
);
}

private ResultActions performPostWithCatalogName(String sourceUrl, String catalogEndpoint, ExpectedCount count, ResponseCreator response, String catalogName) throws Exception {
private void expectGetResponse(String sourceUrl, ExpectedCount count, ResponseCreator response) throws Exception {
expectResponse(HttpMethod.GET, sourceUrl, count, response);
}

private void expectPostResponse(String sourceUrl, ExpectedCount count, ResponseCreator response) throws Exception {
expectResponse(HttpMethod.POST, sourceUrl, count, response);
return mockMvc.perform(post("/patron/{uin}/" + catalogEndpoint, UIN, ITEM_ID)
.param("catalogName", catalogName)
.contentType(MediaType.APPLICATION_JSON)
);
}

private void expectResponse(HttpMethod httpMethod, String sourceUrl, ExpectedCount count, ResponseCreator response) throws Exception {
Expand All @@ -345,6 +331,10 @@ private String getRenewalUrl() {
return String.format("%s/account/%s/item/%s/renew?apikey=%s", BASE_PATH, UIN, ITEM_ID, API_KEY);
}

private DefaultResponseCreator okResponse(Resource resource) throws Exception {
return withStatus(HttpStatus.OK).body(getMockJson(resource)).contentType(MediaType.APPLICATION_JSON);
}

private DefaultResponseCreator successResponse(Resource resource) throws Exception {
return withSuccess(getMockJson(resource), MediaType.APPLICATION_JSON);
}
Expand Down

0 comments on commit 1a93444

Please sign in to comment.