Search in sources :

Example 31 with HttpResponse

use of io.micronaut.http.HttpResponse in project bi-api by Breeding-Insight.

the class MetadataFilterIntegrationTest method filterNotCalledNoAnnotation.

@Test
public void filterNotCalledNoAnnotation() {
    // Check that the metadata filter does not add metadata when annotation is missing
    Response mockedResponse = getResponseMock();
    when(userController.users(any(QueryParams.class))).thenReturn(HttpResponse.ok(mockedResponse));
    Flowable<HttpResponse<String>> call = client.exchange(GET("/users").cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class);
    HttpResponse<String> response = call.blockingFirst();
    assertEquals(HttpStatus.OK, response.getStatus());
    JsonObject metadata = JsonParser.parseString(response.body()).getAsJsonObject().getAsJsonObject("metadata");
    assertTrue(metadata == null, "Metadata was populated, but shouldn't have been");
}
Also used : Response(org.breedinginsight.api.model.v1.response.Response) DataResponse(org.breedinginsight.api.model.v1.response.DataResponse) HttpResponse(io.micronaut.http.HttpResponse) HttpResponse(io.micronaut.http.HttpResponse) JsonObject(com.google.gson.JsonObject) QueryParams(org.breedinginsight.api.model.v1.request.query.QueryParams) NettyCookie(io.micronaut.http.netty.cookies.NettyCookie) MicronautTest(io.micronaut.test.annotation.MicronautTest) DatabaseTest(org.breedinginsight.DatabaseTest)

Example 32 with HttpResponse

use of io.micronaut.http.HttpResponse in project bi-api by Breeding-Insight.

the class BrapiAuthorizeControllerIntegrationTest method redirectMissingRequiredParameters.

@Test
void redirectMissingRequiredParameters() {
    Flowable<HttpResponse<String>> call = client.exchange(GET(String.format("/programs/%s/brapi/authorize", validProgram.getId())).cookie(new NettyCookie("phylo-token", "test-registered-user")), String.class);
    HttpClientResponseException e = Assertions.assertThrows(HttpClientResponseException.class, () -> {
        HttpResponse<String> response = call.blockingFirst();
    });
    assertEquals(HttpStatus.BAD_REQUEST, e.getStatus());
}
Also used : HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) HttpResponse(io.micronaut.http.HttpResponse) NettyCookie(io.micronaut.http.netty.cookies.NettyCookie) MicronautTest(io.micronaut.test.annotation.MicronautTest) DatabaseTest(org.breedinginsight.DatabaseTest)

Example 33 with HttpResponse

use of io.micronaut.http.HttpResponse in project bi-api by Breeding-Insight.

the class BrAPIV2ControllerIntegrationTest method testPostGetVariablesProxy.

