Skip to content

Commit

Permalink
Added sanity test for api call to post
Browse files Browse the repository at this point in the history
  • Loading branch information
knighto82 committed Nov 21, 2024
1 parent bb7f6c0 commit 899496d
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import io.github.jpmorganchase.fusion.api.exception.APICallException;
import io.github.jpmorganchase.fusion.http.Client;
import io.github.jpmorganchase.fusion.http.HttpResponse;
import io.github.jpmorganchase.fusion.model.CatalogResource;
import io.github.jpmorganchase.fusion.model.Dataset;
import io.github.jpmorganchase.fusion.oauth.provider.FusionTokenProvider;
import io.github.jpmorganchase.fusion.serializing.APIRequestSerializer;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -32,6 +35,9 @@ public class FusionApiManagerTest {
@Mock
private FusionTokenProvider fusionTokenProvider;

@Mock
private APIRequestSerializer serializer;

private String apiPath;

private final Map<String, String> requestHeaders = new HashMap<>();
Expand All @@ -42,6 +48,10 @@ public class FusionApiManagerTest {

private String actualResponse;

private CatalogResource catalogResource;

private String serializedCatalogResource;

@Test
void successfulGetCall() {
givenFusionApiManager();
Expand All @@ -54,6 +64,33 @@ void successfulGetCall() {
thenTheResponseBodyShouldMatchExpected();
}

@Test
void successfulPostCall() {
givenFusionApiManager();
givenApiPath("http://localhost:8080/test");
givenSessionBearerToken("my-token");
givenResponseBody("sample response");
givenCatalogResource("dataset_one");
givenSerializedCatalogResource("dataset_one");
givenRequestHeader("Authorization", "Bearer my-token");
givenCallToClientToPostIsSuccessful();
givenCallToSerializeCatalogResource();
WhenFusionApiManagerIsCalledToPost();
thenTheResponseBodyShouldMatchExpected();
}

private void givenSerializedCatalogResource(String identifier) {
serializedCatalogResource = String.format("{\"identifier\":\"%s\"}", identifier);
}

private void givenCallToSerializeCatalogResource() {
given(serializer.serialize(catalogResource)).willReturn(serializedCatalogResource);
}

private void givenCatalogResource(String identifier) {
catalogResource = Dataset.builder().identifier(identifier).build();
}

private void givenSessionBearerToken(String token) {
given(fusionTokenProvider.getSessionBearerToken()).willReturn(token);
}
Expand Down Expand Up @@ -86,6 +123,10 @@ private void WhenFusionApiManagerIsCalledToGet() {
actualResponse = fusionAPIManager.callAPI(apiPath);
}

private void WhenFusionApiManagerIsCalledToPost() {
actualResponse = fusionAPIManager.callAPIToPost(apiPath, catalogResource);
}

private void givenResponseBody(String responseBody) {
this.responseBody = responseBody;
}
Expand All @@ -98,6 +139,14 @@ private void givenCallToClientToGetIsSuccessful() {
when(client.get(apiPath, requestHeaders)).thenReturn(expectedHttpResponse);
}

private void givenCallToClientToPostIsSuccessful() {
HttpResponse<String> expectedHttpResponse = HttpResponse.<String>builder()
.statusCode(200)
.body(responseBody)
.build();
when(client.post(apiPath, requestHeaders, serializedCatalogResource)).thenReturn(expectedHttpResponse);
}

private void thenExceptionMessageShouldMatchExpected(String message) {
assertThat(thrown.getMessage(), is(equalTo(message)));
}
Expand Down Expand Up @@ -126,6 +175,7 @@ private void givenFusionApiManager() {
fusionAPIManager = FusionAPIManager.builder()
.httpClient(client)
.tokenProvider(fusionTokenProvider)
.serializer(serializer)
.build();
}
}

0 comments on commit 899496d

Please sign in to comment.