Skip to content

Commit

Permalink
improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
mjacoby committed Nov 6, 2023
1 parent a908051 commit f8bade7
Show file tree
Hide file tree
Showing 21 changed files with 328 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,89 @@ public default Page<ConceptDescription> getAllConceptDescriptions(QueryModifier
public default Page<SubmodelElement> getAllSubmodelElements(QueryModifier modifier, PagingInfo paging) throws ResourceNotFoundException {
return findSubmodelElements(SubmodelElementSearchCriteria.NONE, modifier, paging);
}


/**
* Checks if a given {@code org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell} exists.
*
* @param id the id
* @return true if exists, false otherwise
*/
public default boolean assetAdministrationShellExists(String id) {
try {
getAssetAdministrationShell(id, QueryModifier.DEFAULT);
return true;
}
catch (ResourceNotFoundException e) {
return false;
}
}


/**
* Checks if a given {@code org.eclipse.digitaltwin.aas4j.v3.model.ConceptDescription} exists.
*
* @param id the id
* @return true if exists, false otherwise
*/
public default boolean conceptDescriptionExists(String id) {
try {
getConceptDescription(id, QueryModifier.DEFAULT);
return true;
}
catch (ResourceNotFoundException e) {
return false;
}
}


/**
* Checks if a given {@code org.eclipse.digitaltwin.aas4j.v3.model.Submodel} exists.
*
* @param id the id
* @return true if exists, false otherwise
*/
public default boolean submodelExists(String id) {
try {
getSubmodel(id, QueryModifier.DEFAULT);
return true;
}
catch (ResourceNotFoundException e) {
return false;
}
}


/**
* Checks if a given {@code org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement} exists.
*
* @param reference the reference
* @return true if exists, false otherwise
*/
public default boolean submodelElementExists(Reference reference) {
try {
getSubmodelElement(reference, QueryModifier.DEFAULT);
return true;
}
catch (ResourceNotFoundException e) {
return false;
}
}


/**
* Checks if a given {@code org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement} exists.
*
* @param identifier the identifier
* @return true if exists, false otherwise
*/
public default boolean submodelElementExists(SubmodelElementIdentifier identifier) {
try {
getSubmodelElement(identifier, QueryModifier.DEFAULT);
return true;
}
catch (ResourceNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
import de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue;
import de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue;
import de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper;
import de.fraunhofer.iosb.ilt.faaast.service.persistence.Persistence;
import de.fraunhofer.iosb.ilt.faaast.service.util.DeepCopyHelper;
import de.fraunhofer.iosb.ilt.faaast.service.util.FaaastConstants;
import de.fraunhofer.iosb.ilt.faaast.service.util.LambdaExceptionHelper;
import de.fraunhofer.iosb.ilt.faaast.service.util.ReferenceHelper;
import jakarta.json.JsonMergePatch;
import jakarta.json.JsonValue;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -41,6 +44,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.util.AasUtils;
Expand Down Expand Up @@ -143,6 +147,30 @@ else if (SubmodelElementCollection.class.isAssignableFrom(submodelElement.getCla
}


/**
* Removes all asset connections to elements contained in this element.If there are no more providers registerd, the
* asset connection is disconnected.
*
* @param parent reference to the parent element, e.g. a submodel
* @param persistence persistence implementation needed to check if submodel elements still exist
* @throws AssetConnectionException if disconnection fails
*/
protected void cleanupDanglingAssetConnectionsForParent(Reference parent, Persistence persistence) throws AssetConnectionException {
Predicate<Reference> condition = x -> ReferenceHelper.startsWith(x, parent) && !persistence.submodelElementExists(x);
context.getAssetConnectionManager().getConnections().stream()
.forEach(LambdaExceptionHelper.rethrowConsumer(connection -> {
connection.getValueProviders().keySet().removeIf(condition);
connection.getOperationProviders().keySet().removeIf(condition);
connection.getSubscriptionProviders().keySet().removeIf(condition);
if (connection.getValueProviders().isEmpty()
&& connection.getOperationProviders().isEmpty()
&& connection.getSubscriptionProviders().isEmpty()) {
connection.disconnect();
}
}));
}


/**
* Creates an updated element based on a JSON merge patch.
*
Expand Down Expand Up @@ -193,7 +221,7 @@ protected List<AssetIdentification> parseSpecificAssetIds(List<SpecificAssetID>
return List.of();
}
return specificAssetIds.stream()
.map(x -> parseSpecificAssetId(x))
.map(this::parseSpecificAssetId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public GetSelfDescriptionRequestHandler(RequestExecutionContext context) {


@Override
public GetSelfDescriptionResponse process(GetSelfDescriptionRequest request) throws ResourceNotFoundException, AssetConnectionException, ValueMappingException, Exception {
public GetSelfDescriptionResponse process(GetSelfDescriptionRequest request) throws ResourceNotFoundException, AssetConnectionException, ValueMappingException {
return GetSelfDescriptionResponse.builder()
.payload(ServiceDescription.builder()
.profile(ServiceSpecificationProfile.AAS_FULL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValidationException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException;
import de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementUpdateEventMessage;
import de.fraunhofer.iosb.ilt.faaast.service.model.validation.ModelValidator;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.AbstractRequestHandler;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestExecutionContext;
import de.fraunhofer.iosb.ilt.faaast.service.util.ReferenceBuilder;
Expand All @@ -44,12 +45,10 @@ public PatchSubmodelRequestHandler(RequestExecutionContext context) {
@Override
public PatchSubmodelResponse process(PatchSubmodelRequest request)
throws ResourceNotFoundException, AssetConnectionException, ValueMappingException, MessageBusException, ValidationException, ResourceNotAContainerElementException {
// TODO how to do validation on JSON merge patch?
// ModelValidator.validate(request.getSubmodel(), coreConfig.getValidationOnUpdate());
Submodel current = context.getPersistence().getSubmodel(request.getSubmodelId(), QueryModifier.DEFAULT);
Submodel updated = mergePatch(request.getChanges(), current, Submodel.class);
ModelValidator.validate(updated, context.getCoreConfig().getValidationOnUpdate());
context.getPersistence().save(updated);
// TODO what to do with asset connection here?
Reference reference = ReferenceBuilder.forSubmodel(updated);
syncWithAsset(reference, updated.getSubmodelElements());
context.getMessageBus().publish(ElementUpdateEventMessage.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
*/
package de.fraunhofer.iosb.ilt.faaast.service.request.handler.submodel;

import de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException;
import de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException;
import de.fraunhofer.iosb.ilt.faaast.service.model.IdShortPath;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.QueryModifier;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.request.submodel.PostSubmodelElementByPathRequest;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.response.submodel.PostSubmodelElementByPathResponse;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ResourceNotAContainerElementException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ResourceNotFoundException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValidationException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException;
import de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementCreateEventMessage;
import de.fraunhofer.iosb.ilt.faaast.service.model.validation.ModelValidator;
Expand Down Expand Up @@ -47,7 +51,8 @@ public PostSubmodelElementByPathRequestHandler(RequestExecutionContext context)


@Override
public PostSubmodelElementByPathResponse doProcess(PostSubmodelElementByPathRequest request) throws ResourceNotFoundException, ValueMappingException, Exception {
public PostSubmodelElementByPathResponse doProcess(PostSubmodelElementByPathRequest request)
throws ResourceNotFoundException, ValueMappingException, ValidationException, ResourceNotAContainerElementException, AssetConnectionException, MessageBusException {
ModelValidator.validate(request.getSubmodelElement(), context.getCoreConfig().getValidationOnCreate());
IdShortPath idShortPath = IdShortPath.parse(request.getPath());
Reference parentReference = new ReferenceBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
*/
package de.fraunhofer.iosb.ilt.faaast.service.request.handler.submodel;

import de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException;
import de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.request.submodel.PostSubmodelElementRequest;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.response.submodel.PostSubmodelElementResponse;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ResourceNotAContainerElementException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ResourceNotFoundException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValidationException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException;
import de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementCreateEventMessage;
import de.fraunhofer.iosb.ilt.faaast.service.model.validation.ModelValidator;
Expand All @@ -42,7 +46,8 @@ public PostSubmodelElementRequestHandler(RequestExecutionContext context) {


@Override
public PostSubmodelElementResponse doProcess(PostSubmodelElementRequest request) throws ResourceNotFoundException, ValueMappingException, Exception {
public PostSubmodelElementResponse doProcess(PostSubmodelElementRequest request)
throws ResourceNotFoundException, ValueMappingException, ValidationException, ResourceNotAContainerElementException, AssetConnectionException, MessageBusException {
ModelValidator.validate(request.getSubmodelElement(), context.getCoreConfig().getValidationOnCreate());
Reference parentReference = ReferenceBuilder.forSubmodel(request.getSubmodelId());
Reference childReference = ReferenceBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package de.fraunhofer.iosb.ilt.faaast.service.request.handler.submodelrepository;

import de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException;
import de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.StatusCode;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.QueryModifier;
Expand All @@ -23,6 +24,7 @@
import de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementDeleteEventMessage;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.AbstractRequestHandler;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestExecutionContext;
import de.fraunhofer.iosb.ilt.faaast.service.util.ReferenceBuilder;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;


Expand All @@ -41,17 +43,16 @@ public DeleteSubmodelByIdRequestHandler(RequestExecutionContext context) {


@Override
public DeleteSubmodelByIdResponse process(DeleteSubmodelByIdRequest request) throws ResourceNotFoundException, MessageBusException {
public DeleteSubmodelByIdResponse process(DeleteSubmodelByIdRequest request) throws ResourceNotFoundException, MessageBusException, AssetConnectionException {
DeleteSubmodelByIdResponse response = new DeleteSubmodelByIdResponse();
Submodel submodel = context.getPersistence().getSubmodel(request.getId(), QueryModifier.DEFAULT);
context.getPersistence().deleteSubmodel(request.getId());
response.setStatusCode(StatusCode.SUCCESS_NO_CONTENT);
//TODO: Delete AssetConnections of underlying submodel elements?
cleanupDanglingAssetConnectionsForParent(ReferenceBuilder.forSubmodel(submodel), context.getPersistence());
context.getMessageBus().publish(ElementDeleteEventMessage.builder()
.element(submodel)
.value(submodel)
.build());
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValidationException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException;
import de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementUpdateEventMessage;
import de.fraunhofer.iosb.ilt.faaast.service.model.validation.ModelValidator;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.AbstractRequestHandler;
import de.fraunhofer.iosb.ilt.faaast.service.request.handler.RequestExecutionContext;
import de.fraunhofer.iosb.ilt.faaast.service.util.ReferenceBuilder;
Expand All @@ -45,13 +46,12 @@ public PatchSubmodelByIdRequestHandler(RequestExecutionContext context) {
@Override
public PatchSubmodelByIdResponse process(PatchSubmodelByIdRequest request)
throws ResourceNotFoundException, AssetConnectionException, ValueMappingException, MessageBusException, ValidationException, ResourceNotAContainerElementException {
// TODO how to do validation on JSON merge patch?
// ModelValidator.validate(request.getSubmodel(), coreConfig.getValidationOnUpdate());
Submodel current = context.getPersistence().getSubmodel(request.getId(), QueryModifier.DEFAULT);
Submodel updated = mergePatch(request.getChanges(), current, Submodel.class);
ModelValidator.validate(updated, context.getCoreConfig().getValidationOnUpdate());
context.getPersistence().save(updated);
// TODO what to do with asset connection here?
Reference reference = ReferenceBuilder.forSubmodel(updated);
cleanupDanglingAssetConnectionsForParent(reference, context.getPersistence());
syncWithAsset(reference, updated.getSubmodelElements());
context.getMessageBus().publish(ElementUpdateEventMessage.builder()
.element(reference)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package de.fraunhofer.iosb.ilt.faaast.service.request.handler.submodelrepository;

import de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException;
import de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.request.submodelrepository.PostSubmodelRequest;
import de.fraunhofer.iosb.ilt.faaast.service.model.api.response.submodelrepository.PostSubmodelResponse;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ResourceNotAContainerElementException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ResourceNotFoundException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValidationException;
import de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException;
import de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementCreateEventMessage;
import de.fraunhofer.iosb.ilt.faaast.service.model.validation.ModelValidator;
Expand All @@ -42,7 +45,8 @@ public PostSubmodelRequestHandler(RequestExecutionContext context) {


@Override
public PostSubmodelResponse process(PostSubmodelRequest request) throws ResourceNotFoundException, AssetConnectionException, ValueMappingException, Exception {
public PostSubmodelResponse process(PostSubmodelRequest request)
throws ResourceNotFoundException, AssetConnectionException, ValueMappingException, ValidationException, ResourceNotAContainerElementException, MessageBusException {
ModelValidator.validate(request.getSubmodel(), context.getCoreConfig().getValidationOnCreate());
context.getPersistence().save(request.getSubmodel());
Reference reference = AasUtils.toReference(request.getSubmodel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public void testPostAssetAdministrationShellRequest() throws Exception {
.statusCode(StatusCode.SUCCESS_CREATED)
.build();
Assert.assertTrue(ResponseHelper.equalsIgnoringTime(expected, actual));
verify(persistence, times(1)).save(eq(environment.getAssetAdministrationShells().get(0)));
verify(persistence, times(1)).save(environment.getAssetAdministrationShells().get(0));
}


Expand Down Expand Up @@ -373,19 +373,19 @@ public void testPutAssetAdministrationShellByIdRequest() throws Exception {
.statusCode(StatusCode.SUCCESS)
.build();
Assert.assertTrue(ResponseHelper.equalsIgnoringTime(expected, actual));
verify(persistence, times(1)).save(eq(environment.getAssetAdministrationShells().get(0)));
verify(persistence, times(1)).save(environment.getAssetAdministrationShells().get(0));
}


@Test
public void testDeleteAssetAdministrationShellByIdRequest() throws ResourceNotFoundException, Exception {
when(persistence.getAssetAdministrationShell(eq(environment.getAssetAdministrationShells().get(0).getId()), any()))
.thenReturn(environment.getAssetAdministrationShells().get(0));
DeleteAssetAdministrationShellByIdRequest request = new DeleteAssetAdministrationShellByIdRequest().builder()
DeleteAssetAdministrationShellByIdRequest request = DeleteAssetAdministrationShellByIdRequest.builder()
.id(environment.getAssetAdministrationShells().get(0).getId())
.build();
DeleteAssetAdministrationShellByIdResponse actual = manager.execute(request);
DeleteAssetAdministrationShellByIdResponse expected = new DeleteAssetAdministrationShellByIdResponse.Builder()
DeleteAssetAdministrationShellByIdResponse expected = DeleteAssetAdministrationShellByIdResponse.builder()
.statusCode(StatusCode.SUCCESS_NO_CONTENT)
.build();
Assert.assertTrue(ResponseHelper.equalsIgnoringTime(expected, actual));
Expand Down Expand Up @@ -952,7 +952,7 @@ public void testPutSubmodelElementByPathRequest() throws ResourceNotFoundExcepti
.build();
Assert.assertTrue(ResponseHelper.equalsIgnoringTime(expected, actual));
verify(assetValueProvider).setValue(ElementValueMapper.toValue(newSubmodelElement, DataElementValue.class));
verify(persistence).update(eq(ReferenceBuilder.forSubmodel(request.getSubmodelId(), request.getSubmodelElement().getIdShort())), eq(newSubmodelElement));
verify(persistence).update(ReferenceBuilder.forSubmodel(request.getSubmodelId(), request.getSubmodelElement().getIdShort()), newSubmodelElement);
}


Expand Down
Loading

0 comments on commit f8bade7

Please sign in to comment.