Skip to content

Commit

Permalink
Merge pull request #143 from mdanish98/fix/aas-serv-event
Browse files Browse the repository at this point in the history
Fixes wrong ObjectMapper usage in AAS Service eventing
  • Loading branch information
FrankSchnicke authored Nov 22, 2023
2 parents c6a8f06 + 8e0c4ac commit bf1932c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions basyx.aasservice/basyx.aasservice-feature-mqtt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.mqttcore</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.http</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ public class MqttAasService implements AasService {

private IMqttClient mqttClient;
private String repoId;
private ObjectMapper objectMapper;

public MqttAasService(AasService decorated, IMqttClient mqttClient, MqttAasServiceTopicFactory topicFactory, String repoId) {
public MqttAasService(AasService decorated, IMqttClient mqttClient, MqttAasServiceTopicFactory topicFactory, String repoId, ObjectMapper objectMapper) {
this.topicFactory = topicFactory;
this.decorated = decorated;
this.mqttClient = mqttClient;
this.repoId = repoId;
this.objectMapper = objectMapper;
}

public String serialize(Object obj) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@
import org.eclipse.digitaltwin.basyx.aasservice.AasServiceFactory;
import org.eclipse.paho.client.mqttv3.IMqttClient;

import com.fasterxml.jackson.databind.ObjectMapper;

public class MqttAasServiceFactory implements AasServiceFactory {

private AasServiceFactory decorated;
private IMqttClient client;
private MqttAasServiceTopicFactory topicFactory;
private String repoId;
private ObjectMapper objectMapper;

public MqttAasServiceFactory(AasServiceFactory decorated, IMqttClient client, MqttAasServiceTopicFactory topicFactory, String repoId) {
public MqttAasServiceFactory(AasServiceFactory decorated, IMqttClient client, MqttAasServiceTopicFactory topicFactory, String repoId, ObjectMapper objectMapper) {
this.decorated = decorated;
this.client = client;
this.topicFactory = topicFactory;
this.repoId = repoId;
this.objectMapper = objectMapper;
}

@Override
public AasService create(AssetAdministrationShell aas) {
return new MqttAasService(decorated.create(aas), client, topicFactory, repoId);
return new MqttAasService(decorated.create(aas), client, topicFactory, repoId, objectMapper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

@ConditionalOnExpression("#{${" + MqttAasServiceFeature.FEATURENAME + ".enabled:false} or ${basyx.feature.mqtt.enabled:false}}")
@Component
public class MqttAasServiceFeature implements AasServiceFeature {
Expand All @@ -43,18 +45,19 @@ public class MqttAasServiceFeature implements AasServiceFeature {
private boolean enabled;

private IMqttClient mqttClient;

private String repoId;
private ObjectMapper objectMapper;

@Autowired
public MqttAasServiceFeature(IMqttClient mqttClient, AasRepository repo) {
public MqttAasServiceFeature(IMqttClient mqttClient, AasRepository repo, ObjectMapper objectMapper) {
this.mqttClient = mqttClient;
this.repoId = repo.getName();
this.objectMapper = objectMapper;
}

@Override
public AasServiceFactory decorate(AasServiceFactory aasServiceFactory) {
return new MqttAasServiceFactory(aasServiceFactory, mqttClient, new MqttAasServiceTopicFactory(new URLEncoder()), repoId);
return new MqttAasServiceFactory(aasServiceFactory, mqttClient, new MqttAasServiceTopicFactory(new URLEncoder()), repoId, objectMapper);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.DeserializationException;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
Expand All @@ -45,6 +47,9 @@
import org.eclipse.digitaltwin.basyx.aasservice.backend.InMemoryAasServiceFactory;
import org.eclipse.digitaltwin.basyx.common.mqttcore.encoding.URLEncoder;
import org.eclipse.digitaltwin.basyx.common.mqttcore.listener.MqttTestListener;
import org.eclipse.digitaltwin.basyx.http.Aas4JHTTPSerializationExtension;
import org.eclipse.digitaltwin.basyx.http.BaSyxHTTPConfiguration;
import org.eclipse.digitaltwin.basyx.http.SerializationExtension;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttSecurityException;
Expand Down Expand Up @@ -74,16 +79,17 @@ public class TestMqttAasService extends AasServiceSuite {

private MqttAasService mqttAasService;
private AssetAdministrationShell shell;
private static ObjectMapper objectMapper;

@BeforeClass
public static void setUpClass() throws MqttException, IOException {
objectMapper = configureObjectMapper();
mqttBroker = startBroker();
listener = configureInterceptListener(mqttBroker);
mqttClient = createAndConnectClient();

aasRepository = createMqttAasRepository();
mqttAasServiceFactory = createMqttAasServiceFactory(mqttClient);

}

@Before
Expand All @@ -105,7 +111,8 @@ protected AasServiceFactory getAASServiceFactory() {

private static AasServiceFactory createMqttAasServiceFactory(MqttClient client) {
AasServiceFactory serviceFactory = new InMemoryAasServiceFactory();
MqttAasServiceFeature mqttFeature = new MqttAasServiceFeature(client, aasRepository);
MqttAasServiceFeature mqttFeature = new MqttAasServiceFeature(client, aasRepository, objectMapper);

return mqttFeature.decorate(serviceFactory);
}

Expand Down Expand Up @@ -137,7 +144,6 @@ public void addSubmodelReferenceEvent() throws DeserializationException, JsonPro
}

private String serialize(Object obj) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException ignore) {
Expand All @@ -156,6 +162,12 @@ public void removeSubmodelReferenceEvent() throws DeserializationException, Json
assertEquals(topicFactory.createRemoveSubmodelReferenceTopic(repoId, shell.getId()), listener.lastTopic);
assertEquals(serialize(DummyAssetAdministrationShell.submodelReference), listener.lastPayload);
}

private static ObjectMapper configureObjectMapper() {
List<SerializationExtension> extensions = Arrays.asList(new Aas4JHTTPSerializationExtension());

return new BaSyxHTTPConfiguration().jackson2ObjectMapperBuilder(extensions).build();
}

private static AasRepository createMqttAasRepository() {
AasRepositoryFactory repoFactory = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), mqttAasServiceFactory);
Expand Down

0 comments on commit bf1932c

Please sign in to comment.