Search in sources :

Example 1 with JoynrMessagingError

use of io.joynr.messaging.datatypes.JoynrMessagingError in project joynr by bmwcarit.

the class ControlledBounceProxyServerTest method testCreateChannelWithSessionIdOnBounceProxy.

@Test
@Ignore("Feature not implemented yet")
public void testCreateChannelWithSessionIdOnBounceProxy() throws Exception {
    RestAssured.baseURI = configuration.getAnyBounceProxyUrl();
    final String channelId = "channel-testCreateChannelWithSessionId";
    final String trackingId = "trackingId-testCreateChannelWithSessionId";
    // create channel on bounce proxy
    /* @formatter:off */
    Response responseCreateChannel = given().log().all().header(X_ATMOSPHERE_TRACKING_ID, trackingId).post("channels" + SESSIONID_APPENDIX + "sessionId12345?ccid=" + channelId);
    /* @formatter:on */
    assertEquals(400, /* Bad Request */
    responseCreateChannel.getStatusCode());
    String body = responseCreateChannel.getBody().asString();
    JoynrMessagingError error = objectMapper.readValue(body, JoynrMessagingError.class);
    assertNotNull(error);
    JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
    assertNotNull(joynrMessagingErrorCode);
    assertEquals(JoynrMessagingErrorCode.JOYNRMESSAGINGERROR_SESSIONIDSET, joynrMessagingErrorCode);
}
Also used : Response(com.jayway.restassured.response.Response) JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) JoynrMessagingErrorCode(io.joynr.messaging.datatypes.JoynrMessagingErrorCode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with JoynrMessagingError

use of io.joynr.messaging.datatypes.JoynrMessagingError in project joynr by bmwcarit.

the class MessagingTest method testPostMessageExpiryDateExpired.

@Test
public void testPostMessageExpiryDateExpired() throws Exception {
    ImmutableMessage message = createJoynrMessage(ExpiryDate.fromAbsolute(1));
    byte[] serializedMessage = message.getSerializedMessage();
    // 
    Response response = // 
    given().contentType(ContentType.BINARY).when().body(serializedMessage).post(serverUrl + "/channels/channel-123/message");
    assertEquals(400, /* Bad Request */
    response.getStatusCode());
    JoynrMessagingError error = objectMapper.readValue(response.getBody().asString(), JoynrMessagingError.class);
    assertEquals(JOYNRMESSAGINGERROR_EXPIRYDATEEXPIRED.getCode(), error.getCode());
    assertEquals(JOYNRMESSAGINGERROR_EXPIRYDATEEXPIRED.getDescription() + ": 127.0.0.1", error.getReason());
    Mockito.verify(mock, Mockito.never()).passMessageToReceiver("channel-123", serializedMessage);
}
Also used : JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) ImmutableMessage(joynr.ImmutableMessage) Test(org.junit.Test)

Example 3 with JoynrMessagingError

use of io.joynr.messaging.datatypes.JoynrMessagingError in project joynr by bmwcarit.

the class MessagingTest method testPostMessageWithoutExpiryDate.

// @Test
// public void testPostMessageWithMissingChannelId() throws
// JsonProcessingException {
// 
// String serializedMessage = createJoynrMessage();
// 
// // TODO how is the call in this case???
// Response response = //
// given(). //
// contentType(ContentType.JSON).
// when().body(serializedMessage).
// post(serverUrl + "/channels/null/message");
// 
// assertEquals(201 /* Created */, response.getStatusCode());
// 
// }
@Test
public void testPostMessageWithoutExpiryDate() throws Exception {
    ImmutableMessage message = createJoynrMessage(ExpiryDate.fromAbsolute(0));
    byte[] serializedMessage = message.getSerializedMessage();
    // 
    Response response = // 
    given().contentType(ContentType.BINARY).when().body(serializedMessage).post(serverUrl + "/channels/channel-123/message");
    assertEquals(400, /* Bad Request */
    response.getStatusCode());
    JoynrMessagingError error = objectMapper.readValue(response.getBody().asString(), JoynrMessagingError.class);
    assertEquals(JOYNRMESSAGINGERROR_EXPIRYDATENOTSET.getCode(), error.getCode());
    assertEquals(JOYNRMESSAGINGERROR_EXPIRYDATENOTSET.getDescription(), error.getReason());
    Mockito.verify(mock, Mockito.never()).passMessageToReceiver("channel-123", serializedMessage);
}
Also used : JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) ImmutableMessage(joynr.ImmutableMessage) Test(org.junit.Test)

Example 4 with JoynrMessagingError

use of io.joynr.messaging.datatypes.JoynrMessagingError in project joynr by bmwcarit.

the class AbstractBounceProxyServerTest method testPostMessageToNonExistingChannel.

@Test
public void testPostMessageToNonExistingChannel() throws Exception {
    byte[] serializedMessage = bpMock.createImmutableMessage(100000l, "some-payload".getBytes(Charsets.UTF_8)).getSerializedMessage();
    /* @formatter:off */
    Response postMessageResponse = bpMock.onrequest().with().body(serializedMessage).when().post("/channels/non-existing-channel/message/");
    /* @formatter:on */
    assertEquals(400, /* Bad Request */
    postMessageResponse.getStatusCode());
    String body = postMessageResponse.getBody().asString();
    JoynrMessagingError error = objectMapper.readValue(body, JoynrMessagingError.class);
    assertNotNull(error);
    JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
    assertNotNull(joynrMessagingErrorCode);
    assertEquals(JoynrMessagingErrorCode.JOYNRMESSAGINGERROR_CHANNELNOTFOUND, joynrMessagingErrorCode);
}
Also used : Response(com.jayway.restassured.response.Response) JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) JoynrMessagingErrorCode(io.joynr.messaging.datatypes.JoynrMessagingErrorCode) Test(org.junit.Test)

