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

fix: Response body for post #484

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,23 @@ public void setSubmodelElementValue(String submodelId, String smeIdShort, Submod
}

@Override
public void createSubmodelElement(String submodelId, SubmodelElement smElement) {
public SubmodelElement createSubmodelElement(String submodelId, SubmodelElement smElement) {
SubmodelService submodelService = getSubmodelServiceOrThrow(submodelId);

submodelService.createSubmodelElement(smElement);
SubmodelElement createdSME = submodelService.createSubmodelElement(smElement);

updateSubmodel(submodelId, submodelService.getSubmodel());
return createdSME;
}

@Override
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
public SubmodelElement createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
SubmodelService submodelService = getSubmodelServiceOrThrow(submodelId);

submodelService.createSubmodelElement(idShortPath, smElement);
SubmodelElement createdSME = submodelService.createSubmodelElement(idShortPath, smElement);

updateSubmodel(submodelId, submodelService.getSubmodel());
return createdSME;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ public void setSubmodelElementValue(String submodelId, String smeIdShort, Submod
}

@Override
public void createSubmodelElement(String submodelId, SubmodelElement smElement) {
getConnectedSubmodelService(submodelId).createSubmodelElement(smElement);
public SubmodelElement createSubmodelElement(String submodelId, SubmodelElement smElement) {
return getConnectedSubmodelService(submodelId).createSubmodelElement(smElement);
}

@Override
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
getConnectedSubmodelService(submodelId).createSubmodelElement(idShortPath, smElement);
public SubmodelElement createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
return getConnectedSubmodelService(submodelId).createSubmodelElement(idShortPath, smElement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public interface SubmodelRepository {
* @param smElement
* the SubmodelElement
*/
public void createSubmodelElement(String submodelId, SubmodelElement smElement);
public SubmodelElement createSubmodelElement(String submodelId, SubmodelElement smElement);

/**
* Creates a nested SubmodelElement
Expand All @@ -176,7 +176,7 @@ public interface SubmodelRepository {
* @param smElement
* the SubmodelElement
*/
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException;
public SubmodelElement createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException;

/**
* Deletes a SubmodelElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public void setSubmodelElementValue(String idShortPath, SubmodelElementValue val
}

@Override
public void createSubmodelElement(SubmodelElement submodelElement) {
repoApi.createSubmodelElement(submodelId, submodelElement);
public SubmodelElement createSubmodelElement(SubmodelElement submodelElement) {
return repoApi.createSubmodelElement(submodelId, submodelElement);
}

@Override
public void createSubmodelElement(String idShortPath, SubmodelElement submodelElement)
public SubmodelElement createSubmodelElement(String idShortPath, SubmodelElement submodelElement)
throws ElementDoesNotExistException {
repoApi.createSubmodelElement(submodelId, idShortPath, submodelElement);
return repoApi.createSubmodelElement(submodelId, idShortPath, submodelElement);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ public void setSubmodelElementValue(String submodelId, String smeIdShortPath, Su
}

@Override
public void createSubmodelElement(String submodelId, SubmodelElement smElement) {
public SubmodelElement createSubmodelElement(String submodelId, SubmodelElement smElement) {
boolean isAuthorized = permissionResolver.hasPermission(Action.UPDATE, new SubmodelTargetInformation(getIdAsList(submodelId), getIdAsList(ALL_ALLOWED_WILDCARD)));

throwExceptionIfInsufficientPermission(isAuthorized);

decorated.createSubmodelElement(submodelId, smElement);
return decorated.createSubmodelElement(submodelId, smElement);
}

@Override
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
public SubmodelElement createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
boolean isAuthorized = permissionResolver.hasPermission(Action.UPDATE, new SubmodelTargetInformation(getIdAsList(submodelId), getIdAsList(idShortPath)));

throwExceptionIfInsufficientPermission(isAuthorized);

decorated.createSubmodelElement(submodelId, idShortPath, smElement);
return decorated.createSubmodelElement(submodelId, idShortPath, smElement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ public void setSubmodelElementValue(String submodelId, String idShortPath, Submo
}

@Override
public void createSubmodelElement(String submodelId, SubmodelElement smElement) {
decorated.createSubmodelElement(submodelId, smElement);
SubmodelElement submodelElement = decorated.getSubmodelElement(submodelId, smElement.getIdShort());
public SubmodelElement createSubmodelElement(String submodelId, SubmodelElement smElement) {
SubmodelElement submodelElement = decorated.createSubmodelElement(submodelId, smElement);
submodelElementCreated(submodelElement, getName(), submodelId, smElement.getIdShort());
return submodelElement;
}

@Override
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
decorated.createSubmodelElement(submodelId, idShortPath, smElement);
SubmodelElement submodelElement = decorated.getSubmodelElement(submodelId, idShortPath);
public SubmodelElement createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
SubmodelElement submodelElement = decorated.createSubmodelElement(submodelId, smElement);
submodelElementCreated(submodelElement, getName(), submodelId, idShortPath);
return submodelElement;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public void createSubmodelElementEvent() throws DeserializationException {
Submodel submodel = createSubmodelDummy("createSubmodelForElementEventId");
submodelRepository.createSubmodel(submodel);
SubmodelElement submodelElement = createSubmodelElementDummy("createSubmodelElementEventId");
submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);
SubmodelElement responseSubmodelElement = submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);

assertEquals(submodelElement, responseSubmodelElement);
assertEquals(topicFactory.createCreateSubmodelElementTopic(submodelRepository.getName(), submodel.getId(), submodelElement.getIdShort()), listener.lastTopic);
assertEquals(submodelElement, deserializeSubmodelElementPayload(listener.lastPayload));
}
Expand All @@ -138,7 +139,10 @@ public void updateSubmodelElementEvent() throws DeserializationException {
Submodel submodel = createSubmodelDummy("updateSubmodelForElementEventId");
submodelRepository.createSubmodel(submodel);
SubmodelElement submodelElement = createSubmodelElementDummy("updateSubmodelElementEventId");
submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);
SubmodelElement responseSubmodelElement = submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);

assertEquals(submodelElement, responseSubmodelElement);

SubmodelElementValue value = new PropertyValue("updatedValue");
submodelRepository.setSubmodelElementValue(submodel.getId(), submodelElement.getIdShort(), value);

Expand All @@ -151,7 +155,10 @@ public void deleteSubmodelElementEvent() throws DeserializationException {
Submodel submodel = createSubmodelDummy("deleteSubmodelForElementEventId");
submodelRepository.createSubmodel(submodel);
SubmodelElement submodelElement = createSubmodelElementDummy("deleteSubmodelElementEventId");
submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);
SubmodelElement responseSubmodelElement = submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);

assertEquals(submodelElement, responseSubmodelElement);

submodelRepository.deleteSubmodelElement(submodel.getId(), submodelElement.getIdShort());

assertEquals(topicFactory.createDeleteSubmodelElementTopic(submodelRepository.getName(), submodel.getId(), submodelElement.getIdShort()), listener.lastTopic);
Expand All @@ -165,8 +172,9 @@ public void createSubmodelElementWithoutValueEvent() throws DeserializationExcep
SubmodelElement submodelElement = createSubmodelElementDummy("noValueSubmodelElementEventId");
List<Qualifier> qualifierList = createNoValueQualifierList();
submodelElement.setQualifiers(qualifierList);
submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);
SubmodelElement responseSubmodelElement = submodelRepository.createSubmodelElement(submodel.getId(), submodelElement);

assertEquals(submodelElement, responseSubmodelElement);
assertEquals(topicFactory.createCreateSubmodelElementTopic(submodelRepository.getName(), submodel.getId(), submodelElement.getIdShort()), listener.lastTopic);
assertNotEquals(submodelElement, deserializeSubmodelElementPayload(listener.lastPayload));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ public void setSubmodelElementValue(String submodelId, String smeIdShort, Submod
}

@Override
public void createSubmodelElement(String submodelId, SubmodelElement smElement) {
decorated.createSubmodelElement(submodelId, smElement);
public SubmodelElement createSubmodelElement(String submodelId, SubmodelElement smElement) {
return decorated.createSubmodelElement(submodelId, smElement);
}

@Override
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
decorated.createSubmodelElement(submodelId, idShortPath, smElement);
public SubmodelElement createSubmodelElement(String submodelId, String idShortPath, SubmodelElement smElement) throws ElementDoesNotExistException {
return decorated.createSubmodelElement(submodelId, idShortPath, smElement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ public void setSubmodelElementValue(String submodelId, String idShortPath, Submo
}

@Override
public void createSubmodelElement(String submodelId, SubmodelElement submodelElement) {
decorated.createSubmodelElement(submodelId, submodelElement);
public SubmodelElement createSubmodelElement(String submodelId, SubmodelElement submodelElement) {
return decorated.createSubmodelElement(submodelId, submodelElement);
}

@Override
public void createSubmodelElement(String submodelId, String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
decorated.createSubmodelElement(submodelId, idShortPath, submodelElement);
public SubmodelElement createSubmodelElement(String submodelId, String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
return decorated.createSubmodelElement(submodelId, idShortPath, submodelElement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ public ResponseEntity<SubmodelElement> getSubmodelElementByPathSubmodelRepo(Base

@Override
public ResponseEntity<SubmodelElement> postSubmodelElementByPathSubmodelRepo(Base64UrlEncodedIdentifier submodelIdentifier, String idShortPath, @Valid SubmodelElement body, @Valid String level, @Valid String extent) {
repository.createSubmodelElement(submodelIdentifier.getIdentifier(), idShortPath, body);
return new ResponseEntity<SubmodelElement>(HttpStatus.CREATED);
SubmodelElement createdSME = repository.createSubmodelElement(submodelIdentifier.getIdentifier(), idShortPath, body);
return new ResponseEntity<SubmodelElement>(createdSME, HttpStatus.CREATED);
}

@Override
public ResponseEntity<SubmodelElement> postSubmodelElementSubmodelRepo(Base64UrlEncodedIdentifier submodelIdentifier, @Valid SubmodelElement body) {
repository.createSubmodelElement(submodelIdentifier.getIdentifier(), body);
return new ResponseEntity<SubmodelElement>(HttpStatus.CREATED);
SubmodelElement createdSME = repository.createSubmodelElement(submodelIdentifier.getIdentifier(), body);
return new ResponseEntity<SubmodelElement>(createdSME, HttpStatus.CREATED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ public void setSubmodelElementValue(String idShort, SubmodelElementValue value)
}

@Override
public void createSubmodelElement(SubmodelElement submodelElement) throws CollidingIdentifierException {
public SubmodelElement createSubmodelElement(SubmodelElement submodelElement) throws CollidingIdentifierException {
throwIfSubmodelElementExists(submodelElement.getIdShort());

List<SubmodelElement> smElements = submodel.getSubmodelElements();
smElements.add(submodelElement);
submodel.setSubmodelElements(smElements);
return submodelElement;
}

private void throwIfSubmodelElementExists(String submodelElementId) {
Expand All @@ -141,7 +142,7 @@ private void throwIfSubmodelElementExists(String submodelElementId) {
}

@Override
public void createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException, CollidingIdentifierException {
public SubmodelElement createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException, CollidingIdentifierException {
throwIfSubmodelElementExists(getFullIdShortPath(idShortPath, submodelElement.getIdShort()));

SubmodelElement parentSme = parser.getSubmodelElementFromIdShortPath(idShortPath);
Expand All @@ -150,15 +151,14 @@ public void createSubmodelElement(String idShortPath, SubmodelElement submodelEl
List<SubmodelElement> submodelElements = list.getValue();
submodelElements.add(submodelElement);
list.setValue(submodelElements);
return;
}
if (parentSme instanceof SubmodelElementCollection) {
SubmodelElementCollection collection = (SubmodelElementCollection) parentSme;
List<SubmodelElement> submodelElements = collection.getValue();
submodelElements.add(submodelElement);
collection.setValue(submodelElements);
return;
}
return submodelElement;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,21 @@ public void setSubmodelElementValue(String idShortPath, SubmodelElementValue val
}

@Override
public void createSubmodelElement(SubmodelElement submodelElement) {
public SubmodelElement createSubmodelElement(SubmodelElement submodelElement) {
InMemorySubmodelService inMemorySubmodelService = getInMemorySubmodelService();
inMemorySubmodelService.createSubmodelElement(submodelElement);
SubmodelElement createdSME = inMemorySubmodelService.createSubmodelElement(submodelElement);
Submodel submodel = inMemorySubmodelService.getSubmodel();
crudRepository.save(submodel);
return createdSME;
}

@Override
public void createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
public SubmodelElement createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
InMemorySubmodelService inMemorySubmodelService = getInMemorySubmodelService();
inMemorySubmodelService.createSubmodelElement(idShortPath, submodelElement);
SubmodelElement createdSME = inMemorySubmodelService.createSubmodelElement(idShortPath, submodelElement);
Submodel submodel = inMemorySubmodelService.getSubmodel();
crudRepository.save(submodel);
return createdSME;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public void setSubmodelElementValue(String idShortPath, SubmodelElementValue val
}

@Override
public void createSubmodelElement(SubmodelElement submodelElement) {
serviceApi.postSubmodelElement(submodelElement);
public SubmodelElement createSubmodelElement(SubmodelElement submodelElement) {
return serviceApi.postSubmodelElement(submodelElement);
}

@Override
public void createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
public SubmodelElement createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException {
try {
serviceApi.postSubmodelElementByPath(idShortPath, submodelElement);
return serviceApi.postSubmodelElementByPath(idShortPath, submodelElement);
} catch (ApiException e) {
throw mapExceptionSubmodelElementAccess(idShortPath, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public interface SubmodelService {
* Creates a Submodel Element
*
*/
public void createSubmodelElement(SubmodelElement submodelElement);
public SubmodelElement createSubmodelElement(SubmodelElement submodelElement);

/**
* Create a nested submodel element
Expand All @@ -101,16 +101,18 @@ public interface SubmodelService {
* the SubmodelElement IdShortPath
* @param submodelElement
* the submodel element to be created
* @return the SubmodelElement
* @throws ElementDoesNotExistException
* If the submodel element defined in the path does not exist
*/
public void createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException;
public SubmodelElement createSubmodelElement(String idShortPath, SubmodelElement submodelElement) throws ElementDoesNotExistException;

/**
* Updates a submodel element
*
* @param idShortPath
* @param submodelElement
* @return the SubmodelElement
* @throws ElementDoesNotExistException
* If the submodel element defined in the path does not exist
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ public static Property createDummyProperty(String idShort, String value, DataTyp
.build();
}

public static SubmodelElement createDummySME(String idShort, String value, DataTypeDefXsd dataType) {
return new DefaultProperty.Builder().idShort(idShort)
.category("cat1")
.value(value)
.valueType(dataType)
.build();
}

public static File createDummyFile(String idShort, String contentType, String value) {
return new DefaultFile.Builder().idShort(idShort)
.category("file")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ private DefaultEntity createDummyEntityWithStatement(SubmodelElement submodelEle
private DefaultProperty createDummyProperty(String idShort) {
return new DefaultProperty.Builder().idShort(idShort).category("cat1").value("123").valueType(DataTypeDefXsd.INTEGER).build();
}

private SubmodelElement createDummySME(String idShort) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please explain the need of this? Because I can't find any reference to this private method in this class.

return new DefaultProperty.Builder().idShort(idShort).value("123").build();
}

private InputStream getInputStreamOfDummyFile(String fileContent) throws FileNotFoundException, IOException {
return new ByteArrayInputStream(fileContent.getBytes());
Expand Down
Loading