Best Citrus code snippet using com.consol.citrus.jdbc.server.JdbcEndpointAdapterController.handleMessage
Source:JdbcEndpointAdapterControllerTest.java
...49 public void testHandleMessage(){50 //GIVEN51 final Message request = mock(Message.class);52 final Message expectedResponse = mock(Message.class);53 when(endpointAdapter.handleMessage(request)).thenReturn(expectedResponse);54 //WHEN55 final Message response = jdbcEndpointAdapterController.handleMessage(request);56 //THEN57 assertEquals(response, expectedResponse);58 }59 @Test60 public void testHandleMessageWhenEndpointAdapterReturnsNull(){61 //GIVEN62 final Message request = mock(Message.class);63 final Message expectedResponse = JdbcMessage.success();64 when(endpointAdapter.handleMessage(request)).thenReturn(null);65 //WHEN66 final Message response = jdbcEndpointAdapterController.handleMessage(request);67 //THEN68 assertEquals(response.getPayload(), expectedResponse.getPayload());69 }70 @Test71 public void testHandleMessageOperationPayloadConversion(){72 //GIVEN73 final Message request = mock(Message.class);74 final Operation payload = mock(Operation.class);75 when(request.getPayload()).thenReturn(payload);76 when(request.getPayload(Operation.class)).thenReturn(payload);77 final JdbcMarshaller jdbcMarshallerMock = mock(JdbcMarshaller.class);78 when(jdbcEndpointConfiguration.getMarshaller()).thenReturn(jdbcMarshallerMock);79 //WHEN80 jdbcEndpointAdapterController.handleMessage(request);81 //THEN82 verify(jdbcMarshallerMock).marshal(eq(payload), any(StringResult.class));83 verify(request).setPayload(anyString());84 }85 @Test86 public void testOpenConnection(){87 //GIVEN88 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);89 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(true);90 jdbcEndpointAdapterController.getConnections().set(0);91 //WHEN92 final int before = jdbcEndpointAdapterController.getConnections().get();93 jdbcEndpointAdapterController.openConnection(new HashMap<>());94 final int after = jdbcEndpointAdapterController.getConnections().get();95 //THEN96 assertEquals(before + 1, after);97 verify(jdbcEndpointAdapterController, never()).handleMessage(any());98 }99 @Test100 public void testOpenConnectionWithoutAutoConnect(){101 //GIVEN102 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);103 jdbcEndpointAdapterController.getConnections().set(0);104 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(false);105 //WHEN106 final int before = jdbcEndpointAdapterController.getConnections().get();107 jdbcEndpointAdapterController.openConnection(new HashMap<>());108 final int after = jdbcEndpointAdapterController.getConnections().get();109 //THEN110 verify(jdbcEndpointAdapterController).handleMessage(any());111 assertEquals(before + 1, after);112 }113 @Test(expectedExceptions = JdbcServerException.class)114 public void testOpenConnectionWithoutAutoConnectAndInvalidProperties(){115 //GIVEN116 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(false);117 jdbcEndpointAdapterController.getConnections().set(0);118 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);119 final Message errorMessage = mock(Message.class);120 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");121 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());122 //WHEN123 jdbcEndpointAdapterController.openConnection(new HashMap<>());124 //THEN125 //Exception is Thrown126 }127 @Test(expectedExceptions = JdbcServerException.class)128 public void testOpenConnectionMaximumConnectionsReached(){129 //GIVEN130 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(true);131 jdbcEndpointAdapterController.getConnections().set(1);132 //WHEN133 jdbcEndpointAdapterController.openConnection(new HashMap<>());134 //THEN135 //Exception is Thrown136 }137 @Test138 public void testCloseConnection(){139 //GIVEN140 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);141 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(true);142 jdbcEndpointAdapterController.getConnections().set(1);143 //WHEN144 final int before = jdbcEndpointAdapterController.getConnections().get();145 jdbcEndpointAdapterController.closeConnection();146 final int after = jdbcEndpointAdapterController.getConnections().get();147 //THEN148 verify(jdbcEndpointAdapterController, never()).handleMessage(any());149 assertEquals(before -1 , after);150 }151 @Test152 public void testCloseConnectionWithoutAutoConnect(){153 //GIVEN154 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);155 jdbcEndpointAdapterController.getConnections().set(1);156 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(false);157 //WHEN158 final int before = jdbcEndpointAdapterController.getConnections().get();159 jdbcEndpointAdapterController.closeConnection();160 final int after = jdbcEndpointAdapterController.getConnections().get();161 //THEN162 verify(jdbcEndpointAdapterController).handleMessage(any());163 assertEquals(before - 1, after);164 }165 @Test(expectedExceptions = JdbcServerException.class)166 public void testErroneousCloseConnection(){167 //GIVEN168 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(false);169 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);170 final Message errorMessage = mock(Message.class);171 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");172 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());173 //WHEN174 jdbcEndpointAdapterController.closeConnection();175 //THEN176 //Exception is Thrown177 }178 @Test179 public void testCloseConnectionWithoutOpenConnectionIsSuccessful(){180 //GIVEN181 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);182 when(jdbcEndpointConfiguration.isAutoConnect()).thenReturn(true);183 jdbcEndpointAdapterController.getConnections().set(0);184 //WHEN185 jdbcEndpointAdapterController.closeConnection();186 //THEN187 verify(jdbcEndpointAdapterController, never()).handleMessage(any());188 assertEquals(jdbcEndpointAdapterController.getConnections().get(), 0);189 }190 @Test191 public void testCreatePreparedStatementWithAutoCreateStatement(){192 //GIVEN193 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);194 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(true);195 //WHEN196 jdbcEndpointAdapterController.createPreparedStatement("some statement");197 //THEN198 verify(jdbcEndpointAdapterController, never()).handleMessage(any());199 }200 @Test201 public void testCreatePreparedStatementWithoutAutoCreateStatement(){202 //GIVEN203 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);204 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);205 //WHEN206 jdbcEndpointAdapterController.createPreparedStatement("some statement");207 //THEN208 verify(jdbcEndpointAdapterController).handleMessage(any());209 }210 @Test(expectedExceptions = JdbcServerException.class)211 public void testCreatePreparedStatementWithoutAutoCreateStatementAndFailure(){212 //GIVEN213 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);214 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);215 final Message errorMessage = mock(Message.class);216 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");217 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());218 //WHEN219 jdbcEndpointAdapterController.createPreparedStatement("some statement");220 //THEN221 //Exception is thrown222 }223 @Test224 public void testCreateStatementWithAutoCreateStatement(){225 //GIVEN226 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);227 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(true);228 //WHEN229 jdbcEndpointAdapterController.createStatement();230 //THEN231 verify(jdbcEndpointAdapterController, never()).handleMessage(any());232 }233 @Test234 public void testCreateStatementWithoutAutoCreateStatement(){235 //GIVEN236 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);237 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);238 //WHEN239 jdbcEndpointAdapterController.createStatement();240 //THEN241 verify(jdbcEndpointAdapterController).handleMessage(any());242 }243 @Test(expectedExceptions = JdbcServerException.class)244 public void testCreateStatementWithoutAutoCreateStatementAndFailure(){245 //GIVEN246 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);247 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);248 final Message errorMessage = mock(Message.class);249 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");250 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());251 //WHEN252 jdbcEndpointAdapterController.createStatement();253 //THEN254 //Exception is thrown255 }256 @Test257 public void testExecuteQuery(){258 //GIVEN259 final DataSet expectedDataSet = mock(DataSet.class);260 final DataSetCreator dataSetCreator = mock(DataSetCreator.class);261 when(dataSetCreator.createDataSet(any(), any())).thenReturn(expectedDataSet);262 final JdbcEndpointAdapterController jdbcEndpointAdapterController =263 spy(new JdbcEndpointAdapterController(jdbcEndpointConfiguration, endpointAdapter, dataSetCreator));264 final Message messageToMarshal = mock(Message.class);265 when(messageToMarshal.getHeader(MessageHeaders.MESSAGE_TYPE)).thenReturn(MessageType.JSON.toString());266 doReturn(messageToMarshal).when(jdbcEndpointAdapterController).handleMessage(any());267 final String query = "some query";268 //WHEN269 final DataSet dataSet = jdbcEndpointAdapterController.executeQuery(query);270 //THEN271 verify(jdbcEndpointAdapterController).handleMessage(any());272 verify(dataSetCreator).createDataSet(messageToMarshal, MessageType.JSON);273 assertEquals(dataSet, expectedDataSet);274 }275 @Test(expectedExceptions = JdbcServerException.class)276 public void testExecuteQueryForwardsException(){277 //GIVEN278 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);279 final Message errorMessage = mock(Message.class);280 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");281 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());282 final String query = "some query";283 //WHEN284 jdbcEndpointAdapterController.executeQuery(query);285 //THEN286 //Exception is thrown287 }288 @Test289 public void testExecute(){290 //GIVEN291 final DataSet expectedDataSet = mock(DataSet.class);292 final DataSetCreator dataSetCreator = mock(DataSetCreator.class);293 when(dataSetCreator.createDataSet(any(), any())).thenReturn(expectedDataSet);294 final JdbcEndpointAdapterController jdbcEndpointAdapterController =295 spy(new JdbcEndpointAdapterController(jdbcEndpointConfiguration, endpointAdapter, dataSetCreator));296 final Message messageToMarshal = mock(Message.class);297 when(messageToMarshal.getHeader(MessageHeaders.MESSAGE_TYPE)).thenReturn(MessageType.JSON.toString());298 doReturn(messageToMarshal).when(jdbcEndpointAdapterController).handleMessage(any());299 //WHEN300 jdbcEndpointAdapterController.executeStatement("statement");301 //THEN302 verify(jdbcEndpointAdapterController).handleMessage(any());303 }304 @Test(expectedExceptions = JdbcServerException.class)305 public void testExecuteWithFailure(){306 //GIVEN307 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);308 final Message errorMessage = mock(Message.class);309 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");310 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());311 //WHEN312 jdbcEndpointAdapterController.executeStatement("statement");313 //THEN314 //Exception is thrown315 }316 @Test317 public void testExecuteUpdate(){318 //GIVEN319 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);320 final Message errorMessage = mock(Message.class);321 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_ROWS_UPDATED)).thenReturn("2");322 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());323 //WHEN324 final int rowsUpdated = jdbcEndpointAdapterController.executeUpdate("statement");325 //THEN326 verify(jdbcEndpointAdapterController).handleMessage(any());327 assertEquals(rowsUpdated, 2);328 }329 @Test(expectedExceptions = JdbcServerException.class)330 public void testExecuteUpdateWithFailure(){331 //GIVEN332 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);333 final Message errorMessage = mock(Message.class);334 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");335 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());336 //WHEN337 jdbcEndpointAdapterController.executeUpdate("statement");338 //THEN339 //Exception is thrown340 }341 @Test342 public void testCloseStatementWithAutoCreateStatement(){343 //GIVEN344 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);345 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(true);346 //WHEN347 jdbcEndpointAdapterController.closeStatement();348 //THEN349 verify(jdbcEndpointAdapterController, never()).handleMessage(any());350 }351 @Test352 public void testCloseStatementWithoutAutoCreateStatement(){353 //GIVEN354 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);355 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);356 //WHEN357 jdbcEndpointAdapterController.closeStatement();358 //THEN359 verify(jdbcEndpointAdapterController).handleMessage(any());360 }361 @Test(expectedExceptions = JdbcServerException.class)362 public void testCloseStatementWithoutAutoCreateStatementAndFailure(){363 //GIVEN364 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);365 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);366 final Message errorMessage = mock(Message.class);367 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");368 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());369 //WHEN370 jdbcEndpointAdapterController.closeStatement();371 //THEN372 //Exception is thrown373 }374 @Test375 public void testSetTransactionState(){376 //GIVEN377 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);378 final boolean expectedBoolean = new Random().nextBoolean();379 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(true);380 //WHEN381 jdbcEndpointAdapterController.setTransactionState(expectedBoolean);382 //THEN383 verify(jdbcEndpointAdapterController, never()).handleMessage(any());384 assertEquals(jdbcEndpointAdapterController.getTransactionState(), expectedBoolean);385 }386 @Test387 public void testSetTransactionStateWithoutAutoTransactionHandling(){388 //GIVEN389 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);390 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(false);391 //WHEN392 jdbcEndpointAdapterController.setTransactionState(true);393 //THEN394 verify(jdbcEndpointAdapterController).handleMessage(any());395 }396 @Test397 public void testSetTransactionStateVerifyMessageOnlyIfTransactionHasBeenStarted(){398 //GIVEN399 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);400 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(false);401 //WHEN402 jdbcEndpointAdapterController.setTransactionState(false);403 //THEN404 verify(jdbcEndpointAdapterController, never()).handleMessage(any());405 }406 @Test407 public void testCommitStatementsWithAutoCreateStatement(){408 //GIVEN409 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);410 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(true);411 //WHEN412 jdbcEndpointAdapterController.commitStatements();413 //THEN414 verify(jdbcEndpointAdapterController, never()).handleMessage(any());415 }416 @Test417 public void testCommitStatementsWithoutAutoCreateStatement(){418 //GIVEN419 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);420 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(false);421 //WHEN422 jdbcEndpointAdapterController.commitStatements();423 //THEN424 verify(jdbcEndpointAdapterController).handleMessage(any());425 }426 @Test(expectedExceptions = JdbcServerException.class)427 public void testCommitStatementsWithoutAutoCreateStatementAndFailure(){428 //GIVEN429 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);430 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(false);431 final Message errorMessage = mock(Message.class);432 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");433 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());434 //WHEN435 jdbcEndpointAdapterController.commitStatements();436 //THEN437 //Exception is thrown438 }439 @Test440 public void testRollbackStatementsWithAutoCreateStatement(){441 //GIVEN442 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);443 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(true);444 //WHEN445 jdbcEndpointAdapterController.rollbackStatements();446 //THEN447 verify(jdbcEndpointAdapterController, never()).handleMessage(any());448 }449 @Test450 public void testRollbackStatementsWithoutAutoCreateStatement(){451 //GIVEN452 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);453 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(false);454 //WHEN455 jdbcEndpointAdapterController.rollbackStatements();456 //THEN457 verify(jdbcEndpointAdapterController).handleMessage(any());458 }459 @Test(expectedExceptions = JdbcServerException.class)460 public void testRollbackStatementsWithoutAutoCreateStatementAndFailure(){461 //GIVEN462 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);463 when(jdbcEndpointConfiguration.isAutoTransactionHandling()).thenReturn(false);464 final Message errorMessage = mock(Message.class);465 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");466 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());467 //WHEN468 jdbcEndpointAdapterController.rollbackStatements();469 //THEN470 //Exception is thrown471 }472 @Test473 public void testCreateCallableStatementWithAutoCreateStatement(){474 //GIVEN475 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);476 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(true);477 //WHEN478 jdbcEndpointAdapterController.createCallableStatement("some statement");479 //THEN480 verify(jdbcEndpointAdapterController, never()).handleMessage(any());481 }482 @Test483 public void testCreateCallableStatementWithoutAutoCreateStatement(){484 //GIVEN485 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);486 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);487 //WHEN488 jdbcEndpointAdapterController.createCallableStatement("some statement");489 //THEN490 verify(jdbcEndpointAdapterController).handleMessage(any());491 }492 @Test(expectedExceptions = JdbcServerException.class)493 public void testCreateCallableStatementWithoutAutoCreateStatementAndFailure(){494 //GIVEN495 final JdbcEndpointAdapterController jdbcEndpointAdapterController = spy(this.jdbcEndpointAdapterController);496 when(jdbcEndpointConfiguration.isAutoCreateStatement()).thenReturn(false);497 final Message errorMessage = mock(Message.class);498 when(errorMessage.getHeader(JdbcMessageHeaders.JDBC_SERVER_SUCCESS)).thenReturn("false");499 doReturn(errorMessage).when(jdbcEndpointAdapterController).handleMessage(any());500 //WHEN501 jdbcEndpointAdapterController.createCallableStatement("some statement");502 //THEN503 //Exception is thrown504 }505 @Test506 public void testHandleMessageWithAutoHandleQueriesEmptyOperation(){507 //GIVEN508 final Message request = mock(Message.class);509 when(request.getPayload(Operation.class)).thenReturn(null);510 final Message expectedResponse = mock(Message.class);;511 when(endpointAdapter.handleMessage(request)).thenReturn(expectedResponse);512 //WHEN513 final Message response = jdbcEndpointAdapterController.handleMessage(request);514 //THEN515 assertEquals(response.getPayload(), expectedResponse.getPayload());516 verify(endpointAdapter, times(1)).handleMessage(request);517 }518 @Test519 public void testHandleMessageWithAutoHandleQueriesQueries(){520 //GIVEN521 final Message request = mock(Message.class);522 Operation operation = new Operation();523 operation.setExecute(new Execute());524 operation.getExecute().setStatement(new Execute.Statement());525 operation.getExecute().getStatement().setSql("SELECT 1");526 when(request.getPayload(Operation.class)).thenReturn(operation);527 //WHEN528 final Message response = jdbcEndpointAdapterController.handleMessage(request);529 //THEN530 assertTrue(JdbcMessage.class.isAssignableFrom(response.getClass()));531 JdbcMessage jdbcMessageResponse = (JdbcMessage) response;532 OperationResult operationResult = jdbcMessageResponse.getPayload(OperationResult.class);533 assertTrue(operationResult.isSuccess());534 assertEquals(operationResult.getDataSet(), null);535 assertEquals(operationResult.getAffectedRows(), new Integer(0));536 verify(endpointAdapter, times(0)).handleMessage(request);537 }538 @DataProvider539 public Object[][] systemQueries() {540 return new Object[][] {541 {"Select 1", true},542 {"Select 1 from", false},543 {"SELECT USER", true},544 {"SELECT USER from DUAL", true},545 {"SELECT 1 from DUAL", true},546 {"SELECT USER FROM SYSIBM.SYSDUMMY1", true},547 {"SELECT 1 FROM SYSIBM.SYSDUMMY1", true},548 {"SELECT 1 FROM SYSIBM.SYSDUMMY1 where", false},549 };550 }551 @Test(dataProvider = "systemQueries")552 public void match(String query, boolean isMatching) {553 reset(endpointAdapter);554 //GIVEN555 final Message request = mock(Message.class);556 Operation operation = new Operation();557 operation.setExecute(new Execute());558 operation.getExecute().setStatement(new Execute.Statement());559 operation.getExecute().getStatement().setSql(query);560 when(request.getPayload(Operation.class)).thenReturn(operation);561 //WHEN562 final Message response = jdbcEndpointAdapterController.handleMessage(request);563 //THEN564 assertTrue(JdbcMessage.class.isAssignableFrom(response.getClass()));565 JdbcMessage jdbcMessageResponse = (JdbcMessage) response;566 OperationResult operationResult = jdbcMessageResponse.getPayload(OperationResult.class);567 assertTrue(operationResult.isSuccess());568 assertEquals(operationResult.getDataSet(), null);569 assertEquals(operationResult.getAffectedRows(), isMatching ? 0 : null);570 571 verify(endpointAdapter, times(isMatching ? 0 : 1)).handleMessage(any());572 }573 @DataProvider574 public Object[][] systemQueriesOverwrite() {575 return new Object[][] {576 {"Select 1", true},577 {"Select 1 from", false},578 {"SELECT USER", false},579 {"SELECT 1", true},580 };581 }582 @Test(dataProvider = "systemQueriesOverwrite")583 public void matchUsingSystemProperty(String query, boolean isMatching) {584 reset(endpointAdapter);585 System.setProperty(JdbcEndpointAdapterController.AUTO_HANDLE_QUERY_PROPERTY, "select 1;;");586 JdbcEndpointAdapterController jdbcEndpointAdapterController =587 new JdbcEndpointAdapterController(jdbcEndpointConfiguration, endpointAdapter);588 //GIVEN589 final Message request = mock(Message.class);590 Operation operation = new Operation();591 operation.setExecute(new Execute());592 operation.getExecute().setStatement(new Execute.Statement());593 operation.getExecute().getStatement().setSql(query);594 when(request.getPayload(Operation.class)).thenReturn(operation);595 //WHEN596 final Message response = jdbcEndpointAdapterController.handleMessage(request);597 //THEN598 assertTrue(JdbcMessage.class.isAssignableFrom(response.getClass()));599 JdbcMessage jdbcMessageResponse = (JdbcMessage) response;600 OperationResult operationResult = jdbcMessageResponse.getPayload(OperationResult.class);601 assertTrue(operationResult.isSuccess());602 assertEquals(operationResult.getDataSet(), null);603 assertEquals(operationResult.getAffectedRows(), isMatching ? 0 : null);604 verify(endpointAdapter, times(isMatching ? 0 : 1)).handleMessage(any());605 }606}...
Source:JdbcEndpointAdapterController.java
...79 .collect(Collectors.toList());80 autoHandleQueryPattern = Pattern.compile(String.join("|", autoQueryPatterns));81 }82 @Override83 public Message handleMessage(Message request) {84 if (request.getPayload() instanceof Operation) {85 StringResult result = new StringResult();86 endpointConfiguration.getMarshaller().marshal(request.getPayload(Operation.class), result);87 request.setPayload(result.toString());88 }89 if (log.isDebugEnabled()) {90 log.debug(String.format("Received request on server: '%s':%n%s",91 endpointConfiguration.getServerConfiguration().getDatabaseName(),92 request.getPayload(String.class)));93 }94 if (request.getPayload(Operation.class) != null) {95 String sqlQuery = Optional.ofNullable(request.getPayload(Operation.class).getExecute())96 .map(Execute::getStatement)97 .map(Execute.Statement::getSql)98 .orElse("");99 if (autoHandleQueryPattern.matcher(sqlQuery).find()) {100 log.debug(String.format("Auto handle query '%s' with positive response", sqlQuery));101 JdbcMessage defaultResponse = JdbcMessage.success().rowsUpdated(0);102 defaultResponse.setHeader(MessageHeaders.MESSAGE_TYPE, MessageType.XML.name());103 return defaultResponse;104 }105 }106 return Optional.ofNullable(delegate.handleMessage(request))107 .orElse(JdbcMessage.success());108 }109 /**110 * Opens the connection with the given properties111 * @param properties The properties to open the connection with112 * @throws JdbcServerException In case that the maximum connections have been reached113 */114 @Override115 public void openConnection(Map<String, String> properties) throws JdbcServerException {116 if (!endpointConfiguration.isAutoConnect()) {117 List<OpenConnection.Property> propertyList = convertToPropertyList(properties);118 handleMessageAndCheckResponse(JdbcMessage.openConnection(propertyList));119 }120 if (connections.get() == endpointConfiguration.getServerConfiguration().getMaxConnections()) {121 throw new JdbcServerException(String.format("Maximum number of connections (%s) reached",122 endpointConfiguration.getServerConfiguration().getMaxConnections()));123 }124 connections.incrementAndGet();125 }126 /**127 * Closes the connection128 * @throws JdbcServerException In case that the connection could not be closed129 */130 @Override131 public void closeConnection() throws JdbcServerException {132 if (!endpointConfiguration.isAutoConnect()) {133 handleMessageAndCheckResponse(JdbcMessage.closeConnection());134 }135 if (connections.decrementAndGet() < 0) {136 connections.set(0);137 }138 }139 /**140 * Creates a prepared statement141 * @param stmt The statement to create142 * @throws JdbcServerException In case that the statement was not successful143 */144 @Override145 public void createPreparedStatement(String stmt) throws JdbcServerException {146 if (!endpointConfiguration.isAutoCreateStatement()) {147 handleMessageAndCheckResponse(JdbcMessage.createPreparedStatement(stmt));148 }149 }150 /**151 * Creates a statement152 * @throws JdbcServerException In case that the statement was not successfully created153 */154 @Override155 public void createStatement() throws JdbcServerException {156 if (!endpointConfiguration.isAutoCreateStatement()) {157 handleMessageAndCheckResponse(JdbcMessage.createStatement());158 }159 }160 /**161 * Executes a given query and returns the mapped result162 * @param query The query to execute163 * @return The DataSet containing the query result164 * @throws JdbcServerException In case that the query was not successful165 */166 @Override167 public DataSet executeQuery(String query) throws JdbcServerException {168 log.info("Received execute query request: " + query);169 Message response = handleMessageAndCheckResponse(JdbcMessage.execute(query));170 return dataSetCreator.createDataSet(response, getMessageType(response));171 }172 /**173 * Executes the given statement174 * @param stmt The statement to be executed175 * @throws JdbcServerException In case that the execution was not successful176 */177 @Override178 public DataSet executeStatement(String stmt) throws JdbcServerException {179 log.info("Received execute statement request: " + stmt);180 Message response = handleMessageAndCheckResponse(JdbcMessage.execute(stmt));181 return dataSetCreator.createDataSet(response, getMessageType(response));182 }183 /**184 * Executes the given update185 * @param updateSql The update statement to be executed186 * @throws JdbcServerException In case that the execution was not successful187 */188 @Override189 public int executeUpdate(String updateSql) throws JdbcServerException {190 log.info("Received execute update request: " + updateSql);191 Message response = handleMessageAndCheckResponse(JdbcMessage.execute(updateSql));192 return Optional.ofNullable(193 response.getHeader(JdbcMessageHeaders.JDBC_ROWS_UPDATED))194 .map(Object::toString).map(Integer::valueOf)195 .orElse(0);196 }197 /**198 * Closes the connection199 * @throws JdbcServerException In case that the connection could not be closed200 */201 @Override202 public void closeStatement() throws JdbcServerException {203 if (!endpointConfiguration.isAutoCreateStatement()) {204 handleMessageAndCheckResponse(JdbcMessage.closeStatement());205 }206 }207 /**208 * Sets the transaction state of the database connection209 * @param transactionState The boolean value whether the server is in transaction state.210 */211 @Override212 public void setTransactionState(boolean transactionState) {213 if (log.isDebugEnabled()) {214 log.debug(String.format("Received transaction state change: '%s':%n%s",215 endpointConfiguration.getServerConfiguration().getDatabaseName(),216 String.valueOf(transactionState)));217 }218 this.transactionState = transactionState;219 if(!endpointConfiguration.isAutoTransactionHandling() && transactionState){220 handleMessageAndCheckResponse(JdbcMessage.startTransaction());221 }222 }223 /**224 * Returns the transaction state225 * @return The transaction state of the connection226 */227 @Override228 public boolean getTransactionState() {229 return this.transactionState;230 }231 /**232 * Commits the transaction statements233 */234 @Override235 public void commitStatements() {236 if (log.isDebugEnabled()) {237 log.debug(String.format("Received transaction commit: '%s':%n",238 endpointConfiguration.getServerConfiguration().getDatabaseName()));239 }240 if(!endpointConfiguration.isAutoTransactionHandling()){241 handleMessageAndCheckResponse(JdbcMessage.commitTransaction());242 }243 }244 /**245 * Performs a rollback on the current transaction246 */247 @Override248 public void rollbackStatements() {249 if (log.isDebugEnabled()) {250 log.debug(String.format("Received transaction rollback: '%s':%n",251 endpointConfiguration.getServerConfiguration().getDatabaseName()));252 }253 if(!endpointConfiguration.isAutoTransactionHandling()){254 handleMessageAndCheckResponse(JdbcMessage.rollbackTransaction());255 }256 }257 /**258 * Creates a callable statement259 */260 @Override261 public void createCallableStatement(String sql) {262 if (!endpointConfiguration.isAutoCreateStatement()) {263 handleMessageAndCheckResponse(JdbcMessage.createCallableStatement(sql));264 }265 }266 /**267 * Determines the MessageType of the given response268 * @param response The response to get the message type from269 * @return The MessageType of the response270 */271 private MessageType getMessageType(Message response) {272 String messageTypeString = (String) response.getHeader(MessageHeaders.MESSAGE_TYPE);273 if (MessageType.knows(messageTypeString)){274 return MessageType.valueOf(messageTypeString.toUpperCase());275 }276 return null;277 }278 /**279 * Converts a property map propertyKey -> propertyValue to a list of OpenConnection.Properties280 * @param properties The map to convert281 * @return A list of Properties282 */283 private List<OpenConnection.Property> convertToPropertyList(Map<String, String> properties) {284 return properties.entrySet()285 .stream()286 .map(this::convertToProperty)287 .sorted(Comparator.comparingInt(OpenConnection.Property::hashCode))288 .collect(Collectors.toList());289 }290 /**291 * Converts a Map entry into a OpenConnection.Property292 * @param entry The entry to convert293 * @return the OpenConnection.Property representation294 */295 private OpenConnection.Property convertToProperty(Map.Entry<String, String> entry) {296 OpenConnection.Property property = new OpenConnection.Property();297 property.setName(entry.getKey());298 property.setValue(entry.getValue());299 return property;300 }301 /**302 * Handle request message and check response is successful.303 * @param request The request message to handle304 * @return The response Message305 * @throws JdbcServerException Thrown when the response has some exception header.306 */307 private Message handleMessageAndCheckResponse(Message request) throws JdbcServerException {308 Message response = handleMessage(request);309 checkSuccess(response);310 return response;311 }312 /**313 * Check that response is not having an exception message.314 * @param response The response message to check315 * @throws JdbcServerException In case the message contains a error.316 */317 private void checkSuccess(Message response) throws JdbcServerException {318 OperationResult operationResult = null;319 if (response instanceof JdbcMessage || response.getPayload() instanceof OperationResult) {320 operationResult = response.getPayload(OperationResult.class);321 } else if (response.getPayload() != null && StringUtils.hasText(response.getPayload(String.class))) {322 operationResult = (OperationResult) endpointConfiguration.getMarshaller().unmarshal(new StringSource(response.getPayload(String.class)));...
handleMessage
Using AI Code Generation
1package com.consol.citrus.jdbc.server;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.message.Message;4import com.consol.citrus.server.AbstractServer;5import com.consol.citrus.server.ServerEndpoint;6import com.consol.citrus.server.ServerEndpointAdapter;7import com.consol.citrus.server.ServerEndpointAdapterController;8import com.consol.citrus.server.ServerException;9import com.consol.citrus.server.ServerMessageHandler;10import com.consol.citrus.spi.ReferenceResolver;11import com.consol.citrus.spi.ReferenceResolverAware;12import com.consol.citrus.spi.ReferenceResolverAwareBehavior;13import org.slf4j.Logger;14import org.slf4j.LoggerFactory;15import org.springframework.core.io.Resource;16import java.io.IOException;17import java.util.ArrayList;18import java.util.List;19public class JdbcEndpointAdapterController extends AbstractServer implements ServerEndpointAdapterController, ReferenceResolverAware {20 private static final Logger LOG = LoggerFactory.getLogger(JdbcEndpointAdapterController.class);21 private final ServerEndpointAdapter serverEndpointAdapter;22 private final List<ServerMessageHandler> messageHandlers = new ArrayList<>();23 private final List<ServerEndpoint> endpoints = new ArrayList<>();24 private ReferenceResolver referenceResolver;25 public JdbcEndpointAdapterController(ServerEndpointAdapter serverEndpointAdapter) {26 super("jdbc");27 this.serverEndpointAdapter = serverEndpointAdapter;28 }29 public void addEndpoint(ServerEndpoint endpoint) {30 endpoints.add(endpoint);31 }32 public void addMessageHandler(ServerMessageHandler messageHandler) {33 messageHandlers.add(messageHandler);34 }35 public void handleMessage(Message message, TestContext context) {36 for (ServerMessageHandler messageHandler : messageHandlers) {37 if (messageHandler.supportsMessageType(message.getPayload(String.class))) {38 messageHandler.handleMessage(message, context);39 }40 }41 }42 public void handleEndpointRequest(ServerEndpoint endpoint, Message request, TestContext context) {43 for (ServerEndpoint serverEndpoint : endpoints) {44 if (serverEndpoint.getName().equals(endpoint.getName())) {45 serverEndpoint.handleRequest(request, context);
handleMessage
Using AI Code Generation
1package com.consol.citrus;2import org.springframework.context.annotation.Bean;3import org.springframework.context.annotation.Configuration;4import org.springframework.jdbc.core.JdbcTemplate;5import org.springframework.jdbc.datasource.DriverManagerDataSource;6import org.springframework.util.StringUtils;7import com.consol.citrus.dsl.builder.BuilderSupport;8import com.consol.citrus.dsl.builder.ReceiveMessageBuilder;9import com.consol.citrus.dsl.builder.SendMessageBuilder;10import com.consol.citrus.jdbc.message.JdbcMessage;11import com.consol.citrus.message.MessageType;12import com.consol.citrus.server.Server;13import com.consol.citrus.ws.server.WebServiceServer;14public class JdbcServerConfig {15 public JdbcTemplate jdbcTemplate() {16 DriverManagerDataSource dataSource = new DriverManagerDataSource();17 dataSource.setDriverClassName("org.hsqldb.jdbcDriver");18 dataSource.setUrl("jdbc:hsqldb:mem:testdb");19 dataSource.setUsername("sa");20 dataSource.setPassword("");21 return new JdbcTemplate(dataSource);22 }23 public Server jdbcServer() {24 return CitrusEndpoints.jdbc()25 .port(8080)26 .adapter(new JdbcEndpointAdapterController())27 .autoStart(true)28 .build();29 }30 public ReceiveMessageBuilder receiveJdbcMessageBuilder() {31 return CitrusEndpoints.jdbc()32 .server(jdbcServer())33 .autoStart(true)34 .timeout(5000L);35 }36 public SendMessageBuilder sendJdbcMessageBuilder() {37 return CitrusEndpoints.jdbc()38 .server(jdbcServer())39 .autoStart(true);40 }41 private static class JdbcEndpointAdapterController implements JdbcEndpointAdapter {42 private JdbcTemplate jdbcTemplate;43 public JdbcEndpointAdapterController() {44 jdbcTemplate = jdbcTemplate();45 }46 public void handleMessage(JdbcMessage message, JdbcEndpointAdapterCallback callback) {47 String sql = message.getPayload(String.class);48 if (StringUtils.hasText(sql)) {49 if (sql.toLowerCase().startsWith("select")) {50 callback.sendResultSet(jdbcTemplate.queryForList(sql));51 } else {52 callback.sendUpdateCount(jdbcTemplate.update(sql));53 }54 }55 }56 }57}58package com.consol.citrus;59import org.springframework.context.annotation
handleMessage
Using AI Code Generation
1package com.consol.citrus.jdbc.server;2import java.sql.Connection;3import java.sql.DriverManager;4import java.sql.SQLException;5import java.sql.Statement;6import java.util.ArrayList;7import java.util.Collections;8import java.util.HashMap;9import java.util.List;10import java.util.Map;11import org.springframework.jdbc.core.JdbcTemplate;12import org.springframework.jdbc.datasource.SimpleDriverDataSource;13import org.springframework.util.StringUtils;14import com.consol.citrus.exceptions.CitrusRuntimeException;15import com.consol.citrus.message.DefaultMessage;16import com.consol.citrus.message.Message;17import com.consol.citrus.message.MessageHandler;18import com.consol.citrus.message.MessageHeaders;19import com.consol.citrus.message.MessageType;20import com.consol.citrus.message.MessageTypeResolver;21import com.consol.citrus.message.MessageTypeResolverChain;22import com.consol.citrus.message.MessageTypeResolverChain.MessageTypeResolverChainBuilder;23import com.consol.citrus.message.MessageTypeResolverRegistry;24import com.consol.citrus.message.MessageTypeResolverRegistry.MessageTypeResolverRegistryBuilder;25import com.consol.citrus.message.MessageTypeResolverRegistry.MessageTypeResolverRegistryBuilder.MessageTypeResolverRegistryBuilderBuilder;26import com.consol.citrus.server.AbstractServer;27public class JdbcServer extends AbstractServer {28 private final String driverClassName;29 private final String connectionUrl;30 private final String username;31 private final String password;32 private boolean autoCommit = true;33 private boolean rollback = false;34 private int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;35 private final String databaseName;36 private final String schemaName;37 private final JdbcEndpointAdapter endpointAdapter;38 private SimpleDriverDataSource dataSource;39 private JdbcTemplate jdbcTemplate;40 private MessageHandler messageHandler;41 private MessageTypeResolver messageTypeResolver;
handleMessage
Using AI Code Generation
1public class JdbcEndpointAdapterControllerTest extends AbstractJUnit4SpringContextTests {2 private JdbcEndpointAdapterController jdbcEndpointAdapterController;3 public void testJdbcEndpointAdapterController() throws Exception {4 JdbcEndpointAdapterController.JdbcEndpointAdapterControllerRequest request = new JdbcEndpointAdapterController.JdbcEndpointAdapterControllerRequest();5 request.setSql("select * from user");6 request.setParameters(Collections.emptyList());7 JdbcEndpointAdapterController.JdbcEndpointAdapterControllerResponse response = jdbcEndpointAdapterController.handleMessage(request);8 Assert.assertEquals(1, response.getResults().size());9 }10}11public class JdbcEndpointAdapterControllerTest extends AbstractJUnit4SpringContextTests {12 private JdbcEndpointAdapterController jdbcEndpointAdapterController;13 public void testJdbcEndpointAdapterController() throws Exception {14 JdbcEndpointAdapterController.JdbcEndpointAdapterControllerRequest request = new JdbcEndpointAdapterController.JdbcEndpointAdapterControllerRequest();15 request.setSql("select * from user");16 request.setParameters(Collections.emptyList());17 JdbcEndpointAdapterController.JdbcEndpointAdapterControllerResponse response = jdbcEndpointAdapterController.handleMessage(request);18 Assert.assertEquals(1, response.getResults().size());19 }20}21public class JdbcEndpointAdapterControllerTest extends AbstractJUnit4SpringContextTests {22 private JdbcEndpointAdapterController jdbcEndpointAdapterController;23 public void testJdbcEndpointAdapterController() throws Exception {24 JdbcEndpointAdapterController.JdbcEndpointAdapterControllerRequest request = new JdbcEndpointAdapterController.JdbcEndpointAdapterControllerRequest();25 request.setSql("select * from user");26 request.setParameters(Collections.emptyList());27 JdbcEndpointAdapterController.JdbcEndpointAdapterControllerResponse response = jdbcEndpointAdapterController.handleMessage(request);28 Assert.assertEquals(1, response.getResults().size());29 }30}31public class JdbcEndpointAdapterControllerTest extends AbstractJUnit4SpringContextTests {
handleMessage
Using AI Code Generation
1public class MyJdbcEndpointAdapterController extends JdbcEndpointAdapterController {2 public MyJdbcEndpointAdapterController(ApplicationContext applicationContext) {3 super(applicationContext);4 }5 public String handleMessage(String message) {6 return "my response";7 }8 }9public class MyJdbcEndpointAdapterController extends JdbcEndpointAdapterController {10 public MyJdbcEndpointAdapterController(ApplicationContext applicationContext) {11 super(applicationContext);12 }13 public String handleMessage(String message) {14 return "my response";15 }16 }17public class MyJdbcEndpointAdapterController extends JdbcEndpointAdapterController {18 public MyJdbcEndpointAdapterController(ApplicationContext applicationContext) {19 super(applicationContext);20 }21 public String handleMessage(String message) {22 return "my response";23 }24 }25public class MyJdbcEndpointAdapterController extends JdbcEndpointAdapterController {26 public MyJdbcEndpointAdapterController(ApplicationContext applicationContext) {27 super(applicationContext);28 }29 public String handleMessage(String message) {30 return "my response";31 }32 }33public class MyJdbcEndpointAdapterController extends JdbcEndpointAdapterController {34 public MyJdbcEndpointAdapterController(ApplicationContext applicationContext) {35 super(applicationContext);36 }37 public String handleMessage(String message) {38 return "my response";39 }40 }41public class MyJdbcEndpointAdapterController extends JdbcEndpointAdapterController {42 public MyJdbcEndpointAdapterController(ApplicationContext applicationContext) {43 super(applicationContext);44 }45 public String handleMessage(String message) {46 return "my response";47 }48 }
handleMessage
Using AI Code Generation
1package com.consol.citrus.dsl.endpoint;2import org.springframework.context.annotation.Bean;3import org.springframework.context.annotation.Configuration;4import org.springframework.jdbc.core.JdbcTemplate;5import org.springframework.jdbc.datasource.DriverManagerDataSource;6import com.consol.citrus.dsl.endpoint.adapter.JdbcEndpointAdapterControllerBuilder;7import com.consol.citrus.dsl.endpoint.adapter.JdbcEndpointAdapterControllerBuilder.JdbcEndpointAdapterController;8public class JdbcEndpointAdapterControllerConfig {9 public JdbcEndpointAdapterController jdbcEndpointAdapterController() {10 return new JdbcEndpointAdapterControllerBuilder()11 .jdbcTemplate(jdbcTemplate())12 .build();13 }14 public JdbcTemplate jdbcTemplate() {15 DriverManagerDataSource dataSource = new DriverManagerDataSource();16 dataSource.setDriverClassName("org.hsqldb.jdbcDriver");17 dataSource.setUrl("jdbc:hsqldb:mem:db1");18 dataSource.setUsername("sa");19 dataSource.setPassword("");20 return new JdbcTemplate(dataSource);21 }22}23package com.consol.citrus.dsl.endpoint;24import org.springframework.context.annotation.Bean;25import org.springframework.context.annotation.Configuration;26import org.springframework.jdbc.core.JdbcTemplate;27import org.springframework.jdbc.datasource.DriverManagerDataSource;28import com.consol.citrus.dsl.endpoint.adapter.JdbcEndpointAdapterControllerBuilder;29import com.consol.citrus.dsl.endpoint.adapter.JdbcEndpointAdapterControllerBuilder.JdbcEndpointAdapterController;30public class JdbcEndpointAdapterControllerConfig {31 public JdbcEndpointAdapterController jdbcEndpointAdapterController() {32 return new JdbcEndpointAdapterControllerBuilder()33 .jdbcTemplate(jdbcTemplate())34 .build();35 }36 public JdbcTemplate jdbcTemplate() {37 DriverManagerDataSource dataSource = new DriverManagerDataSource();38 dataSource.setDriverClassName("org.hsqldb.jdbcDriver");39 dataSource.setUrl("jdbc:hsqldb:mem:db1");40 dataSource.setUsername("sa");41 dataSource.setPassword("");42 return new JdbcTemplate(dataSource);43 }44}45package com.consol.citrus.dsl.endpoint;46import org.springframework.context.annotation.Bean;47import org.springframework.context
handleMessage
Using AI Code Generation
1package com.consol.citrus.samples;2import com.consol.citrus.dsl.endpoint.CitrusEndpoints;3import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;4import com.consol.citrus.jdbc.message.JdbcMessage;5import com.consol.citrus.message.MessageType;6import com.consol.citrus.server.Server;7import org.springframework.beans.factory.annotation.Autowired;8import org.springframework.core.io.ClassPathResource;9import org.springframework.jdbc.core.JdbcTemplate;10import org.springframework.jdbc.datasource.DriverManagerDataSource;11import org.springframework.jdbc.datasource.init.ScriptUtils;12import org.springframework.test.context.ContextConfiguration;13import org.testng.annotations.*;14import javax.sql.DataSource;15import java.sql.SQLException;16import java.util.HashMap;17import java.util.Map;18@ContextConfiguration(classes = {JdbcServerConfig.class})19public class JdbcServerTest extends JUnit4CitrusTestDesigner {20 private Server jdbcServer;21 private DataSource dataSource;22 public void setup() throws SQLException {23 ScriptUtils.executeSqlScript(dataSource.getConnection(), new ClassPathResource("schema.sql"));24 }25 public void testJdbcServer() {26 JdbcMessage requestMessage = new JdbcMessage("SELECT * FROM CUSTOMER WHERE ID = 1");27 requestMessage.setMessageType(MessageType.TEXT);28 Map<String, Object> responseMap = new HashMap<>();29 responseMap.put("ID", 1);30 responseMap.put("NAME", "John Doe");31 responseMap.put("ADDRESS", "Main Street 1");32 JdbcMessage responseMessage = new JdbcMessage(responseMap);33 responseMessage.setMessageType(MessageType.TEXT);34 send(CitrusEndpoints.jdbc().server(jdbcServer).message(requestMessage));35 receive(CitrusEndpoints.jdbc().server(jdbcServer).message(responseMessage));36 }37 public void teardown() throws SQLException {38 ScriptUtils.executeSqlScript(dataSource.getConnection(), new ClassPathResource("drop.sql"));39 }40}
handleMessage
Using AI Code Generation
1import com.consol.citrus.dsl.runner.TestRunner;2import com.consol.citrus.dsl.runner.TestRunnerSupport;3import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;4import com.consol.citrus.message.MessageType;5import com.consol.citrus.testng.CitrusParameters;6import org.springframework.beans.factory.annotation.Autowired;7import org.springframework.beans.factory.annotation.Qualifier;8import org.testng.annotations.Test;9public class JdbcJavaITest extends TestNGCitrusTestRunner {10 @Qualifier("jdbcEndpointAdapter")11 private JdbcEndpointAdapterController jdbcEndpointAdapter;12 @CitrusParameters({"message"})13 public void testJdbcEndpointAdapter(String message) {14 variable("message", message);15 echo("Sending message to JDBC endpoint: '${message}'");16 jdbcEndpointAdapter.handleMessage(message);17 echo("Message sent to JDBC endpoint: '${message}'");18 }19}20import com.consol.citrus.dsl.runner.TestRunner;21import com.consol.citrus.dsl.runner.TestRunnerSupport;22import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;23import com.consol.citrus.message.MessageType;24import com.consol.citrus.testng.CitrusParameters;25import org.springframework.beans.factory.annotation.Autowired;26import org.springframework.beans.factory.annotation.Qualifier;27import org.testng.annotations.Test;28public class JdbcJavaITest extends TestNGCitrusTestRunner {29 @Qualifier("jdbcEndpointAdapter")30 private JdbcEndpointAdapterController jdbcEndpointAdapter;31 @CitrusParameters({"message"})32 public void testJdbcEndpointAdapter(String message) {33 variable("message", message);34 echo("Sending message to JDBC endpoint: '${message}'");35 jdbcEndpointAdapter.handleMessage(message);36 echo("Message sent to JDBC endpoint: '${message}'");37 }38}39import com.consol.citrus.dsl.runner.TestRunner;40import
handleMessage
Using AI Code Generation
1public void testJdbcEndpointAdapterController() throws Exception {2 JdbcEndpointAdapterController jdbcEndpointAdapterController = new JdbcEndpointAdapterController();3 jdbcEndpointAdapterController.setDataSource(dataSource);4 jdbcEndpointAdapterController.setSqlQuery("select * from CUSTOMER");5 jdbcEndpointAdapterController.afterPropertiesSet();6 Message<String> message = new DefaultMessage<String>("test");7 Message<String> response = jdbcEndpointAdapterController.handleMessage(message);8 System.out.println("Response : " + response.getPayload());9}10public void testJdbcEndpointAdapterController() throws Exception {11 JdbcEndpointAdapterController jdbcEndpointAdapterController = new JdbcEndpointAdapterController();12 jdbcEndpointAdapterController.setDataSource(dataSource);13 jdbcEndpointAdapterController.setSqlQuery("select * from CUSTOMER");14 jdbcEndpointAdapterController.afterPropertiesSet();15 Message<String> message = new DefaultMessage<String>("test");16 Message<String> response = jdbcEndpointAdapterController.handleMessage(message);17 System.out.println("Response : " + response.getPayload());18}19public void testJdbcEndpointAdapterController() throws Exception {20 JdbcEndpointAdapterController jdbcEndpointAdapterController = new JdbcEndpointAdapterController();21 jdbcEndpointAdapterController.setDataSource(dataSource);22 jdbcEndpointAdapterController.setSqlQuery("select * from CUSTOMER");23 jdbcEndpointAdapterController.afterPropertiesSet();24 Message<String> message = new DefaultMessage<String>("test");25 Message<String> response = jdbcEndpointAdapterController.handleMessage(message);26 System.out.println("Response : " + response.getPayload());27}28public void testJdbcEndpointAdapterController() throws Exception {29 JdbcEndpointAdapterController jdbcEndpointAdapterController = new JdbcEndpointAdapterController();30 jdbcEndpointAdapterController.setDataSource(dataSource);31 jdbcEndpointAdapterController.setSqlQuery("select * from CUSTOMER");
handleMessage
Using AI Code Generation
1{2 "jdbcEndpointAdapterController": {3 }4}5{6 "jdbcEndpointAdapterController": {7 }8}9{10 "jdbcEndpointAdapterController": {11 }12}13{14 "jdbcEndpointAdapterController": {15 }16}17{18 "jdbcEndpointAdapterController": {19 }20}21{22 "jdbcEndpointAdapterController": {23 }24}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!