Testing Spring-Integration with subscriber channel - testing

I have the following:
Direct-Channel => Splitter => PublishSubscribeChannel
I would like to send data to the direct-channel and test the outcome in the publish-subscribe-channel
What I have so far partly taken from Spring.io (and it works only if I step through debug mode) is:
DirectChannel incomeChannel
PublishSubscribeChannel subscribeChannel
#Test
public void test() {
final AtomicInteger count = new AtomicInteger()
assert true == subscribeChannel.subscribe(new MessageHandler() {
void handleMessage(Message<?> message) throws MessagingException {
count.getAndIncrement();
Entity response = message.getPayload()
assert response != null
// assertions ...
}
})
def request = MessageBuilder.withPayload(entities).build()
assert incomeChannel.send(request) == true
Thread.sleep(10000)
assert 0 < count.get()
}

You don't show your configuration but, if your pub sub channel has a task executor, you need to add a latch; you also should do the asserts on the main thread...
#Test
public void test() {
final AtomicReference<Message<?>> messageRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
assert true == subscriberChannel.subscribe(new MessageHandler() {
void handleMessage(Message<?> message) throws MessagingException {
messageRef.set(message);
latch.countDown();
}
})
def request = MessageBuilder.withPayload(entities).build()
assert incomeChannel.send(request) == true
assert true == latch.await(10, TimeUnit.SECONDS)
Entity response = msg.get().getPayload()
assert response != null
// assertions ...
}

Related

Using MQTT ManagedClient with ASP NET API, how to?

I'm currently working on a project that has to rely heavily on MQTT - one of the parts that needs to utilize MQTT is a ASP Net API, but I'm having difficulties receiving messages.
Here is my MQTTHandler:
public MQTTHandler()
{
_mqttUrl = Properties.Resources.mqttURL ?? "";
_mqttPort = Properties.Resources.mqttPort ?? "";
_mqttUsername = Properties.Resources.mqttUsername ?? "";
_mqttPassword = Properties.Resources.mqttUsername ?? "";
_mqttFactory = new MqttFactory();
_tls = false;
}
public async Task<IManagedMqttClient> ConnectClientAsync()
{
var clientID = Guid.NewGuid().ToString();
var messageBuilder = new MqttClientOptionsBuilder()
.WithClientId(clientID)
.WithCredentials(_mqttUsername, _mqttPassword)
.WithTcpServer(_mqttUrl, Convert.ToInt32(_mqttPort));
var options = _tls ? messageBuilder.WithTls().Build() : messageBuilder.Build();
var managedOptions = new ManagedMqttClientOptionsBuilder()
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithClientOptions(options)
.Build();
_mqttClient = new MqttFactory().CreateManagedMqttClient();
await _mqttClient.StartAsync(managedOptions);
Console.WriteLine("Klient startet");
return _mqttClient;
}
public async Task PublishAsync(string topic, string payload, bool retainFlag = true, int qos = 1)
{
await _mqttClient.EnqueueAsync(new MqttApplicationMessageBuilder()
.WithTopic(topic)
.WithPayload(payload)
.WithQualityOfServiceLevel((MQTTnet.Protocol.MqttQualityOfServiceLevel)qos)
.WithRetainFlag(retainFlag)
.Build());
Console.WriteLine("Besked published");
}
public async Task SubscribeAsync(string topic, int qos = 1)
{
var topicFilters = new List<MQTTnet.Packets.MqttTopicFilter>
{
new MqttTopicFilterBuilder()
.WithTopic(topic)
.WithQualityOfServiceLevel((MQTTnet.Protocol.MqttQualityOfServiceLevel)(qos))
.Build()
};
await _mqttClient.SubscribeAsync(topicFilters);
}
public Status GetSystemStatus(MqttApplicationMessageReceivedEventArgs e)
{
try
{
var json = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
var status = JsonSerializer.Deserialize<Status>(json);
if (status != null)
{
return status;
}
else
{
return null;
}
}
catch (Exception)
{
throw;
}
}
The above has been tested with a console app and works as it should.
The reason I need MQTT in the APi is that a POST method has to act on the value of a topic;
In particular I need to check a systems status before allowing the post;
[HttpPost]
public async Task<ActionResult<Order>> PostOrder(Order order)
{
if (_lastStatus != null)
{
if (_lastStatus.OpStatus)
{
return StatusCode(400, "System is busy!");
}
else
{
var response = await _orderManager.AddOrder(order);
return StatusCode(response.StatusCode, response.Message);
}
}
return StatusCode(400, "Something went wrong");
}
So I will need to set up a subscriber for this controller, and set the value of _lastStatus on received messages:
private readonly MQTTHandler _mqttHandler;
private IManagedMqttClient _mqttClient;
private Status _lastStatus;
public OrdersController(OrderManager orderManager)
{
_orderManager = orderManager;
_mqttHandler = new MQTTHandler();
_mqttClient = _mqttHandler.ConnectClientAsync().Result;
_mqttHandler.SubscribeAsync("JSON/Status");
_mqttClient.ApplicationMessageReceivedAsync += e =>
{
_lastStatus = _mqttHandler.GetSystemStatus(e);
return Task.CompletedTask;
};
}
However, it's behaving a little odd and I'm not experienced enough to know why.
The first time I make a POST request, _lastStatus is null - every following POST request seem to have the last retained message.
I'm guessing that I am struggling due to stuff being asynchronous, but not sure, and every attempt I've attempted to make it synchronous have failed.
Anyone have a clue about what I'm doing wrong?

