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 'same Submodel Reference can be applied to same AAS again without throwing 409' #388

Merged
merged 4 commits into from
Aug 22, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -39,10 +39,7 @@
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetAdministrationShell;
import org.eclipse.digitaltwin.basyx.aasservice.AasService;
import org.eclipse.digitaltwin.basyx.aasservice.AasServiceSuite;
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.IdentificationMismatchException;
import org.eclipse.digitaltwin.basyx.core.exceptions.MissingIdentifierException;
import org.eclipse.digitaltwin.basyx.core.exceptions.*;
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.junit.Test;
Expand Down Expand Up @@ -145,6 +142,14 @@ public void addSubmodelReferenceToNonExistingAas() {
aasRepo.addSubmodelReference("doesNotMatter", reference);
}

@Test(expected = CollidingSubmodelReferenceException.class)
public void duplicateSubmodelReference() {
AssetAdministrationShell aas = DummyAasFactory.createAasWithSubmodelReference();
AasRepository aasRepo = getAasRepository(Collections.singleton(aas));
Reference reference = DummyAasFactory.createDummyReference(DummyAasFactory.DUMMY_SUBMODEL_ID);
aasRepo.addSubmodelReference(aas.getId(), reference);
}

@Test(expected = ElementDoesNotExistException.class)
public void removeSubmodelReferenceOfNonExistingAas() {
AasRepository aasRepo = getAasRepository();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -355,37 +355,37 @@ public void getSubmodelReferencesWithNoAuthorization() throws IOException {
public void addSubmodelReferenceWithCorrectRoleAndPermission() throws IOException {
String accessToken = getAccessToken(DummyCredentialStore.BASYX_UPDATER_CREDENTIAL);

CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference.json"), accessToken);
CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference_1.json"), accessToken);
assertEquals(HttpStatus.CREATED.value(), retrievalResponse.getCode());
}

@Test
public void addSubmodelReferenceWithCorrectRoleAndSpecificAasPermission() throws IOException {
String accessToken = getAccessToken(DummyCredentialStore.BASYX_UPDATER_TWO_CREDENTIAL);

CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference.json"), accessToken);
CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference_2.json"), accessToken);
assertEquals(HttpStatus.CREATED.value(), retrievalResponse.getCode());
}

@Test
public void addSubmodelReferenceWithCorrectRoleAndUnauthorizedSpecificAas() throws IOException {
String accessToken = getAccessToken(DummyCredentialStore.BASYX_UPDATER_TWO_CREDENTIAL);

CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID_2), getAasJSONString("SingleSubmodelReference.json"), accessToken);
CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID_2), getAasJSONString("SingleSubmodelReference_1.json"), accessToken);
assertEquals(HttpStatus.FORBIDDEN.value(), retrievalResponse.getCode());
}

@Test
public void addSubmodelReferenceWithInsufficientPermissionRole() throws IOException {
String accessToken = getAccessToken(DummyCredentialStore.BASYX_READER_CREDENTIAL);

CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference.json"), accessToken);
CloseableHttpResponse retrievalResponse = updateElementWithAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference_1.json"), accessToken);
assertEquals(HttpStatus.FORBIDDEN.value(), retrievalResponse.getCode());
}

