From b6e05c80bb116bf7f3d8d183c89899600aab85c2 Mon Sep 17 00:00:00 2001 From: Semen Date: Sat, 25 Nov 2023 17:22:16 +0200 Subject: [PATCH] update tests --- partials/AmqpPublisherImpl.java | 2 +- partials/KafkaPublisherImpl.java | 2 +- tests/__snapshots__/kafka.test.js.snap | 51 ++++-- tests/__snapshots__/mqtt.test.js.snap | 217 +++++++++++++++++++++---- tests/kafka.test.js | 3 +- tests/mqtt.test.js | 6 +- 6 files changed, 227 insertions(+), 54 deletions(-) diff --git a/partials/AmqpPublisherImpl.java b/partials/AmqpPublisherImpl.java index ea01cf703..fa6aa53c5 100644 --- a/partials/AmqpPublisherImpl.java +++ b/partials/AmqpPublisherImpl.java @@ -16,7 +16,7 @@ @Generated(value="com.asyncapi.generator.template.spring", date="{{''|currentTime }}") @Service -public class PublisherServiceImpl { +public class PublisherServiceImpl implements PublisherService { @Autowired private RabbitTemplate template; diff --git a/partials/KafkaPublisherImpl.java b/partials/KafkaPublisherImpl.java index e0e036496..4afce586d 100644 --- a/partials/KafkaPublisherImpl.java +++ b/partials/KafkaPublisherImpl.java @@ -17,7 +17,7 @@ @Generated(value="com.asyncapi.generator.template.spring", date="{{''|currentTime }}") @Service -public class PublisherService { +public class PublisherServiceImpl implements PublisherService { @Autowired private KafkaTemplate kafkaTemplate; diff --git a/tests/__snapshots__/kafka.test.js.snap b/tests/__snapshots__/kafka.test.js.snap index 087688967..49c190667 100644 --- a/tests/__snapshots__/kafka.test.js.snap +++ b/tests/__snapshots__/kafka.test.js.snap @@ -101,31 +101,16 @@ public class Config { exports[`template integration tests for generated files using the generator and kafka example should generate proper config, services and DTOs files for provided kafka 2`] = ` "package com.asyncapi.service; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.kafka.core.KafkaTemplate; -import org.springframework.kafka.support.KafkaHeaders; -import org.springframework.messaging.Message; -import org.springframework.messaging.support.MessageBuilder; -import org.springframework.stereotype.Service; import com.asyncapi.model.LightMeasuredPayload; import javax.annotation.processing.Generated; @Generated(value="com.asyncapi.generator.template.spring", date="AnyDate") -@Service -public class PublisherService { +public interface PublisherService { - @Autowired - private KafkaTemplate kafkaTemplate; - public void updateLightMeasurement(Integer key, LightMeasuredPayload lightMeasuredPayload) { - Message message = MessageBuilder.withPayload(lightMeasuredPayload) - .setHeader(KafkaHeaders.TOPIC, "event.lighting.measured") - .setHeader(KafkaHeaders.KEY, key) - .build(); - kafkaTemplate.send(message); - } + public void updateLightMeasurement(Integer key, LightMeasuredPayload lightMeasuredPayload); } " `; @@ -520,6 +505,38 @@ exports[`template integration tests for generated files using the generator and springDependencyManager=1.1.3" `; +exports[`template integration tests for generated files using the generator and kafka example should generate proper config, services and DTOs files for provided kafka 9`] = ` +"package com.asyncapi.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.kafka.support.KafkaHeaders; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.stereotype.Service; + +import com.asyncapi.model.LightMeasuredPayload; +import javax.annotation.processing.Generated; + +@Generated(value="com.asyncapi.generator.template.spring", date="AnyDate") +@Service +public class PublisherServiceImpl implements PublisherService { + + @Autowired + private KafkaTemplate kafkaTemplate; + + + public void updateLightMeasurement(Integer key, LightMeasuredPayload lightMeasuredPayload) { + Message message = MessageBuilder.withPayload(lightMeasuredPayload) + .setHeader(KafkaHeaders.TOPIC, "event.lighting.measured") + .setHeader(KafkaHeaders.KEY, key) + .build(); + kafkaTemplate.send(message); + } +} +" +`; + exports[`template integration tests for generated files using the generator and kafka with parameters example should generate proper config and message handler for provided kafka 1`] = ` "package com.asyncapi.infrastructure; diff --git a/tests/__snapshots__/mqtt.test.js.snap b/tests/__snapshots__/mqtt.test.js.snap index 20323fe2e..4db0fd110 100644 --- a/tests/__snapshots__/mqtt.test.js.snap +++ b/tests/__snapshots__/mqtt.test.js.snap @@ -12,7 +12,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.dsl.IntegrationFlow; -import org.springframework.integration.dsl.IntegrationFlows; import org.springframework.integration.endpoint.MessageProducerSupport; import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; import org.springframework.integration.mqtt.core.MqttPahoClientFactory; @@ -92,7 +91,7 @@ public class Config { @Bean public IntegrationFlow receiveLightMeasurementFlow() { - return IntegrationFlows.from(receiveLightMeasurementInbound()) + return IntegrationFlow.from(receiveLightMeasurementInbound()) .handle(messageHandlerService::handleReceiveLightMeasurement) .get(); } @@ -115,7 +114,7 @@ public class Config { } @Bean - @ServiceActivator(inputChannel = "turnOnOutboundChannel") + @ServiceActivator(outputChannel = "turnOnOutboundChannel") public MessageHandler turnOnOutbound() { MqttPahoMessageHandler pahoMessageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory()); pahoMessageHandler.setAsync(true); @@ -133,7 +132,7 @@ public class Config { } @Bean - @ServiceActivator(inputChannel = "turnOffOutboundChannel") + @ServiceActivator(outputChannel = "turnOffOutboundChannel") public MessageHandler turnOffOutbound() { MqttPahoMessageHandler pahoMessageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory()); pahoMessageHandler.setAsync(true); @@ -151,7 +150,7 @@ public class Config { } @Bean - @ServiceActivator(inputChannel = "dimLightOutboundChannel") + @ServiceActivator(outputChannel = "dimLightOutboundChannel") public MessageHandler dimLightOutbound() { MqttPahoMessageHandler pahoMessageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory()); pahoMessageHandler.setAsync(true); @@ -171,34 +170,27 @@ public class Config { exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 2`] = ` "package com.asyncapi.service; -import org.springframework.integration.annotation.Gateway; -import org.springframework.integration.annotation.MessagingGateway; +import com.asyncapi.model.TurnOnOffPayload; +import com.asyncapi.model.TurnOnOffPayload; +import com.asyncapi.model.DimLightPayload; import javax.annotation.processing.Generated; @Generated(value="com.asyncapi.generator.template.spring", date="AnyDate") -@MessagingGateway public interface PublisherService { - - - @Gateway(requestChannel = "turnOnOutboundChannel") - void turnOn(String data); + void turnOn(TurnOnOffPayload turnOnOffPayload, String streetlightId); - - @Gateway(requestChannel = "turnOffOutboundChannel") - void turnOff(String data); + void turnOff(TurnOnOffPayload turnOnOffPayload, String streetlightId); - - @Gateway(requestChannel = "dimLightOutboundChannel") - void dimLight(String data); + void dimLight(DimLightPayload dimLightPayload, String streetlightId); } @@ -833,7 +825,7 @@ public class TestcontainerMqttTest { receivedMessages.add(message); }); - publisherService.turnOn(payload.toString()); + publisherService.turnOn(payload, new String()); MqttMessage message = receivedMessages.get(receivedMessages.size() - 1); @@ -850,7 +842,7 @@ public class TestcontainerMqttTest { receivedMessages.add(message); }); - publisherService.turnOff(payload.toString()); + publisherService.turnOff(payload, new String()); MqttMessage message = receivedMessages.get(receivedMessages.size() - 1); @@ -867,7 +859,7 @@ public class TestcontainerMqttTest { receivedMessages.add(message); }); - publisherService.dimLight(payload.toString()); + publisherService.dimLight(payload, new String()); MqttMessage message = receivedMessages.get(receivedMessages.size() - 1); @@ -916,6 +908,110 @@ exports[`template integration tests for generated files using the generator and springDependencyManager=1.1.3" `; +exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for basic example 13`] = ` +"package com.asyncapi.service; + + +import com.asyncapi.model.TurnOnOffPayload; +import com.asyncapi.model.TurnOnOffPayload; +import com.asyncapi.model.DimLightPayload; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.integration.mqtt.support.MqttHeaders; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageHandler; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.stereotype.Service; + +import javax.annotation.processing.Generated; +import java.util.HashMap; +import java.util.Map; + +@Generated(value="com.asyncapi.generator.template.spring", date="AnyDate") +@Service +public class PublisherServiceImpl implements PublisherService { + @Value("\${mqtt.topic.turnOn}") + private String turnOnTopic; + + @Autowired + private MessageHandler turnOnOutbound; + @Value("\${mqtt.topic.turnOff}") + private String turnOffTopic; + + @Autowired + private MessageHandler turnOffOutbound; + @Value("\${mqtt.topic.dimLight}") + private String dimLightTopic; + + @Autowired + private MessageHandler dimLightOutbound; + + + + + + public void turnOn(TurnOnOffPayload turnOnOffPayload, String streetlightId) { + Map headers = new HashMap<>(); + headers.put(MqttHeaders.TOPIC, getturnOnTopic(streetlightId)); + Message message = new GenericMessage<>(turnOnOffPayload, headers); + turnOnOutbound.handleMessage(message); + } + + private String getturnOnTopic(String streetlightId) { + Map parameters = new HashMap<>(); + parameters.put("streetlightId", streetlightId); + return replaceParameters(turnOnTopic, parameters); + } + + + + + public void turnOff(TurnOnOffPayload turnOnOffPayload, String streetlightId) { + Map headers = new HashMap<>(); + headers.put(MqttHeaders.TOPIC, getturnOffTopic(streetlightId)); + Message message = new GenericMessage<>(turnOnOffPayload, headers); + turnOffOutbound.handleMessage(message); + } + + private String getturnOffTopic(String streetlightId) { + Map parameters = new HashMap<>(); + parameters.put("streetlightId", streetlightId); + return replaceParameters(turnOffTopic, parameters); + } + + + + + public void dimLight(DimLightPayload dimLightPayload, String streetlightId) { + Map headers = new HashMap<>(); + headers.put(MqttHeaders.TOPIC, getdimLightTopic(streetlightId)); + Message message = new GenericMessage<>(dimLightPayload, headers); + dimLightOutbound.handleMessage(message); + } + + private String getdimLightTopic(String streetlightId) { + Map parameters = new HashMap<>(); + parameters.put("streetlightId", streetlightId); + return replaceParameters(dimLightTopic, parameters); + } + + + + private String replaceParameters(String topic, Map parameters) { + if (parameters != null) { + String compiledTopic = topic; + for (String key : parameters.keySet()) { + compiledTopic = compiledTopic.replace("{" + key + "}", parameters.get(key)); + } + return compiledTopic; + } + return topic; + } +} +" +`; + exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for provided mqtt 1`] = ` "package com.asyncapi.infrastructure; @@ -928,7 +1024,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.dsl.IntegrationFlow; -import org.springframework.integration.dsl.IntegrationFlows; import org.springframework.integration.endpoint.MessageProducerSupport; import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory; import org.springframework.integration.mqtt.core.MqttPahoClientFactory; @@ -1021,7 +1116,7 @@ public class Config { @Bean public IntegrationFlow receiveLightMeasurementFlow() { - return IntegrationFlows.from(receiveLightMeasurementInbound()) + return IntegrationFlow.from(receiveLightMeasurementInbound()) .handle(messageHandlerService::handleReceiveLightMeasurement) .get(); } @@ -1044,7 +1139,7 @@ public class Config { } @Bean - @ServiceActivator(inputChannel = "turnOnOutboundChannel") + @ServiceActivator(outputChannel = "turnOnOutboundChannel") public MessageHandler turnOnOutbound() { MqttPahoMessageHandler pahoMessageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory()); pahoMessageHandler.setAsync(true); @@ -1064,22 +1159,18 @@ public class Config { exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for provided mqtt 2`] = ` "package com.asyncapi.service; -import org.springframework.integration.annotation.Gateway; -import org.springframework.integration.annotation.MessagingGateway; +import com.asyncapi.model.TurnOnOffPayload; +import com.asyncapi.model.ZoneId; import javax.annotation.processing.Generated; @Generated(value="com.asyncapi.generator.template.spring", date="AnyDate") -@MessagingGateway public interface PublisherService { - - - @Gateway(requestChannel = "turnOnOutboundChannel") - void turnOn(String data); + void turnOn(TurnOnOffPayload turnOnOffPayload, String streetlightId, ZoneId zoneId); } @@ -1365,7 +1456,7 @@ public class TestcontainerMqttTest { receivedMessages.add(message); }); - publisherService.turnOn(payload.toString()); + publisherService.turnOn(payload, new String(), new com.asyncapi.model.ZoneId()); MqttMessage message = receivedMessages.get(receivedMessages.size() - 1); @@ -1413,3 +1504,65 @@ exports[`template integration tests for generated files using the generator and "springBootVersion=3.1.3 springDependencyManager=1.1.3" `; + +exports[`template integration tests for generated files using the generator and mqtt example should generate proper config, services and DTOs files for provided mqtt 9`] = ` +"package com.asyncapi.service; + + +import com.asyncapi.model.TurnOnOffPayload; +import com.asyncapi.model.ZoneId; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.integration.mqtt.support.MqttHeaders; +import org.springframework.messaging.Message; +import org.springframework.messaging.MessageHandler; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.stereotype.Service; + +import javax.annotation.processing.Generated; +import java.util.HashMap; +import java.util.Map; + +@Generated(value="com.asyncapi.generator.template.spring", date="AnyDate") +@Service +public class PublisherServiceImpl implements PublisherService { + @Value("\${mqtt.topic.turnOn}") + private String turnOnTopic; + + @Autowired + private MessageHandler turnOnOutbound; + + + + + + public void turnOn(TurnOnOffPayload turnOnOffPayload, String streetlightId, ZoneId zoneId) { + Map headers = new HashMap<>(); + headers.put(MqttHeaders.TOPIC, getturnOnTopic(streetlightId, zoneId)); + Message message = new GenericMessage<>(turnOnOffPayload, headers); + turnOnOutbound.handleMessage(message); + } + + private String getturnOnTopic(String streetlightId, ZoneId zoneId) { + Map parameters = new HashMap<>(); + parameters.put("streetlightId", streetlightId); + parameters.put("zoneId", zoneId.toString()); + return replaceParameters(turnOnTopic, parameters); + } + + + + private String replaceParameters(String topic, Map parameters) { + if (parameters != null) { + String compiledTopic = topic; + for (String key : parameters.keySet()) { + compiledTopic = compiledTopic.replace("{" + key + "}", parameters.get(key)); + } + return compiledTopic; + } + return topic; + } +} +" +`; diff --git a/tests/kafka.test.js b/tests/kafka.test.js index b934acd30..9baec1d37 100644 --- a/tests/kafka.test.js +++ b/tests/kafka.test.js @@ -29,7 +29,8 @@ describe('template integration tests for generated files using the generator and '/src/main/java/com/asyncapi/model/LightMeasured.java', '/src/test/java/com/asyncapi/TestcontainerKafkaTest.java', '/build.gradle', - '/gradle.properties' + '/gradle.properties', + '/src/main/java/com/asyncapi/service/PublisherServiceImpl.java' ]; for (const index in expectedFiles) { const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8'); diff --git a/tests/mqtt.test.js b/tests/mqtt.test.js index 4f7b58003..1d017ccbf 100644 --- a/tests/mqtt.test.js +++ b/tests/mqtt.test.js @@ -35,7 +35,8 @@ describe('template integration tests for generated files using the generator and '/src/main/java/com/asyncapi/model/TurnOnOff.java', '/src/test/java/com/asyncapi/TestcontainerMqttTest.java', '/build.gradle', - '/gradle.properties' + '/gradle.properties', + '/src/main/java/com/asyncapi/service/PublisherServiceImpl.java' ]; for (const index in expectedFiles) { const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8'); @@ -60,7 +61,8 @@ describe('template integration tests for generated files using the generator and '/src/main/java/com/asyncapi/model/LightMeasured.java', '/src/test/java/com/asyncapi/TestcontainerMqttTest.java', '/build.gradle', - '/gradle.properties' + '/gradle.properties', + '/src/main/java/com/asyncapi/service/PublisherServiceImpl.java' ]; for (const index in expectedFiles) { const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8');