How to write Unit Test for Hybris DAO Implementation

I am new with unit test and I am trying to test this method, but it did not manage to capture the query of the method, I only managed to get it to enter an exception but not to take the query and return it.
Is there a way to return "result.getResult().get(0)" in the unit test?
Thanks
#Override
public HouseModel findByCode(String code) {
var sQuery = "SELECT {h:pk} FROM {House as h} WHERE {h:id} = ?id ";
var query = new FlexibleSearchQuery(sQuery);
query.addQueryParameter("id", Objects.requireNonNullElse(code, ""));
SearchResult<HouseModel> result = flexibleSearchService.search(query);
return result.getResult().get(0);
}
Code Test:
#Test
public void testFindByCode() {
when(flexibleSearchService.search((FlexibleSearchQuery) any())).thenThrow(new RuntimeException("test"));
RuntimeException exception = new RuntimeException();
try {
var result2 = houseDAOImpl.findByCode("testcode");
} catch (RuntimeException e) {
e.printStackTrace();
exception = e;
}
boolean shouldtrue = exception.getMessage().equalsIgnoreCase("test");
System.out.println(exception.getMessage());
System.out.println(shouldtrue);
assertTrue(shouldtrue);
}
Hybris supports TransactionTest incase of interaction with db.
public class HouseDAOImpTest extends HybrisJUnit4TransactionalTest
{
private TypeService typeService;
private ModelService modelService;
private DeeplinkUrlDao dao;
private List<HouseModel> createdRules;
/**
* #throws java.lang.Exception
*/
#Before
public void setUp() throws Exception
{
createdRules = createHouses();
houseDAOImpl = (HouseFinderDao) Registry.getApplicationContext().getBean("houseFinderDao");
}
#Test
public void testFindByCode()
{
final HouseModel hm = houseDAOImpl.findByCode("testcode");
assertThat(hm.getCode(), is(equalTo(""testcode""));
}
private ModelService getModelService()
{
if (modelService == null)
{
modelService = (ModelService) Registry.getApplicationContext().getBean("modelService");
}
return modelService;
}
private TypeService getTypeService()
{
if (typeService == null)
{
typeService = (TypeService) Registry.getApplicationContext().getBean("typeService");
}
return typeService;
}
/**
* Creates the Houses.
*/
private List<HouseModel> createHouses()
{
final List<HouseModel> result = new ArrayList<HouseModel>();
final HouseModel houseModel1 = getModelService().create(HouseModel.class);
houseModel.setCode("testcode");
modelService.save(houseModel1);
// create other houses model and follow previous steps
result.add(houseModel1);
result.add(houseModel2);
result.add(houseModel3);
return result;
}
}

How to validate error body in Webflux/Webclient

I have a handler method for an endpoint, that is this one:
public Mono<ServerResponse> create(ServerRequest serverRequest) {
Validator validator = new CreateUserValidator();
Mono<UserDto> userDtoMono = serverRequest.bodyToMono(UserDto.class);
return userDtoMono.flatMap(user ->
{
Errors errors = new BeanPropertyBindingResult(user, UserDto.class.getName());
validator.validate(user, errors);
if (errors == null || errors.getAllErrors().isEmpty()) {
return userService.create(user).flatMap(aa -> ServerResponse.status(HttpStatus.CREATED)
.contentType(MediaType.APPLICATION_JSON).body(fromValue(aa))).onErrorResume(this::handleException);
} else {
Set<String> errors1 = new HashSet<String>();
errors.getAllErrors().forEach(message -> {
errors1.add(message.getDefaultMessage());
});
return handleException(new InvalidAttributesException(errors1));
}
});
}
private Mono<ServerResponse> handleException(Throwable exception) {
ErrorResponse errorResponse = new ErrorResponse();
if (exception instanceof InvalidAttributesException) {
InvalidAttributesException asd = (InvalidAttributesException) exception;
asd.getErrors().forEach(error ->
errorResponse.addMessage(messagesService.getMessage(error)));
} else {
errorResponse.addMessage(messagesService.getMessage(exception.getMessage()));
}
logger.info("Error:" + errorResponse);
return ServerResponse.status(HttpStatus.BAD_REQUEST).body(fromValue(errorResponse));
}
As you can see, if the validator fails, the method return a bad request error with a ErrorResponse as a body.
I use a WebClient in order to test it. The WebClient has a filter to get the ErrorResponse in case of a error status:
WebClient client = WebClient.builder().clientConnector(new
ReactorClientHttpConnector(HttpClient.create(ConnectionProvider.newConnection()))).filter(ExchangeFilterFunction.ofResponseProcessor(clientResponse ->
{
if (clientResponse.statusCode().isError()){
return clientResponse.bodyToMono(ErrorResponse.class).flatMap(errorResponse ->
Mono.error(new InvalidAttributesException(new HashSet<>(errorResponse.getMessages())))
);
}
return Mono.just(clientResponse);
})).baseUrl("http://localhost:8080").build();
Mono<ErrorResponse> response = (Mono<ErrorResponse>) client.post().uri(thingsEndpoint(url)).accept( MediaType.APPLICATION_JSON ).body(Mono.just(userDto),UserDto.class).ti
.exchange();
response.subscribe(as -> {
List<String> expectedMessages = new ArrayList<>();
expectedMessages.add("name is mandatory");
expectedMessages.add("email is mandatory");
assertTrue(as.getMessages().containsAll(expectedMessages));
});
But it doesn't work. When I debug the test, it seems that when the exchange() method is called returns an exception before calling the endpoint. What am I doing bad?

1 request exchange, 1 reply exchange, N number responses

I want to push out 1 response and wait X milliseconds for N responses based on a correlation ID in the headers.
Current code is pretty simple: Send a call then start polling indiscriminately. That works ... for one call.
I know there is talk of a JMS solution ("JMSReader?") that spawns N number of listeners looking for correlation ID allowing these futures to time out, but I am not finding anything remotely related.
Here is a demo app that shows one way to do it...
#SpringBootApplication
public class So57377491Application {
public static void main(String[] args) {
SpringApplication.run(So57377491Application.class, args);
}
private final ConcurrentMap<String, List<String>> pending = new ConcurrentHashMap<>();
private final ConcurrentMap<String, SettableListenableFuture<List<String>>> futures = new ConcurrentHashMap<>();
#Bean
public ApplicationRunner runner(RabbitTemplate template) {
return args -> {
this.pending.put("bar", new ArrayList<>());
this.futures.put("bar", new SettableListenableFuture<>());
template.convertAndSend("so57377491", "", "Foo", msg -> {
msg.getMessageProperties().setCorrelationId("bar");
msg.getMessageProperties().setReplyTo("replyExchange/so57377491-replies");
return msg;
});
try {
List<String> list = this.futures.get("bar").get(5, TimeUnit.SECONDS);
System.out.println(list);
}
catch (TimeoutException toe) {
System.out.println("Partial result after timeout " + this.pending.remove("bar"));
}
finally {
this.futures.remove("bar");
}
};
}
#RabbitListener(bindings = #QueueBinding(
value = #Queue(value = "so57377491-1"),
exchange = #Exchange(value = "so57377491", type = "fanout")))
public String listen1(String in) {
System.out.println(in);
return in.toUpperCase();
}
#RabbitListener(bindings = #QueueBinding(
value = #Queue(value = "so57377491-2"),
exchange = #Exchange(value = "so57377491", type = "fanout")))
public String listen2(String in) {
System.out.println(in);
return in.toLowerCase();
}
#RabbitListener(bindings = #QueueBinding(
value = #Queue(value = "so57377491-3"),
exchange = #Exchange(value = "so57377491", type = "fanout")))
public String listen3(String in) {
System.out.println(in);
return in + in;
}
#RabbitListener(bindings = #QueueBinding(
value = #Queue(value = "so57377491-replies"),
exchange = #Exchange(value = "replyExchange", type = "fanout")))
public void replies(String in, #Header(AmqpHeaders.CORRELATION_ID) String correlationId) {
System.out.println(in);
List<String> list = this.pending.get(correlationId);
if (list == null) {
System.out.println("Late reply for " + correlationId);
}
else {
list.add(in);
if (list.size() == 3) {
this.futures.get(correlationId).set(list);
this.pending.remove(correlationId);
}
}
}
}
Result
Foo
Foo
Foo
foo
FOO
FooFoo
[foo, FOO, FooFoo]

Spring jmsTemplate send Unit testing doen't work

My service method looks like below, I am trying to mock JmsTemplate so that it can send message during unit testing, but it doesn't execute jmsTemplate.send(...), it directly goes to next line, How can i execute jmsTemplate.send(..) part of code of my service class using unit testing?
public int invokeCallbackListener(final MyObject payload, final MyTask task) throws Exception{
//create map of payload and taskId
int taskStatusCd = task.getTaskSatus().getCode();
final Map<String, Object> map = new HashMap<String, Object>();
map.put(PAYLOAD_KEY, payload);
map.put(TASK_ID_KEY, task.getTaskId());
//generate JMSCorrelationID
final String correlationId = UUID.randomUUID().toString();
String requestQueue = System.getProperty("REQUEST_QUEUE");
requestQueue = requestQueue!=null?requestQueue:ExportConstants.DEFAULT_REQUEST_QUEUE;
jmsTemplate.send(requestQueue, new MessageCreator() {
#Override
public Message createMessage(Session session) throws JMSException {
***ObjectMessage message = session.createObjectMessage((Serializable)map)***; //fail here. Message returns null
message.setJMSCorrelationID(correlationId);
message.setStringProperty(MESSAGE_TYPE_PROPERTY,payload.getMessageType().getMessageType());
return message;
}
});
l.info("Request Message sent with correlationID: " + correlationId);
taskStatusCd = waitForResponseStatus(task.TaskId(), taskStatusCd, correlationId);
return taskStatusCd;
}
This is my test class code.
RemoteInvocationService remoteInvocationService;
JmsTemplate mockTemplate;
Session mockSession;
Queue mockQueue;
ObjectMessage mockMessage;
MessageCreator mockmessageCreator;
#Before
public void setUp() throws Exception {
remoteInvocationService = new RemoteInvocationService();
mockTemplate = mock(JmsTemplate.class);
mockSession = mock(Session.class);
mockQueue = mock(Queue.class);
mockMessage = mock(ObjectMessage.class);
mockmessageCreator = mock(MessageCreator.class);
when(mockSession.createObjectMessage()).thenReturn(mockMessage);
when(mockQueue.toString()).thenReturn("testQueue");
Mockito.doAnswer(new Answer<Message>() {
#Override
public Message answer(final InvocationOnMock invocation) throws JMSException {
final Object[] args = invocation.getArguments();
final String arg2 = (String)args[0];
final MessageCreator arg = (MessageCreator)args[1];
return arg.createMessage(mockSession);
}
}).when(mockTemplate).send(Mockito.any(MessageCreator.class));
mockTemplate.setDefaultDestination(mockQueue);
remoteInvocationService.setJmsTemplate(mockTemplate);
}
#Test
public void testMessage() throws Exception{
MyTask task = new MyTask();
task.setTaskSatus(Status.Pending);
remoteInvocationService.invokeCallbackListener(new MyObject(), task);
}
I have below code which receives message but, I am getting status object null.
Message receivedMsg = jmsTemplate.receiveSelected(responseQueue, messageSelector);if(receivedMsg instanceof TextMessage){
TextMessage status = (TextMessage) receivedMsg;
l.info(status.getText());}
below test code:
TextMessage mockTextMessage;
when(mockSession.createTextMessage()).thenReturn(mockTextMessage);
mockTextMessage.setText("5");
when(mockTemplate.receiveSelected(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(mockTextMessage)
You are mocking the send method that accepts only one parameter (MessageCreator), but you are actually calling the one that accepts two (String, MessageCreator).
Add the String to your mock:
Mockito.doAnswer(new Answer<Message>() {
#Override
public Message answer(final InvocationOnMock invocation) throws JMSException {
final Object[] args = invocation.getArguments();
final MessageCreator arg = (MessageCreator)args[0];
return arg.createMessage(mockSession);
}
}).when(mockTemplate).send(Mockito.any(String.class), Mockito.any(MessageCreator.class));
There is another mistake when mocking the sesssion. You are mocking the method without parameterers:
when(mockSession.createObjectMessage()).thenReturn(mockMessage);
but you actually need to mock the one with the Serializable param:
when(mockSession.createObjectMessage(Mockito.any(Serializable.class)).thenReturn(mockMessage);