Search in sources :

Example 26 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project open-ecard by ecsec.

the class TestRealVersionUpdate method validVersionUpdate.

@Test(enabled = true)
public void validVersionUpdate() throws InvalidUpdateDefinition, MalformedURLException {
    // Same values as the default values of the JSONObjectBuilder
    String downloadPage = "http://www.google.de/downloadpage";
    String downloadUrl = "http://www.google.de/downloadurl";
    String version = "1.3.0";
    String status = Status.MAINTAINED.name();
    JSONObject obj = new JSONObjectBuilder().build();
    VersionUpdate update = VersionUpdate.fromJson(obj);
    Assert.assertEquals(update.getDownloadPage(), new URL(downloadPage));
    Assert.assertEquals(update.getDownloadLink(), new URL(downloadUrl));
    Assert.assertEquals(update.getVersion().compareTo(new SemanticVersion(version)), 0);
    Assert.assertEquals(update.getStatus(), Status.valueOf(status));
    VersionUpdate newerVersion = new VersionUpdate(new SemanticVersion("1.3.1"), new URL(downloadPage), new URL(downloadUrl), Status.MAINTAINED);
    Assert.assertEquals(update.compareTo(newerVersion), -1);
    Assert.assertEquals(newerVersion.compareTo(update), 1);
}
Also used : JSONObject(org.jose4j.json.internal.json_simple.JSONObject) URL(java.net.URL) SemanticVersion(org.openecard.common.SemanticVersion) Test(org.testng.annotations.Test)

Example 27 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project open-ecard by ecsec.

the class TestRealVersionUpdate method versionUpdateinvalidDownloadURL.

@Test(enabled = true)
public void versionUpdateinvalidDownloadURL() {
    try {
        String invalidDownloadURL = "test";
        JSONObject obj = new JSONObjectBuilder().downloadUrl(invalidDownloadURL).build();
        VersionUpdate update = VersionUpdate.fromJson(obj);
        // Exception expected
        Assert.fail();
    } catch (InvalidUpdateDefinition ex) {
        Assert.assertEquals(ex.getMessage(), "Incomplete JSON data received.");
    }
}
Also used : JSONObject(org.jose4j.json.internal.json_simple.JSONObject) InvalidUpdateDefinition(org.openecard.common.util.InvalidUpdateDefinition) Test(org.testng.annotations.Test)

Example 28 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project light-example-4j by networknt.

the class GetAllTodosIT method testGetAllTodos.

@Test
public void testGetAllTodos() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("host", "lightapi.net");
    map.put("service", "todo");
    map.put("action", "gettodos");
    map.put("version", "0.1.0");
    JSONObject json = new JSONObject();
    json.putAll(map);
    System.out.printf("JSON: %s", json.toString());
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json.toString()));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) ClientConnection(io.undertow.client.ClientConnection) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest)

Example 29 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project light-example-4j by networknt.

the class DeleteTodoIT method testDeleteTodo.

@Test
public void testDeleteTodo() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("host", "lightapi.net");
    map.put("service", "todo");
    map.put("action", "delete");
    map.put("version", "0.1.0");
    Map<String, Object> data = new HashMap<>();
    data.put("id", "101010");
    map.put("data", data);
    JSONObject json = new JSONObject();
    json.putAll(map);
    System.out.printf("JSON: %s", json.toString());
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json.toString()));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) ClientConnection(io.undertow.client.ClientConnection) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 30 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project light-example-4j by networknt.

the class UpdateTodoIT method testUpdateTodo.

@Test
public void testUpdateTodo() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("host", "lightapi.net");
    map.put("service", "todo");
    map.put("action", "update");
    map.put("version", "0.1.0");
    Map<String, Object> data = new HashMap<>();
    data.put("id", "101010");
    data.put("title", "create todo from hybrid service unit test");
    data.put("completed", false);
    data.put("order", 1);
    map.put("data", data);
    JSONObject json = new JSONObject();
    json.putAll(map);
    System.out.printf("JSON: %s", json.toString());
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json.toString()));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) ClientConnection(io.undertow.client.ClientConnection) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Aggregations

JSONObject (org.jose4j.json.internal.json_simple.JSONObject)31 HashMap (java.util.HashMap)14 Test (org.junit.Test)14 Test (org.testng.annotations.Test)10 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)7 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)7 URI (java.net.URI)6 InvalidUpdateDefinition (org.openecard.common.util.InvalidUpdateDefinition)6 Http2Client (com.networknt.client.Http2Client)5 ApiException (com.networknt.exception.ApiException)5 ClientException (com.networknt.exception.ClientException)5 ClientConnection (io.undertow.client.ClientConnection)5 ClientRequest (io.undertow.client.ClientRequest)5 ClientResponse (io.undertow.client.ClientResponse)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 URL (java.net.URL)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 GroovyInterceptor (com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor)3 IOException (java.io.IOException)2