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

Added test methods for submodelrepository and submodelservice #335

Merged
merged 7 commits into from
Aug 21, 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the License header to 2024

Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
package org.eclipse.digitaltwin.basyx.submodelrepository.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.eclipse.digitaltwin.aas4j.v3.model.DataTypeDefXsd;
import org.eclipse.digitaltwin.aas4j.v3.model.OperationVariable;
import org.eclipse.digitaltwin.aas4j.v3.model.Property;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException;
Expand All @@ -53,8 +56,12 @@
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceHelper;
import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelServiceSuite;
import org.eclipse.digitaltwin.basyx.submodelservice.value.PropertyValue;
import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelValueOnly;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Testsuite for implementations of the SubmodelRepository interface
*
Expand All @@ -65,6 +72,7 @@ public abstract class SubmodelRepositorySuite extends SubmodelServiceSuite {
private static final PaginationInfo NO_LIMIT_PAGINATION_INFO = new PaginationInfo(null, null);
private static final String EMPTY_ID = " ";
private static final String NULL_ID = null;
private static final String ID = "testId";

protected abstract SubmodelRepository getSubmodelRepository();

Expand Down Expand Up @@ -259,6 +267,57 @@ public void getPaginatedSubmodel() {
assertEquals(1, cursorResult.getResult().size());
}

@Test
public void getSubmodelByIdMetadata() throws JsonProcessingException {
SubmodelRepository repo = getSubmodelRepository();
Submodel expectedSubmodel = buildDummySubmodelWithNoSmElement(ID);
expectedSubmodel.setSubmodelElements(null);
repo.createSubmodel(expectedSubmodel);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A normal Submodel with SM Elements should be created but you can create an expected SM same as per the expected SM returned by the API. So that you can compare the expected and the actual.


Submodel retrievedSubmodelMetadata = repo.getSubmodelByIdMetadata(ID);
retrievedSubmodelMetadata.setSubmodelElements(null);

assertEquals(expectedSubmodel, retrievedSubmodelMetadata);
}

@Test
public void getSubmodelByIdValueOnly() throws JsonProcessingException {
SubmodelRepository repo = getSubmodelRepository();
Submodel submodel = buildDummySubmodelWithNoSmElement(ID);

List<SubmodelElement> submodelElements = buildDummySubmodelElements();
submodel.setSubmodelElements(submodelElements);
repo.createSubmodel(submodel);

SubmodelValueOnly expectedSmValueOnly = new SubmodelValueOnly(submodelElements);
SubmodelValueOnly retrievedSmValueOnly = repo.getSubmodelByIdValueOnly(ID);

ObjectMapper mapper = new ObjectMapper();
String expectedSmValueOnlyJSONContent = mapper.writeValueAsString(expectedSmValueOnly);
String retrievedSmValueOnlyJSONContent = mapper.writeValueAsString(retrievedSmValueOnly);

assertEquals(expectedSmValueOnlyJSONContent, retrievedSmValueOnlyJSONContent);
}

@Override
@Test
public void patchSubmodelElements() {
SubmodelRepository repo = getSubmodelRepository();
Submodel submodel = buildDummySubmodelWithNoSmElement(ID);

List<SubmodelElement> submodelElements = buildDummySubmodelElements();
submodel.setSubmodelElements(submodelElements);
repo.createSubmodel(submodel);

List<SubmodelElement> submodelElementsPatch = buildDummySubmodelElementsToPatch();
repo.patchSubmodelElements(ID, submodelElementsPatch);

Submodel patchedSubmodel = repo.getSubmodel(ID);

assertEquals(submodel.getSubmodelElements().size(), patchedSubmodel.getSubmodelElements().size());
assertEquals(submodelElementsPatch, patchedSubmodel.getSubmodelElements());
}

// Has to be overwritten if backend does not support operations
@Test
public void invokeOperation() {
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the license header to 2024

Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEntity;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultFile;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultLangStringTextType;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodel;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
Expand Down Expand Up @@ -89,6 +91,7 @@ public abstract class SubmodelServiceSuite {

private static final String DUMMY_JSON_1 = "{\"name\":\"SampleJsonFile\",\"description\":\"A JSON file for verification\",\"version\":1}";
private static final String DUMMY_JSON_2 = "{\"name\":\"SampleJsonFile\",\"description\":\"A JSON file for verification\",\"version\":2}";
private static final String ID = "testId";

@Test
public void getSubmodel() {
Expand Down Expand Up @@ -363,22 +366,22 @@ public void updateNonFileSME() {
}

@Test
public void updateNonNestedSME() {
Submodel technicalSubmodel = DummySubmodelFactory.createTechnicalDataSubmodel();
SubmodelService submodelService = getSubmodelService(technicalSubmodel);
public void updateNonNestedSME() {
Submodel technicalSubmodel = DummySubmodelFactory.createTechnicalDataSubmodel();
SubmodelService submodelService = getSubmodelService(technicalSubmodel);

String idShortPath = "dummyProperty";

Property property = createDummyProperty(idShortPath);
submodelService.createSubmodelElement(property);

Property expectedUpdatedProperty = SubmodelServiceHelper.createDummyProperty(idShortPath, "arbitraryValue", DataTypeDefXsd.STRING);
submodelService.updateSubmodelElement(idShortPath, expectedUpdatedProperty);
Property property = createDummyProperty(idShortPath);
submodelService.createSubmodelElement(property);

Property expectedUpdatedProperty = SubmodelServiceHelper.createDummyProperty(idShortPath, "arbitraryValue", DataTypeDefXsd.STRING);
submodelService.updateSubmodelElement(idShortPath, expectedUpdatedProperty);

Property actualUpdatedProperty = (Property) submodelService.getSubmodelElement(idShortPath);
assertEquals(expectedUpdatedProperty, actualUpdatedProperty);
}

Property actualUpdatedProperty = (Property) submodelService.getSubmodelElement(idShortPath);
assertEquals(expectedUpdatedProperty, actualUpdatedProperty);
}

@Test
public void updateNonFileSMEWithFileSME() {
Submodel technicalSubmodel = DummySubmodelFactory.createTechnicalDataSubmodel();
Expand Down Expand Up @@ -606,6 +609,44 @@ public void deleteNonExistingFile() throws IOException {
submodelService.deleteFileValue(SubmodelServiceHelper.SUBMODEL_TECHNICAL_DATA_FILE_ID_SHORT);
}

@Test
public void patchSubmodelElements() {

List<SubmodelElement> submodelElements = buildDummySubmodelElements();
Submodel submodel = buildDummySubmodelWithSmElement(ID, submodelElements);
SubmodelService submodelService = getSubmodelService(submodel);

List<SubmodelElement> submodelElementsPatch = buildDummySubmodelElementsToPatch();
submodelService.patchSubmodelElements(submodelElementsPatch);

Submodel patchedSubmodel = submodelService.getSubmodel();

assertEquals(submodel.getSubmodelElements().size(), patchedSubmodel.getSubmodelElements().size());
assertEquals(submodelElementsPatch, patchedSubmodel.getSubmodelElements());
}

protected Submodel buildDummySubmodelWithSmElement(String id, List<SubmodelElement> submodelElements) {
return new DefaultSubmodel.Builder().id(id).submodelElements(submodelElements).build();
}

protected Submodel buildDummySubmodelWithNoSmElement(String id) {
return new DefaultSubmodel.Builder().id(id).build();
}

protected List<SubmodelElement> buildDummySubmodelElements() {
Property prop = new DefaultProperty.Builder().idShort("propId").value("propValue").build();
File file = new DefaultFile.Builder().idShort("fileId").contentType("contentTypeValue").value("fileValue").build();

return Arrays.asList(prop, file);
}

protected List<SubmodelElement> buildDummySubmodelElementsToPatch() {
Property prop = new DefaultProperty.Builder().idShort("propId").value("propNewValue").build();
File file = new DefaultFile.Builder().idShort("fileId").contentType("contentTypeNewValue").value("fileNewValue").build();

return Arrays.asList(prop, file);
}

private void assertStoredFileContentEquals(SubmodelService submodelService, String fileIdShort, String content) throws IOException {
java.io.File retrievedValue = submodelService.getFileByPath(fileIdShort);

Expand Down Expand Up @@ -671,7 +712,6 @@ private String getExtension(String filename) {
return FilenameUtils.getExtension(filename);
}


private void deleteFileIfExisted(SubmodelService service, String idShort) {
try {
service.getFileByPath(idShort);
Expand Down