Search in sources :

Example 6 with BackEndApi

use of io.apiman.test.policies.BackEndApi in project apiman-plugins by apiman.

the class TransformationPolicyTest method transformClientXmlRequesttToJson.

@Test
@Configuration("{\"clientFormat\": \"XML\", \"serverFormat\": \"JSON\"}")
@BackEndApi(ConsumeJsonBackEndApi.class)
public void transformClientXmlRequesttToJson() throws Throwable {
    String xml = "<a><b>test</b></a>";
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.POST, "/some/resource");
    request.header("Content-Type", "application/xml");
    request.header("Content-Length", String.valueOf(xml.getBytes("UTF-8").length));
    request.body(xml);
    send(request);
}
Also used : PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) ProduceXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceXmlBackEndApi) ProduceJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceJsonBackEndApi) ProduceComplexJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceComplexJsonBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) ConsumeJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeJsonBackEndApi) ConsumeXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeXmlBackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 7 with BackEndApi

use of io.apiman.test.policies.BackEndApi in project apiman-plugins by apiman.

the class TransformationPolicyTest method transformServerEchoXmlResponseToJson.

@Test
@Configuration("{\"clientFormat\": \"JSON\", \"serverFormat\": \"XML\"}")
@BackEndApi(ProduceEchoXmlResponseBackend.class)
public void transformServerEchoXmlResponseToJson() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    PolicyTestResponse response = send(request);
    assertEquals("application/json", response.header("Content-Type"));
    assertNull(response.header("Content-Length"));
    assertTrue(response.body().startsWith("{\"echoResponse\":"));
}
Also used : PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) ProduceXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceXmlBackEndApi) ProduceJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceJsonBackEndApi) ProduceComplexJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceComplexJsonBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) ConsumeJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeJsonBackEndApi) ConsumeXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeXmlBackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 8 with BackEndApi

use of io.apiman.test.policies.BackEndApi in project apiman by apiman.

the class TransferQuotaPolicyTest method testDownloadLimitNoHeaderConfig.

@Test
@Configuration("{" + "  \"limit\" : 1000," + "  \"direction\" : \"download\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Minute\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimitNoHeaderConfig() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    request.header("X-Payload-Size", "389");
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("1000", response.header("X-TransferQuota-Remaining"));
    Assert.assertEquals("1000", response.header("X-TransferQuota-Limit"));
    send(request);
    send(request);
    // Now if we try it one more time, we'll get a failure!
    try {
        send(request);
        Assert.fail("Expected a policy failure!");
    } catch (PolicyFailureError e) {
        PolicyFailure failure = e.getFailure();
        Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
        Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
        Assert.assertEquals(429, failure.getResponseCode());
    }
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 9 with BackEndApi

use of io.apiman.test.policies.BackEndApi in project apiman by apiman.

the class TransferQuotaPolicyTest method testDownloadLimit.

@Test
@Configuration("{" + "  \"limit\" : 1000," + "  \"direction\" : \"download\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"X-DBytes-Remaining\"," + "  \"headerLimit\" : \"X-DBytes-Limit\"," + "  \"headerReset\" : \"X-DBytes-Reset\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimit() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    request.header("X-Payload-Size", "389");
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("1000", response.header("X-DBytes-Remaining"));
    Assert.assertEquals("1000", response.header("X-DBytes-Limit"));
    send(request);
    send(request);
    // Now if we try it one more time, we'll get a failure!
    try {
        send(request);
        Assert.fail("Expected a policy failure!");
    } catch (PolicyFailureError e) {
        PolicyFailure failure = e.getFailure();
        Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
        Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
        Assert.assertEquals(429, failure.getResponseCode());
    }
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 10 with BackEndApi

use of io.apiman.test.policies.BackEndApi in project apiman by apiman.

the class TransferQuotaPolicyTest method testDownloadLimitEmptyHeaderConfig.

@Test
@Configuration("{" + "  \"limit\" : 1000," + "  \"direction\" : \"download\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"\"," + "  \"headerLimit\" : \"\"," + "  \"headerReset\" : \"\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimitEmptyHeaderConfig() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    request.header("X-Payload-Size", "389");
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("1000", response.header("X-TransferQuota-Remaining"));
    Assert.assertEquals("1000", response.header("X-TransferQuota-Limit"));
    send(request);
    send(request);
    // Now if we try it one more time, we'll get a failure!
    try {
        send(request);
        Assert.fail("Expected a policy failure!");
    } catch (PolicyFailureError e) {
        PolicyFailure failure = e.getFailure();
        Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
        Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
        Assert.assertEquals(429, failure.getResponseCode());
    }
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Aggregations

ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)15 BackEndApi (io.apiman.test.policies.BackEndApi)15 Configuration (io.apiman.test.policies.Configuration)15 Test (org.junit.Test)15 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)13 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)12 ConsumeJsonBackEndApi (io.apiman.plugins.transformation_policy.backend.ConsumeJsonBackEndApi)8 ConsumeXmlBackEndApi (io.apiman.plugins.transformation_policy.backend.ConsumeXmlBackEndApi)8 ProduceComplexJsonBackEndApi (io.apiman.plugins.transformation_policy.backend.ProduceComplexJsonBackEndApi)8 ProduceJsonBackEndApi (io.apiman.plugins.transformation_policy.backend.ProduceJsonBackEndApi)8 ProduceXmlBackEndApi (io.apiman.plugins.transformation_policy.backend.ProduceXmlBackEndApi)8 IPolicyTestBackEndApi (io.apiman.test.policies.IPolicyTestBackEndApi)7 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)7 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3