@Test
public void addSubmodelReferenceWithNoAuthorization() throws IOException {
CloseableHttpResponse retrievalResponse = updateElementWithNoAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference.json"));
CloseableHttpResponse retrievalResponse = updateElementWithNoAuthorizationPostRequest(getSpecificAasSubmodelRefAccessURL(SPECIFIC_SHELL_ID), getAasJSONString("SingleSubmodelReference_1.json"));

assertEquals(HttpStatus.UNAUTHORIZED.value(), retrievalResponse.getCode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -38,6 +38,7 @@
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
import org.eclipse.digitaltwin.basyx.aasrepository.http.pagination.GetAssetAdministrationShellsResult;
import org.eclipse.digitaltwin.basyx.aasrepository.http.pagination.GetReferencesResult;
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingSubmodelReferenceException;
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifier;
Expand Down Expand Up @@ -156,7 +157,11 @@ public ResponseEntity<PagedResult> getAllSubmodelReferencesAasRepository(Base64U

@Override
public ResponseEntity<Reference> postSubmodelReferenceAasRepository(Base64UrlEncodedIdentifier aasIdentifier, @Valid Reference body) {
aasRepository.addSubmodelReference(aasIdentifier.getIdentifier(), body);
try {
aasRepository.addSubmodelReference(aasIdentifier.getIdentifier(), body);
}catch(CollidingSubmodelReferenceException e){
return new ResponseEntity<Reference>(HttpStatus.CONFLICT);
}
return new ResponseEntity<Reference>(body, HttpStatus.CREATED);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -206,6 +206,17 @@ public void createSubmodelReference() throws FileNotFoundException, IOException,
BaSyxHttpTestUtils.assertSameJSONContent(getPaginatedSingleSMReferenceJson(), BaSyxHttpTestUtils.getResponseAsString(getResponse));
}

@Test
public void duplicateSubmodelReference() throws FileNotFoundException, IOException, ParseException {
createDummyAasOnServer(getAas1JSONString());

String json = getSingleSubmodelReference();

addSubmodelReferenceToDummyAas(json);
CloseableHttpResponse response = addSubmodelReferenceToDummyAas(json);
assertEquals(response.getCode(), HttpStatus.CONFLICT.value());
}

@Test
public void removeSubmodelReference() throws FileNotFoundException, IOException, ParseException {
createDummyAasOnServer(getAas1JSONString());
Expand Down Expand Up @@ -408,7 +419,7 @@ private String requestSpecificAasJSON(String aasId) throws IOException, ParseExc
}

private String getSpecificSubmodelReferenceUrl() {
Base64UrlEncodedIdentifier identifier = new Base64UrlEncodedIdentifier("http://i40.customer.com/type/1/1/testSubmodel");
Base64UrlEncodedIdentifier identifier = new Base64UrlEncodedIdentifier("http://i40.customer.com/type/1/1/testSubmodelNew");
return getSpecificAasSubmodelRefAccessURL(dummyAasId) + "/" + identifier.getEncodedIdentifier();
}

Expand All @@ -420,8 +431,8 @@ private String getSpecificAssetInformationAccessURL(String aasID) {
return getSpecificAasAccessURL(aasID) + "/asset-information";
}

private void addSubmodelReferenceToDummyAas(String json) throws FileNotFoundException, IOException {
BaSyxHttpTestUtils.executePostOnURL(getSpecificAasSubmodelRefAccessURL(dummyAasId), json);
private CloseableHttpResponse addSubmodelReferenceToDummyAas(String json) throws FileNotFoundException, IOException {
return BaSyxHttpTestUtils.executePostOnURL(getSpecificAasSubmodelRefAccessURL(dummyAasId), json);
}

protected String getSpecificAasAccessURL(String aasId) {
Expand Down Expand Up @@ -473,7 +484,7 @@ private String getAas2JSONString() throws FileNotFoundException, IOException {
}

private String getSingleSubmodelReference() throws FileNotFoundException, IOException {
return BaSyxHttpTestUtils.readJSONStringFromClasspath("SingleSubmodelReference.json");
return BaSyxHttpTestUtils.readJSONStringFromClasspath("SingleSubmodelReference_1.json");
}

private String getSMReferenceRemovalJson() throws FileNotFoundException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"keys": [
{
"type": "Submodel",
"value": "http://i40.customer.com/type/1/1/testSubmodel"
"value": "http://i40.customer.com/type/1/1/testSubmodelNew"
}
],
"type": "ExternalReference"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"keys": [
{
"type": "Submodel",
"value": "http://i40.customer.com/type/1/1/testSubmodel256"
"value": "http://i40.customer.com/type/1/1/testSubmodelNew"
}
],
"type": "ExternalReference"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"keys": [
{
"type": "Submodel",
"value": "http://i40.customer.com/type/1/1/testSubmodel"
"value": "http://i40.customer.com/type/1/1/testSubmodelNew_2"
}
],
"type": "ExternalReference"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -30,6 +30,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Optional;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand All @@ -42,9 +43,7 @@
import org.eclipse.digitaltwin.aas4j.v3.model.Resource;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultResource;
import org.eclipse.digitaltwin.basyx.aasservice.AasService;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.FileHandlingException;
import org.eclipse.digitaltwin.basyx.core.exceptions.*;
import org.eclipse.digitaltwin.basyx.core.filerepository.FileMetadata;
import org.eclipse.digitaltwin.basyx.core.filerepository.FileRepository;
import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult;
Expand Down Expand Up @@ -94,6 +93,8 @@ public CursorResult<List<Reference>> getSubmodelReferences(PaginationInfo pInfo)

@Override
public void addSubmodelReference(Reference submodelReference) {
throwExceptionIfReferenceIsAlreadyPresent(submodelReference);

aas.getSubmodels().add(submodelReference);
}

Expand Down Expand Up @@ -207,4 +208,28 @@ private File getResourceContent(Resource resource) throws IOException {
return new java.io.File(filePath);
}

private void throwExceptionIfReferenceIsAlreadyPresent(Reference submodelReference) {
Optional<Key> submodelIdKey = getSubmodelTypeKey(submodelReference);
if(submodelIdKey.isEmpty())
return;
String submodelId = submodelIdKey.get().getValue();
if (isSubmodelIdAlreadyReferenced(submodelId)) {
throw new CollidingSubmodelReferenceException(submodelId);
}
}

private boolean isSubmodelIdAlreadyReferenced(String submodelId) {
return aas.getSubmodels().stream().anyMatch(ref -> ref.getKeys().stream().anyMatch(key -> key.getValue().equals(submodelId)));
}

private static Optional<Key> getSubmodelTypeKey(Reference submodelReference) {
Optional<Key> submodelIdKey = submodelReference.getKeys().stream().filter(key -> {
KeyTypes type = key.getType();
if(type == null)
throw new MissingKeyTypeException();
return type.equals(KeyTypes.SUBMODEL);
}).findFirst();
return submodelIdKey;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.digitaltwin.basyx.aasservice.AasService;
import org.eclipse.digitaltwin.basyx.aasservice.client.internal.AssetAdministrationShellServiceApi;
import org.eclipse.digitaltwin.basyx.client.internal.ApiException;
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingSubmodelReferenceException;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementNotAFileException;
import org.eclipse.digitaltwin.basyx.core.exceptions.FileDoesNotExistException;
Expand Down Expand Up @@ -168,6 +169,10 @@ private RuntimeException mapAasAccess(ApiException e) {
return new ElementDoesNotExistException();
}

if(e.getCode() == HttpStatus.CONFLICT.value()) {
return new CollidingSubmodelReferenceException();
}

return e;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2023 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -38,18 +38,13 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetKind;
import org.eclipse.digitaltwin.aas4j.v3.model.KeyTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.ReferenceTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.aas4j.v3.model.*;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultAssetInformation;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultKey;
Expand Down Expand Up @@ -99,14 +94,17 @@ public void addSubmodelReference() {

Submodel submodel = createDummySubmodel();

aasService.addSubmodelReference(submodel.getSemanticId());
Reference ref = new DefaultReference();
Key submodelKey = new DefaultKey.Builder().value(submodel.getId()).type(KeyTypes.SUBMODEL).build();
ref.setKeys(Arrays.asList(submodelKey));

aasService.addSubmodelReference(ref);

List<Reference> submodelReferences = aasService.getSubmodelReferences(NO_LIMIT_PAGINATION_INFO).getResult();

Reference submodelReference = getFirstSubmodelReference(submodelReferences);

assertTrue(
submodelReference.getKeys().stream().filter(ref -> ref.getValue().equals("testKey")).findAny().isPresent());
assertTrue(submodelReference.getKeys().contains(submodelKey));
}

@Test
Expand Down Expand Up @@ -233,6 +231,7 @@ private Reference getFirstSubmodelReference(List<Reference> submodelReferences)

private DefaultSubmodel createDummySubmodel() {
return new DefaultSubmodel.Builder()
.id("testId")
.semanticId(
new DefaultReference.Builder().keys(new DefaultKey.Builder().value("testKey").build()).build())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.core.exceptions;

public class CollidingSubmodelReferenceException extends RuntimeException {
public CollidingSubmodelReferenceException() {
}

public CollidingSubmodelReferenceException(String submodelIdentifier) {
super(getMessage(submodelIdentifier));
}

private static String getMessage(String submodelIdentifier) {
return "Submodel Reference with id " + submodelIdentifier + " already exists";
}

}
Loading