use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class HandshakeProcessorTest method handleAuthenticationException.
@Test
public void handleAuthenticationException() throws Exception {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
PowerMockito.when(InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext)).thenThrow(new APIManagementException("Error while accessing truststore"));
HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
InboundProcessorResponseDTO errorResponseDTO = new InboundProcessorResponseDTO();
errorResponseDTO.setError(true);
errorResponseDTO.setErrorCode(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR);
errorResponseDTO.setErrorMessage(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_GENERAL_MESSAGE);
errorResponseDTO.setCloseConnection(true);
PowerMockito.when(InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_GENERAL_MESSAGE)).thenReturn(errorResponseDTO);
InboundProcessorResponseDTO inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
Assert.assertTrue(inboundProcessorResponseDTO.isError());
Assert.assertNotNull(inboundProcessorResponseDTO.getErrorMessage());
Assert.assertTrue(inboundProcessorResponseDTO.isCloseConnection());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorResponseString(), errorResponseDTO.getErrorResponseString());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorCode(), errorResponseDTO.getErrorCode());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorMessage(), errorResponseDTO.getErrorMessage());
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class HandshakeProcessorTest method handleAPISecurityException.
@Test
public void handleAPISecurityException() throws Exception {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
PowerMockito.when(InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext)).thenThrow(new APISecurityException(APISecurityConstants.API_AUTH_INVALID_CREDENTIALS, APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE));
HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
InboundProcessorResponseDTO errorResponseDTO = new InboundProcessorResponseDTO();
errorResponseDTO.setError(true);
errorResponseDTO.setErrorCode(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR);
errorResponseDTO.setErrorMessage(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
errorResponseDTO.setCloseConnection(true);
PowerMockito.when(InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE)).thenReturn(errorResponseDTO);
InboundProcessorResponseDTO inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
Assert.assertTrue(inboundProcessorResponseDTO.isError());
Assert.assertNotNull(inboundProcessorResponseDTO.getErrorMessage());
Assert.assertTrue(inboundProcessorResponseDTO.isCloseConnection());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorResponseString(), errorResponseDTO.getErrorResponseString());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorCode(), errorResponseDTO.getErrorCode());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorMessage(), errorResponseDTO.getErrorMessage());
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class GraphQLResponseProcessorTest method testHandleNonSubscribeResponse.
@Test
public void testHandleNonSubscribeResponse() {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
String msgText = "{\"type\":\"connection_ack\"}";
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.authenticateToken(inboundMessageContext)).thenReturn(responseDTO);
GraphQLResponseProcessor responseProcessor = new GraphQLResponseProcessor();
InboundProcessorResponseDTO processorResponseDTO = responseProcessor.handleResponse(msgSize, msgText, inboundMessageContext);
Assert.assertFalse(processorResponseDTO.isError());
Assert.assertNull(processorResponseDTO.getErrorMessage());
Assert.assertFalse(processorResponseDTO.isCloseConnection());
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class GraphQLResponseProcessorTest method testHandleThrottleOut.
@Test
public void testHandleThrottleOut() {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
String msgText = "{\"type\":\"data\",\"id\":\"1\",\"payload\":{\"data\":" + "{\"liftStatusChange\":{\"name\":\"Astra Express\"}}}}";
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.authenticateToken(inboundMessageContext)).thenReturn(responseDTO);
VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
verbInfoDTO.setHttpVerb("SUBSCRIPTION");
verbInfoDTO.setThrottling("Unlimited");
GraphQLOperationDTO graphQLOperationDTO = new GraphQLOperationDTO(verbInfoDTO, "liftStatusChange");
inboundMessageContext.addVerbInfoForGraphQLMsgId("1", graphQLOperationDTO);
PowerMockito.when(InboundWebsocketProcessorUtil.validateScopes(inboundMessageContext, "liftStatusChange", "1")).thenReturn(responseDTO);
GraphQLProcessorResponseDTO throttleResponseDTO = new GraphQLProcessorResponseDTO();
throttleResponseDTO.setError(true);
throttleResponseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.THROTTLED_OUT_ERROR);
throttleResponseDTO.setErrorMessage(WebSocketApiConstants.FrameErrorConstants.THROTTLED_OUT_ERROR_MESSAGE);
throttleResponseDTO.setId("1");
PowerMockito.when(InboundWebsocketProcessorUtil.doThrottleForGraphQL(msgSize, verbInfoDTO, inboundMessageContext, "1")).thenReturn(throttleResponseDTO);
GraphQLResponseProcessor responseProcessor = new GraphQLResponseProcessor();
InboundProcessorResponseDTO processorResponseDTO = responseProcessor.handleResponse(msgSize, msgText, inboundMessageContext);
Assert.assertTrue(processorResponseDTO.isError());
Assert.assertNotNull(processorResponseDTO.getErrorMessage());
Assert.assertFalse(processorResponseDTO.isCloseConnection());
Assert.assertEquals(processorResponseDTO.getErrorResponseString(), throttleResponseDTO.getErrorResponseString());
Assert.assertEquals(processorResponseDTO.getErrorMessage(), throttleResponseDTO.getErrorMessage());
Assert.assertEquals(processorResponseDTO.getErrorCode(), throttleResponseDTO.getErrorCode());
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class GraphQLResponseProcessorTest method testHandleInvalidScope.
@Test
public void testHandleInvalidScope() {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
String msgText = "{\"type\":\"data\",\"id\":\"1\",\"payload\":{\"data\":" + "{\"liftStatusChange\":{\"name\":\"Astra Express\"}}}}";
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.authenticateToken(inboundMessageContext)).thenReturn(responseDTO);
VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
verbInfoDTO.setHttpVerb("SUBSCRIPTION");
verbInfoDTO.setThrottling("Unlimited");
verbInfoDTO.setAuthType("Any");
GraphQLOperationDTO graphQLOperationDTO = new GraphQLOperationDTO(verbInfoDTO, "liftStatusChange");
inboundMessageContext.addVerbInfoForGraphQLMsgId("1", graphQLOperationDTO);
GraphQLProcessorResponseDTO graphQLProcessorResponseDTO = new GraphQLProcessorResponseDTO();
graphQLProcessorResponseDTO.setError(true);
graphQLProcessorResponseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.RESOURCE_FORBIDDEN_ERROR);
graphQLProcessorResponseDTO.setErrorMessage("User is NOT authorized to access the Resource");
graphQLProcessorResponseDTO.setCloseConnection(false);
graphQLProcessorResponseDTO.setId("1");
PowerMockito.when(InboundWebsocketProcessorUtil.validateScopes(inboundMessageContext, "liftStatusChange", "1")).thenReturn(graphQLProcessorResponseDTO);
PowerMockito.when(InboundWebsocketProcessorUtil.doThrottleForGraphQL(msgSize, verbInfoDTO, inboundMessageContext, "1")).thenReturn(responseDTO);
GraphQLResponseProcessor responseProcessor = new GraphQLResponseProcessor();
InboundProcessorResponseDTO processorResponseDTO = responseProcessor.handleResponse(msgSize, msgText, inboundMessageContext);
Assert.assertTrue(processorResponseDTO.isError());
Assert.assertNotNull(processorResponseDTO.getErrorMessage());
Assert.assertFalse(processorResponseDTO.isCloseConnection());
Assert.assertEquals(processorResponseDTO.getErrorResponseString(), graphQLProcessorResponseDTO.getErrorResponseString());
Assert.assertEquals(processorResponseDTO.getErrorMessage(), graphQLProcessorResponseDTO.getErrorMessage());
Assert.assertEquals(processorResponseDTO.getErrorCode(), graphQLProcessorResponseDTO.getErrorCode());
}
Aggregations