@Test
@SneakyThrows
public void testPostGetVariablesProxy() {
    BrAPIObservationVariable variable = generateVariable();
    Flowable<HttpResponse<String>> postCall = biClient.exchange(POST(String.format("%s/programs/%s/brapi/v2/variables", biApiVersion, validProgram.getId().toString()), Arrays.asList(variable)).contentType(MediaType.APPLICATION_JSON).bearerAuth("test-registered-user"), String.class);
    HttpResponse<String> postResponse;
    try {
        postResponse = postCall.blockingFirst();
    } catch (Exception e) {
        throw new Exception(e);
    }
    // check the POST call was successful
    assertEquals(HttpStatus.OK, postResponse.getStatus());
    BrAPIObservationVariable createdVariable = GSON.fromJson(postResponse.body(), BrAPIObservationVariableListResponse.class).getResult().getData().get(0);
    // and that a variable is returned
    assertNotNull(createdVariable);
    // and that the variable has been assigned an ID
    assertNotNull(createdVariable.getObservationVariableDbId(), "observationVariableDbId is null");
    Flowable<HttpResponse<String>> getCall = biClient.exchange(GET(String.format("%s/programs/%s/brapi/v2/variables/%s", biApiVersion, validProgram.getId().toString(), createdVariable.getObservationVariableDbId())).bearerAuth("test-registered-user"), String.class);
    HttpResponse<String> getResponse;
    try {
        getResponse = getCall.blockingFirst();
    } catch (Exception e) {
        throw new Exception(e);
    }
    assertEquals(HttpStatus.OK, getResponse.getStatus());
    assertNotNull(getResponse.body(), "Response body is empty");
    BrAPIObservationVariableSingleResponse brAPIObservationVariableSingleResponse = GSON.fromJson(getResponse.body(), BrAPIObservationVariableSingleResponse.class);
    BrAPIObservationVariable fetchedVariable = brAPIObservationVariableSingleResponse.getResult();
    assertNotNull(fetchedVariable, "Observation Variable was not found");
    assertEquals(createdVariable.getObservationVariableDbId(), fetchedVariable.getObservationVariableDbId());
    // make sure the original values sent in the POST were saved correctly
    assertEquals(variable.getObservationVariableName(), fetchedVariable.getObservationVariableName());
    assertEquals(variable.getExternalReferences(), fetchedVariable.getExternalReferences());
    assertEquals(variable.getCommonCropName(), fetchedVariable.getCommonCropName());
    assertNotNull(fetchedVariable.getTrait(), "Trait is null");
    assertEquals(createdVariable.getTrait().getTraitDbId(), fetchedVariable.getTrait().getTraitDbId());
    // make sure the original values sent in the POST were saved correctly
    assertEquals(variable.getTrait().getTraitName(), fetchedVariable.getTrait().getTraitName());
    assertEquals(variable.getTrait().getTraitClass(), fetchedVariable.getTrait().getTraitClass());
    assertNotNull(fetchedVariable.getMethod(), "Method is null");
    assertEquals(createdVariable.getMethod().getMethodDbId(), fetchedVariable.getMethod().getMethodDbId());
    // make sure the original values sent in the POST were saved correctly
    assertEquals(variable.getMethod().getMethodName(), fetchedVariable.getMethod().getMethodName());
    assertEquals(variable.getMethod().getMethodClass(), fetchedVariable.getMethod().getMethodClass());
    assertNotNull(fetchedVariable.getScale(), "Scale is null");
    assertEquals(createdVariable.getScale().getScaleDbId(), fetchedVariable.getScale().getScaleDbId());
    // make sure the original values sent in the POST were saved correctly
    assertEquals(variable.getScale().getScaleName(), fetchedVariable.getScale().getScaleName());
    Flowable<HttpResponse<String>> getScaleCall = biClient.exchange(GET(String.format("%s/programs/%s/brapi/v2/scales/%s", biApiVersion, validProgram.getId().toString(), createdVariable.getScale().getScaleDbId())).bearerAuth("test-registered-user"), String.class);
    HttpResponse<String> getScaleResponse;
    try {
        getScaleResponse = getScaleCall.blockingFirst();
    } catch (Exception e) {
        throw new Exception(e);
    }
    assertEquals(HttpStatus.OK, getScaleResponse.getStatus());
    BrAPIScale scaleResponse = GSON.fromJson(JsonParser.parseString(getScaleResponse.body()).getAsJsonObject().getAsJsonObject("result"), BrAPIScale.class);
// TODO this is not being returned
// assertEquals(variable.getScale().getDataType(), scaleResponse.getDataType());
}
Also used : BrAPIObservationVariableSingleResponse(org.brapi.v2.model.pheno.response.BrAPIObservationVariableSingleResponse) HttpResponse(io.micronaut.http.HttpResponse) BrAPIObservationVariableListResponse(org.brapi.v2.model.pheno.response.BrAPIObservationVariableListResponse) MicronautTest(io.micronaut.test.annotation.MicronautTest) BrAPITest(org.breedinginsight.BrAPITest) SneakyThrows(lombok.SneakyThrows)

Example 34 with HttpResponse

use of io.micronaut.http.HttpResponse in project bi-api by Breeding-Insight.

the class BrAPIV2ControllerIntegrationTest method testRootServerInfo.

@Test
public void testRootServerInfo() {
    Flowable<HttpResponse<String>> call = biClient.exchange(GET("/brapi/v2/serverinfo"), String.class);
    HttpResponse<String> response = call.blockingFirst();
    assertEquals(HttpStatus.OK, response.getStatus());
    assertNotNull(response.body(), "Response body is empty");
    JsonObject result = JsonParser.parseString(response.body()).getAsJsonObject().getAsJsonObject("result");
    BrAPIServerInfo serverInfo = GSON.fromJson(result, BrAPIServerInfo.class);
    assertEquals("Breeding Insight Platform", serverInfo.getOrganizationName());
    assertEquals("bi-api", serverInfo.getServerName());
    assertEquals("bidevteam@cornell.edu", serverInfo.getContactEmail());
    assertEquals("breedinginsight.org", serverInfo.getOrganizationURL());
}
Also used : BrAPIServerInfo(org.brapi.v2.model.core.BrAPIServerInfo) HttpResponse(io.micronaut.http.HttpResponse) MicronautTest(io.micronaut.test.annotation.MicronautTest) BrAPITest(org.breedinginsight.BrAPITest)

