Search in sources :

Example 1 with Operand

use of net.morimekta.test.providence.jax.rs.calculator.Operand in project providence by morimekta.

the class TestCalculator method add.

private Operand add(List<Operand> ops) throws CalculateException {
    Imaginary img = null;
    double dbl = 0;
    for (Operand op : ops) {
        Operand operand = op;
        if (operand.unionField() == Operand._Field.OPERATION) {
            operand = calculate(operand.getOperation());
        }
        switch(operand.unionField()) {
            case IMAGINARY:
                if (img == null) {
                    img = new Imaginary(dbl, 0d);
                    dbl = 0d;
                }
                img = addImaginary(img, operand.getImaginary());
                break;
            case NUMBER:
                if (img != null) {
                    img = addImaginary(img, new Imaginary(operand.getNumber(), 0d));
                } else {
                    dbl = dbl + operand.getNumber();
                }
                break;
            default:
                throw CalculateException.builder().setMessage("Unknown operand: " + operand.unionField()).setOperation(op.getOperation()).build();
        }
    }
    if (img != null) {
        return Operand.withImaginary(img);
    } else {
        return Operand.withNumber(dbl);
    }
}
Also used : Operand(net.morimekta.test.providence.jax.rs.calculator.Operand) Imaginary(net.morimekta.test.providence.jax.rs.number.Imaginary)

Example 2 with Operand

use of net.morimekta.test.providence.jax.rs.calculator.Operand in project providence by morimekta.

the class DropWizardIT method testProvidenceJson.

@Test
public void testProvidenceJson() throws IOException {
    Client client = new JerseyClientBuilder(drop_wizard.getEnvironment()).build("test-json");
    Response response = client.target(uri("calculator/calculate")).register(DefaultProvidenceMessageBodyWriter.class).register(DefaultProvidenceMessageBodyReader.class).request().accept(JsonSerializer.MEDIA_TYPE).post(Entity.entity(new Operation(Operator.ADD, list(withNumber(52d), withImaginary(new Imaginary(1d, -1d)), withNumber(15d))), // same problem as with accept.
    JsonSerializer.MEDIA_TYPE));
    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(response.getHeaders().getFirst("Content-Type"), is(equalTo(JsonSerializer.MEDIA_TYPE)));
    Operand op = response.readEntity(Operand.class);
    assertThat(debugString(op), is(equalTo("{\n" + "  imaginary = {\n" + "    v = 68\n" + "    i = -1\n" + "  }\n" + "}")));
}
Also used : Response(javax.ws.rs.core.Response) Operand(net.morimekta.test.providence.jax.rs.calculator.Operand) Operand.withImaginary(net.morimekta.test.providence.jax.rs.calculator.Operand.withImaginary) Imaginary(net.morimekta.test.providence.jax.rs.number.Imaginary) Operation(net.morimekta.test.providence.jax.rs.calculator.Operation) Client(javax.ws.rs.client.Client) JerseyClientBuilder(io.dropwizard.client.JerseyClientBuilder) Test(org.junit.Test)

Example 3 with Operand

use of net.morimekta.test.providence.jax.rs.calculator.Operand in project providence by morimekta.

the class DropWizardIT method testProvidenceServlet.

@Test
public void testProvidenceServlet() throws IOException, CalculateException {
    // This test is just to prove that the providence servlet can be used in dropwizard too.
    Calculator.Iface client = new Calculator.Client(new HttpClientHandler(() -> new GenericUrl(uri("test")), factory(), new DefaultSerializerProvider()));
    Operand result = client.calculate(new Operation(Operator.ADD, list(withNumber(52d), withImaginary(new Imaginary(1d, -1d)), withNumber(15d))));
    assertThat(debugString(result), is(equalTo("{\n" + "  imaginary = {\n" + "    v = 68\n" + "    i = -1\n" + "  }\n" + "}")));
}
Also used : Operand(net.morimekta.test.providence.jax.rs.calculator.Operand) Calculator(net.morimekta.test.providence.jax.rs.calculator.Calculator) DefaultSerializerProvider(net.morimekta.providence.serializer.DefaultSerializerProvider) Operand.withImaginary(net.morimekta.test.providence.jax.rs.calculator.Operand.withImaginary) Imaginary(net.morimekta.test.providence.jax.rs.number.Imaginary) GenericUrl(com.google.api.client.http.GenericUrl) Operation(net.morimekta.test.providence.jax.rs.calculator.Operation) Client(javax.ws.rs.client.Client) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) Test(org.junit.Test)

Example 4 with Operand

use of net.morimekta.test.providence.jax.rs.calculator.Operand in project providence by morimekta.

the class DropWizardIT method testProvidenceBinary.

@Test
public void testProvidenceBinary() throws IOException {
    Client client = new JerseyClientBuilder(drop_wizard.getEnvironment()).build("test-binary");
    Response response = client.target(uri("calculator/calculate")).register(DefaultProvidenceMessageBodyWriter.class).register(DefaultProvidenceMessageBodyReader.class).request().accept(BinarySerializer.MEDIA_TYPE).post(Entity.entity(new Operation(Operator.ADD, list(withNumber(52d), withImaginary(new Imaginary(1d, -1d)), withNumber(15d))), BinarySerializer.MEDIA_TYPE));
    assertThat(response.getStatus(), is(equalTo(200)));
    assertThat(response.getHeaders().getFirst("Content-Type"), is(equalTo(BinarySerializer.MEDIA_TYPE)));
    Operand op = response.readEntity(Operand.class);
    assertThat(debugString(op), is(equalTo("{\n" + "  imaginary = {\n" + "    v = 68\n" + "    i = -1\n" + "  }\n" + "}")));
}
Also used : Response(javax.ws.rs.core.Response) Operand(net.morimekta.test.providence.jax.rs.calculator.Operand) Operand.withImaginary(net.morimekta.test.providence.jax.rs.calculator.Operand.withImaginary) Imaginary(net.morimekta.test.providence.jax.rs.number.Imaginary) Operation(net.morimekta.test.providence.jax.rs.calculator.Operation) Client(javax.ws.rs.client.Client) JerseyClientBuilder(io.dropwizard.client.JerseyClientBuilder) Test(org.junit.Test)

Aggregations

Operand (net.morimekta.test.providence.jax.rs.calculator.Operand)4 Imaginary (net.morimekta.test.providence.jax.rs.number.Imaginary)4 Client (javax.ws.rs.client.Client)3 Operand.withImaginary (net.morimekta.test.providence.jax.rs.calculator.Operand.withImaginary)3 Operation (net.morimekta.test.providence.jax.rs.calculator.Operation)3 Test (org.junit.Test)3 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)2 Response (javax.ws.rs.core.Response)2 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpClientHandler (net.morimekta.providence.client.HttpClientHandler)1 DefaultSerializerProvider (net.morimekta.providence.serializer.DefaultSerializerProvider)1 Calculator (net.morimekta.test.providence.jax.rs.calculator.Calculator)1