Skip to content

Commit

Permalink
Added test methods for submodelrepository and submodelservice (#335)
Browse files Browse the repository at this point in the history
* Added test methods for submodelrepository and submodelservice

* Fixing test methods

* Attend review remarks

* minor changes

* Attend review remarks

* minor change

* minor changes
  • Loading branch information
VivekHub97 authored Aug 21, 2024
1 parent 25be0cf commit 7ea6337
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 14 deletions.
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);

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
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

0 comments on commit 7ea6337

Please sign in to comment.