Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes wrong ObjectMapper usage in AAS Service eventing #143

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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