Search in sources :

Example 1 with HeadlessToolkit

use of com.canoo.platform.client.HeadlessToolkit in project dolphin-platform by canoo.

the class TestDolphinPlatformHttpClientConnector method testBadResponse.

@Test(expectedExceptions = DolphinRemotingException.class)
public void testBadResponse() throws DolphinRemotingException, URISyntaxException {
    PlatformClient.init(new HeadlessToolkit());
    PlatformClient.getClientConfiguration().setHttpURLConnectionFactory(new HttpURLConnectionFactory() {

        @Override
        public HttpURLConnection create(URI url) throws IOException {
            return new HttpURLConnection(url.toURL()) {

                @Override
                public void disconnect() {
                }

                @Override
                public boolean usingProxy() {
                    return false;
                }

                @Override
                public void connect() throws IOException {
                }

                @Override
                public OutputStream getOutputStream() throws IOException {
                    return new ByteArrayOutputStream();
                }
            };
        }
    });
    final ClientModelStore clientModelStore = new ClientModelStore(new DefaultModelSynchronizer(() -> null));
    final DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(getDummyURL(), PlatformClient.getClientConfiguration(), clientModelStore, new JsonCodec(), new SimpleExceptionHandler(), new HttpClientImpl(new Gson(), PlatformClient.getClientConfiguration()));
    final List<Command> commands = new ArrayList<>();
    commands.add(new CreateContextCommand());
    connector.transmit(commands);
}
Also used : DolphinPlatformHttpClientConnector(com.canoo.dp.impl.client.DolphinPlatformHttpClientConnector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) DefaultModelSynchronizer(com.canoo.dp.impl.client.legacy.DefaultModelSynchronizer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleExceptionHandler(com.canoo.dp.impl.client.legacy.communication.SimpleExceptionHandler) CreateContextCommand(com.canoo.dp.impl.remoting.commands.CreateContextCommand) HttpURLConnectionFactory(com.canoo.platform.core.http.HttpURLConnectionFactory) JsonCodec(com.canoo.dp.impl.remoting.legacy.communication.JsonCodec) CreateContextCommand(com.canoo.dp.impl.remoting.commands.CreateContextCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) CreatePresentationModelCommand(com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand) HeadlessToolkit(com.canoo.platform.client.HeadlessToolkit) HttpClientImpl(com.canoo.dp.impl.platform.client.http.HttpClientImpl) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) Test(org.testng.annotations.Test)

Example 2 with HeadlessToolkit

use of com.canoo.platform.client.HeadlessToolkit in project dolphin-platform by canoo.

the class TestDolphinPlatformHttpClientConnector method testSimpleCall.

@Test
public void testSimpleCall() throws DolphinRemotingException, URISyntaxException {
    PlatformClient.init(new HeadlessToolkit());
    PlatformClient.getClientConfiguration().setHttpURLConnectionFactory(new HttpURLConnectionFactory() {

        @Override
        public HttpURLConnection create(URI url) throws IOException {
            return new HttpURLConnection(url.toURL()) {

                @Override
                public void disconnect() {
                }

                @Override
                public boolean usingProxy() {
                    return false;
                }

                @Override
                public void connect() throws IOException {
                }

                @Override
                public int getResponseCode() throws IOException {
                    return HttpStatus.HTTP_OK;
                }

                @Override
                public OutputStream getOutputStream() throws IOException {
                    return new ByteArrayOutputStream();
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    String response = "[{\"pmId\":\"p1\",\"clientSideOnly\":false,\"id\":\"CreatePresentationModel\",\"attributes\":[],\"pmType\":null,\"className\":\"com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand\"}]";
                    return new ByteArrayInputStream(response.getBytes("UTF-8"));
                }

                @Override
                public String getHeaderField(String name) {
                    if (PlatformConstants.CLIENT_ID_HTTP_HEADER_NAME.equals(name)) {
                        return "TEST-ID";
                    }
                    return super.getHeaderField(name);
                }
            };
        }
    });
    final ClientModelStore clientModelStore = new ClientModelStore(new DefaultModelSynchronizer(() -> null));
    final DolphinPlatformHttpClientConnector connector = new DolphinPlatformHttpClientConnector(getDummyURL(), PlatformClient.getClientConfiguration(), clientModelStore, new JsonCodec(), new SimpleExceptionHandler(), PlatformClient.getService(HttpClient.class));
    final CreatePresentationModelCommand command = new CreatePresentationModelCommand();
    command.setPmId("p1");
    Command rawCommand = command;
    final List<Command> result = connector.transmit(Collections.singletonList(rawCommand));
    Assert.assertEquals(result.size(), 1);
    Assert.assertTrue(result.get(0) instanceof CreatePresentationModelCommand);
    Assert.assertEquals(((CreatePresentationModelCommand) result.get(0)).getPmId(), "p1");
}
Also used : DolphinPlatformHttpClientConnector(com.canoo.dp.impl.client.DolphinPlatformHttpClientConnector) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DefaultModelSynchronizer(com.canoo.dp.impl.client.legacy.DefaultModelSynchronizer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleExceptionHandler(com.canoo.dp.impl.client.legacy.communication.SimpleExceptionHandler) HttpURLConnectionFactory(com.canoo.platform.core.http.HttpURLConnectionFactory) JsonCodec(com.canoo.dp.impl.remoting.legacy.communication.JsonCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) CreateContextCommand(com.canoo.dp.impl.remoting.commands.CreateContextCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) CreatePresentationModelCommand(com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand) HttpClient(com.canoo.platform.core.http.HttpClient) CreatePresentationModelCommand(com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand) HeadlessToolkit(com.canoo.platform.client.HeadlessToolkit) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) Test(org.testng.annotations.Test)

Aggregations

DolphinPlatformHttpClientConnector (com.canoo.dp.impl.client.DolphinPlatformHttpClientConnector)2 ClientModelStore (com.canoo.dp.impl.client.legacy.ClientModelStore)2 DefaultModelSynchronizer (com.canoo.dp.impl.client.legacy.DefaultModelSynchronizer)2 SimpleExceptionHandler (com.canoo.dp.impl.client.legacy.communication.SimpleExceptionHandler)2 CreateContextCommand (com.canoo.dp.impl.remoting.commands.CreateContextCommand)2 Command (com.canoo.dp.impl.remoting.legacy.communication.Command)2 CreatePresentationModelCommand (com.canoo.dp.impl.remoting.legacy.communication.CreatePresentationModelCommand)2 JsonCodec (com.canoo.dp.impl.remoting.legacy.communication.JsonCodec)2 HeadlessToolkit (com.canoo.platform.client.HeadlessToolkit)2 HttpURLConnectionFactory (com.canoo.platform.core.http.HttpURLConnectionFactory)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 Test (org.testng.annotations.Test)2 HttpClientImpl (com.canoo.dp.impl.platform.client.http.HttpClientImpl)1 HttpClient (com.canoo.platform.core.http.HttpClient)1 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1