Example 5 with JoynrMessagingError

use of io.joynr.messaging.datatypes.JoynrMessagingError in project joynr by bmwcarit.

the class HttpMessageSender method sendMessage.

public void sendMessage(ChannelAddress address, byte[] serializedMessage, SuccessAction successAction, FailureAction failureAction) {
    // check if messageReceiver is ready to receive replies otherwise delay request by at least 100 ms
    if (!messageReceiver.isReady()) {
        long delay_ms = DELAY_RECEIVER_NOT_STARTED_MS;
        failureAction.execute(new JoynrDelayMessageException(delay_ms, RECEIVER_NOT_STARTED_REASON));
    }
    String sendUrl = urlResolver.getSendUrl(address.getMessagingEndpointUrl());
    logger.trace("SENDING: channelId: {} message: {}", sendUrl, serializedMessage);
    HttpContext context = new BasicHttpContext();
    // execute http command to send
    CloseableHttpResponse response = null;
    try {
        HttpPost httpPost = httpRequestFactory.createHttpPost(URI.create(sendUrl));
        httpPost.addHeader(new BasicHeader(httpConstants.getHEADER_CONTENT_TYPE(), httpConstants.getAPPLICATION_JSON() + ";charset=UTF-8"));
        httpPost.setEntity(new ByteArrayEntity(serializedMessage));
        // Clone the default config
        Builder requestConfigBuilder = RequestConfig.copy(defaultRequestConfig);
        requestConfigBuilder.setConnectionRequestTimeout(httpConstants.getSEND_MESSAGE_REQUEST_TIMEOUT());
        httpPost.setConfig(requestConfigBuilder.build());
        response = httpclient.execute(httpPost, context);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        String statusText = statusLine.getReasonPhrase();
        switch(statusCode) {
            case HttpURLConnection.HTTP_OK:
            case HttpURLConnection.HTTP_CREATED:
                logger.trace("SENT: channelId {} message: {}", sendUrl, serializedMessage);
                successAction.execute();
                break;
            case HttpURLConnection.HTTP_BAD_REQUEST:
                HttpEntity entity = response.getEntity();
                if (entity == null) {
                    failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender. No further reason found in message body"));
                    return;
                }
                String body = EntityUtils.toString(entity, "UTF-8");
                JoynrMessagingError error = objectMapper.readValue(body, JoynrMessagingError.class);
                JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
                logger.error(error.toString());
                switch(joynrMessagingErrorCode) {
                    case JOYNRMESSAGINGERROR_CHANNELNOTFOUND:
                        failureAction.execute(new JoynrChannelMissingException("Channel does not exist. Status: " + statusCode + " error: " + error.getCode() + "reason:" + error.getReason()));
                        break;
                    default:
                        failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender: " + statusText + body + " error: " + error.getCode() + "reason:" + error.getReason()));
                        break;
                }
                break;
            default:
                failureAction.execute(new JoynrCommunicationException("Unknown Error in HttpMessageSender: " + statusText + " statusCode: " + statusCode));
                break;
        }
    } catch (JoynrShutdownException e) {
        failureAction.execute(new JoynrMessageNotSentException("Message not sent to: " + address, e));
    } catch (Exception e) {
        // An exception occured - this could still be a communication error (e.g Connection refused)
        failureAction.execute(new JoynrCommunicationException(e.getClass().getName() + "Exception while communicating. error: " + e.getMessage()));
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : HttpPost(io.joynr.messaging.http.operation.HttpPost) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) Builder(org.apache.http.client.config.RequestConfig.Builder) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) JoynrMessagingErrorCode(io.joynr.messaging.datatypes.JoynrMessagingErrorCode) IOException(java.io.IOException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) IOException(java.io.IOException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) StatusLine(org.apache.http.StatusLine) JoynrChannelMissingException(io.joynr.exceptions.JoynrChannelMissingException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrMessagingError(io.joynr.messaging.datatypes.JoynrMessagingError) JoynrShutdownException(io.joynr.exceptions.JoynrShutdownException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader) JoynrMessageNotSentException(io.joynr.exceptions.JoynrMessageNotSentException)

Aggregations

JoynrMessagingError (io.joynr.messaging.datatypes.JoynrMessagingError)6 JoynrMessagingErrorCode (io.joynr.messaging.datatypes.JoynrMessagingErrorCode)4 Test (org.junit.Test)4 Response (com.jayway.restassured.response.Response)2 JoynrChannelMissingException (io.joynr.exceptions.JoynrChannelMissingException)2 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)2 JoynrShutdownException (io.joynr.exceptions.JoynrShutdownException)2 IOException (java.io.IOException)2 ImmutableMessage (joynr.ImmutableMessage)2 HttpEntity (org.apache.http.HttpEntity)2 JoynrDelayMessageException (io.joynr.exceptions.JoynrDelayMessageException)1 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)1 HttpPost (io.joynr.messaging.http.operation.HttpPost)1 EncodingException (io.joynr.smrf.EncodingException)1 UnsuppportedVersionException (io.joynr.smrf.UnsuppportedVersionException)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 ResponseHandler (org.apache.http.client.ResponseHandler)1 Builder (org.apache.http.client.config.RequestConfig.Builder)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1