JdbcEndpointAdapterController implements JdbcController, EndpointAdapter { @Override public Message handleMessage(Message request) { if (request.getPayload() instanceof Operation) { StringResult result = new StringResult(); endpointConfiguration.getMarshaller().marshal(request.getPayload(Operation.class), result); request.setPayload(result.toString()); } if (log.isDebugEnabled()) { log.debug(String.format("Received request on server: '%s':%n%s", endpointConfiguration.getServerConfiguration().getDatabaseName(), request.getPayload(String.class))); } if (request.getPayload(Operation.class) != null) { String sqlQuery = Optional.ofNullable(request.getPayload(Operation.class).getExecute()) .map(Execute::getStatement) .map(Execute.Statement::getSql) .orElse(""); if (autoHandleQueryPattern.matcher(sqlQuery).find()) { log.debug(String.format("Auto handle query '%s' with positive response", sqlQuery)); JdbcMessage defaultResponse = JdbcMessage.success().rowsUpdated(0); defaultResponse.setType(MessageType.XML); return defaultResponse; } } return Optional.ofNullable(delegate.handleMessage(request)) .orElse(JdbcMessage.success()); } JdbcEndpointAdapterController(
            JdbcEndpointConfiguration endpointConfiguration,
            EndpointAdapter delegate);   JdbcEndpointAdapterController(
            JdbcEndpointConfiguration endpointConfiguration,
            EndpointAdapter delegate,
            DataSetCreator dataSetCreator); @Override Message handleMessage(Message request); @Override void openConnection(Map<String, String> properties); @Override void closeConnection(); @Override void createPreparedStatement(String stmt); @Override void createStatement(); @Override DataSet executeQuery(String query); @Override DataSet executeStatement(String stmt); @Override int executeUpdate(String updateSql); @Override void closeStatement(); @Override void setTransactionState(boolean transactionState); @Override boolean getTransactionState(); @Override void commitStatements(); @Override void rollbackStatements(); @Override void createCallableStatement(String sql); @Override Endpoint getEndpoint(); @Override EndpointConfiguration getEndpointConfiguration();  }
@Test public void testHandleMessage(){ final Message request = mock(Message.class); final Message expectedResponse = mock(Message.class); when(endpointAdapter.handleMessage(request)).thenReturn(expectedResponse); final Message response = jdbcEndpointAdapterController.handleMessage(request); assertEquals(response, expectedResponse); }
@Test public void testHandleMessageOperationPayloadConversion(){ final Message request = mock(Message.class); final Operation payload = mock(Operation.class); when(request.getPayload()).thenReturn(payload); when(request.getPayload(Operation.class)).thenReturn(payload); final JdbcMarshaller jdbcMarshallerMock = mock(JdbcMarshaller.class); when(jdbcEndpointConfiguration.getMarshaller()).thenReturn(jdbcMarshallerMock); jdbcEndpointAdapterController.handleMessage(request); verify(jdbcMarshallerMock).marshal(eq(payload), any(StringResult.class)); verify(request).setPayload(anyString()); }
@Test public void testHandleMessageWithAutoHandleQueriesEmptyOperation(){ final Message request = mock(Message.class); when(request.getPayload(Operation.class)).thenReturn(null); final Message expectedResponse = mock(Message.class);; when(endpointAdapter.handleMessage(request)).thenReturn(expectedResponse); final Message response = jdbcEndpointAdapterController.handleMessage(request); assertEquals(response.getPayload(), expectedResponse.getPayload()); verify(endpointAdapter, times(1)).handleMessage(request); }
@Test public void testHandleMessageWithAutoHandleQueriesQueries(){ final Message request = mock(Message.class); Operation operation = new Operation(); operation.setExecute(new Execute()); operation.getExecute().setStatement(new Execute.Statement()); operation.getExecute().getStatement().setSql("SELECT 1"); when(request.getPayload(Operation.class)).thenReturn(operation); final Message response = jdbcEndpointAdapterController.handleMessage(request); assertTrue(JdbcMessage.class.isAssignableFrom(response.getClass())); JdbcMessage jdbcMessageResponse = (JdbcMessage) response; OperationResult operationResult = jdbcMessageResponse.getPayload(OperationResult.class); assertTrue(operationResult.isSuccess()); assertEquals(operationResult.getDataSet(), null); assertEquals(operationResult.getAffectedRows(), new Integer(0)); verify(endpointAdapter, times(0)).handleMessage(request); }
@Test(dataProvider = "systemQueries") public void match(String query, boolean isMatching) { reset(endpointAdapter); final Message request = mock(Message.class); Operation operation = new Operation(); operation.setExecute(new Execute()); operation.getExecute().setStatement(new Execute.Statement()); operation.getExecute().getStatement().setSql(query); when(request.getPayload(Operation.class)).thenReturn(operation); final Message response = jdbcEndpointAdapterController.handleMessage(request); assertTrue(JdbcMessage.class.isAssignableFrom(response.getClass())); JdbcMessage jdbcMessageResponse = (JdbcMessage) response; OperationResult operationResult = jdbcMessageResponse.getPayload(OperationResult.class); assertTrue(operationResult.isSuccess()); assertEquals(operationResult.getDataSet(), null); assertEquals(operationResult.getAffectedRows(), isMatching ? 0 : null); verify(endpointAdapter, times(isMatching ? 0 : 1)).handleMessage(any()); }
@Test(dataProvider = "systemQueriesOverwrite") public void matchUsingSystemProperty(String query, boolean isMatching) { reset(endpointAdapter); System.setProperty(JdbcEndpointAdapterController.AUTO_HANDLE_QUERY_PROPERTY, "select 1;;"); JdbcEndpointAdapterController jdbcEndpointAdapterController = new JdbcEndpointAdapterController(jdbcEndpointConfiguration, endpointAdapter); final Message request = mock(Message.class); Operation operation = new Operation(); operation.setExecute(new Execute()); operation.getExecute().setStatement(new Execute.Statement()); operation.getExecute().getStatement().setSql(query); when(request.getPayload(Operation.class)).thenReturn(operation); final Message response = jdbcEndpointAdapterController.handleMessage(request); assertTrue(JdbcMessage.class.isAssignableFrom(response.getClass())); JdbcMessage jdbcMessageResponse = (JdbcMessage) response; OperationResult operationResult = jdbcMessageResponse.getPayload(OperationResult.class); assertTrue(operationResult.isSuccess()); assertEquals(operationResult.getDataSet(), null); assertEquals(operationResult.getAffectedRows(), isMatching ? 0 : null); verify(endpointAdapter, times(isMatching ? 0 : 1)).handleMessage(any()); }