Example 35 with HttpResponse

use of io.micronaut.http.HttpResponse in project bi-api by Breeding-Insight.

the class BrAPIV2ControllerIntegrationTest method testPutVariablesProxy.

@Test
@SneakyThrows
public void testPutVariablesProxy() {
    BrAPIObservationVariable variable = generateVariable();
    Flowable<HttpResponse<String>> postCall = biClient.exchange(POST(String.format("%s/programs/%s/brapi/v2/variables", biApiVersion, validProgram.getId().toString()), Arrays.asList(variable)).contentType(MediaType.APPLICATION_JSON).bearerAuth("test-registered-user"), String.class);
    HttpResponse<String> postResponse;
    try {
        postResponse = postCall.blockingFirst();
    } catch (Exception e) {
        throw new Exception(e);
    }
    // check the POST call was successful
    assertEquals(HttpStatus.OK, postResponse.getStatus());
    BrAPIObservationVariable createdVariable = GSON.fromJson(postResponse.body(), BrAPIObservationVariableListResponse.class).getResult().getData().get(0);
    createdVariable.setObservationVariableName("Updated variable name");
    Flowable<HttpResponse<String>> putCall = biClient.exchange(PUT(String.format("%s/programs/%s/brapi/v2/variables/%s", biApiVersion, validProgram.getId().toString(), createdVariable.getObservationVariableDbId()), variable).contentType(MediaType.APPLICATION_JSON).bearerAuth("test-registered-user"), String.class);
    HttpResponse<String> putResponse;
    try {
        putResponse = putCall.blockingFirst();
    } catch (Exception e) {
        throw new Exception(e);
    }
    // check the PUT call was successful
    assertEquals(HttpStatus.OK, putResponse.getStatus());
    Flowable<HttpResponse<String>> getCall = biClient.exchange(GET(String.format("%s/programs/%s/brapi/v2/variables/%s", biApiVersion, validProgram.getId().toString(), createdVariable.getObservationVariableDbId())).bearerAuth("test-registered-user"), String.class);
    HttpResponse<String> getResponse;
    try {
        getResponse = getCall.blockingFirst();
    } catch (Exception e) {
        throw new Exception(e);
    }
    assertEquals(HttpStatus.OK, getResponse.getStatus());
    BrAPIObservationVariableSingleResponse brAPIObservationVariableSingleResponse = GSON.fromJson(getResponse.body(), BrAPIObservationVariableSingleResponse.class);
    BrAPIObservationVariable fetchedVariable = brAPIObservationVariableSingleResponse.getResult();
    // make sure the updated value persisted
    assertEquals(variable.getObservationVariableName(), fetchedVariable.getObservationVariableName());
}
Also used : BrAPIObservationVariableSingleResponse(org.brapi.v2.model.pheno.response.BrAPIObservationVariableSingleResponse) HttpResponse(io.micronaut.http.HttpResponse) BrAPIObservationVariableListResponse(org.brapi.v2.model.pheno.response.BrAPIObservationVariableListResponse) MicronautTest(io.micronaut.test.annotation.MicronautTest) BrAPITest(org.breedinginsight.BrAPITest) SneakyThrows(lombok.SneakyThrows)

Aggregations

HttpResponse (io.micronaut.http.HttpResponse)271 MicronautTest (io.micronaut.test.annotation.MicronautTest)178 NettyCookie (io.micronaut.http.netty.cookies.NettyCookie)152 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)141 BrAPITest (org.breedinginsight.BrAPITest)92 DatabaseTest (org.breedinginsight.DatabaseTest)71 SortOrder (org.breedinginsight.api.v1.controller.metadata.SortOrder)69 HttpStatus (io.micronaut.http.HttpStatus)38 Response (org.breedinginsight.api.model.v1.response.Response)36 Test (org.junit.jupiter.api.Test)36 HttpRequest (io.micronaut.http.HttpRequest)33 JsonObject (com.google.gson.JsonObject)32 List (java.util.List)32 DataResponse (org.breedinginsight.api.model.v1.response.DataResponse)31 Argument (io.micronaut.core.type.Argument)28 AddMetadata (org.breedinginsight.api.v1.controller.metadata.AddMetadata)28 Inject (javax.inject.Inject)25 RxHttpClient (io.micronaut.http.client.RxHttpClient)23 Client (io.micronaut.http.client.annotation.Client)23 TestUtil (com.packtpub.micronaut.util.TestUtil)22