DataSetCreator { public DataSet createDataSet(final Message response, final MessageType messageType) { try { if (response.getPayload() instanceof DataSet) { return response.getPayload(DataSet.class); } else if (isReadyToMarshal(response, messageType)) { return marshalResponse(response, messageType); } else { return new DataSet(); } } catch (final SQLException e) { throw new CitrusRuntimeException("Failed to read dataSet from response message", e); } }  DataSet createDataSet(final Message response, final MessageType messageType);  }
@Test public void testCreateDataSetEmpty() throws SQLException { Message message = mock(Message.class); when(message.getPayload()).thenReturn(""); when(message.getPayload(String.class)).thenReturn(""); DataSet dataSet = dataSetCreator.createDataSet(message, null); assertEquals(dataSet.getColumns().size(), 0L); assertEquals(dataSet.getRows().size(), 0L); }
@Test public void testCreateDataSetWithDataSetPayload(){ DataSet expectedDataSet = mock(DataSet.class); Message message = mock(Message.class); when(message.getPayload()).thenReturn(expectedDataSet); when(message.getPayload(DataSet.class)).thenReturn(expectedDataSet); DataSet dataSet = dataSetCreator.createDataSet(message, null); assertEquals(dataSet, expectedDataSet); }
@Test public void testCreateDataSetWithUnknownType(){ DataSet dataSet = dataSetCreator.createDataSet(mock(Message.class), null); assertEquals(dataSet, new DataSet()); }
@Test public void testCreateDataSetFromJson() throws SQLException { String payload = "[{ \"foo\": \"bar\" }]"; Message message = mock(Message.class); when(message.getPayload()).thenReturn(payload); when(message.getPayload(String.class)).thenReturn(payload); DataSet dataSet = dataSetCreator.createDataSet(message, MessageType.JSON); assertEquals(dataSet.getColumns().toString(), "[foo]"); assertEquals(dataSet.getNextRow().getValues().toString(), "{foo=bar}"); OperationResult operationResult = new OperationResult(); operationResult.setDataSet(payload); JdbcMessage jdbcMessage = mock(JdbcMessage.class); when(jdbcMessage.getPayload(OperationResult.class)).thenReturn(operationResult); when(jdbcMessage.getPayload()).thenReturn(operationResult); dataSet = dataSetCreator.createDataSet(jdbcMessage, MessageType.JSON); assertEquals(dataSet.getColumns().toString(), "[foo]"); assertEquals(dataSet.getNextRow().getValues().toString(), "{foo=bar}"); Message operationResultMessage = mock(Message.class); when(operationResultMessage.getPayload(OperationResult.class)).thenReturn(operationResult); when(operationResultMessage.getPayload()).thenReturn(operationResult); dataSet = dataSetCreator.createDataSet(operationResultMessage, MessageType.JSON); assertEquals(dataSet.getColumns().toString(), "[foo]"); assertEquals(dataSet.getNextRow().getValues().toString(), "{foo=bar}"); }
@Test public void testCreateDataSetFromXml() throws SQLException { String payload = "<dataset><row><foo>bar</foo></row></dataset>"; Message message = mock(Message.class); when(message.getPayload()).thenReturn(payload); when(message.getPayload(String.class)).thenReturn(payload); DataSet dataSet = dataSetCreator.createDataSet(message, MessageType.XML); assertEquals(dataSet.getColumns().toString(), "[foo]"); assertEquals(dataSet.getNextRow().getValues().toString(), "{foo=bar}"); OperationResult operationResult = new OperationResult(); operationResult.setDataSet(payload); JdbcMessage jdbcMessage = mock(JdbcMessage.class); when(jdbcMessage.getPayload(OperationResult.class)).thenReturn(operationResult); when(jdbcMessage.getPayload()).thenReturn(operationResult); dataSet = dataSetCreator.createDataSet(jdbcMessage, MessageType.XML); assertEquals(dataSet.getColumns().toString(), "[foo]"); assertEquals(dataSet.getNextRow().getValues().toString(), "{foo=bar}"); Message operationResultMessage = mock(Message.class); when(operationResultMessage.getPayload(OperationResult.class)).thenReturn(operationResult); when(operationResultMessage.getPayload()).thenReturn(operationResult); dataSet = dataSetCreator.createDataSet(operationResultMessage, MessageType.XML); assertEquals(dataSet.getColumns().toString(), "[foo]"); assertEquals(dataSet.getNextRow().getValues().toString(), "{foo=bar}"); }
@Test public void testCreateDataSetFromNotImplementedType() { Message message = mock(Message.class); when(message.getPayload()).thenReturn(""); when(message.getPayload(String.class)).thenReturn(""); DataSet dataSet = dataSetCreator.createDataSet(message, MessageType.BINARY_BASE64); Assert.assertEquals(dataSet, new DataSet()); }