Skip to content

Commit f1d2f03

Browse files
author
Rob Tjalma
authored
Merge pull request #89 from com-pas/add-comments
Implemented adding comments when doing create or update of SCL.
2 parents 5d1ea0d + 58742c7 commit f1d2f03

File tree

16 files changed

+266
-46
lines changed

16 files changed

+266
-46
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Below environment variable(s) can be used to configure which claims are used to
8888
| Environment variable | Java Property | Description | Example |
8989
| -------------------------------- | ------------------------------ | --------------------------------------------- | ---------------- |
9090
| USERINFO_NAME_CLAIMNAME | compas.userinfo.name.claimname | The Name of the user logged in. | name |
91+
| USERINFO_WHO_CLAIMNAME | compas.userinfo.who.claimname | The Name of the user used in the Who History. | name |
9192

9293
## Security
9394

app/src/main/java/org/lfenergy/compas/scl/data/rest/UserInfoProperties.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
public interface UserInfoProperties {
1111
@WithName("name.claimname")
1212
String name();
13+
14+
@WithName("who.claimname")
15+
String who();
1316
}

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclDataResource.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
package org.lfenergy.compas.scl.data.rest.v1;
55

66
import io.quarkus.security.Authenticated;
7+
import org.eclipse.microprofile.jwt.JsonWebToken;
78
import org.lfenergy.compas.core.commons.ElementConverter;
89
import org.lfenergy.compas.scl.data.model.SclType;
910
import org.lfenergy.compas.scl.data.model.Version;
11+
import org.lfenergy.compas.scl.data.rest.UserInfoProperties;
1012
import org.lfenergy.compas.scl.data.rest.v1.model.*;
1113
import org.lfenergy.compas.scl.data.service.CompasSclDataService;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1216

1317
import javax.enterprise.context.RequestScoped;
1418
import javax.inject.Inject;
@@ -23,9 +27,17 @@
2327
@RequestScoped
2428
@Path("/scl/v1/{" + TYPE_PATH_PARAM + "}")
2529
public class CompasSclDataResource {
30+
private static final Logger LOGGER = LoggerFactory.getLogger(CompasSclDataResource.class);
31+
2632
private final CompasSclDataService compasSclDataService;
2733
private final ElementConverter converter;
2834

35+
@Inject
36+
JsonWebToken jsonWebToken;
37+
38+
@Inject
39+
UserInfoProperties userInfoProperties;
40+
2941
@Inject
3042
public CompasSclDataResource(CompasSclDataService compasSclDataService,
3143
ElementConverter converter) {
@@ -38,8 +50,11 @@ public CompasSclDataResource(CompasSclDataService compasSclDataService,
3850
@Produces(MediaType.APPLICATION_XML)
3951
public CreateResponse create(@PathParam(TYPE_PATH_PARAM) SclType type,
4052
@Valid CreateRequest request) {
53+
String who = jsonWebToken.getClaim(userInfoProperties.who());
54+
LOGGER.trace("Username used for Who {}", who);
55+
4156
var response = new CreateResponse();
42-
response.setId(compasSclDataService.create(type, request.getName(), request.getElements().get(0)));
57+
response.setId(compasSclDataService.create(type, request.getName(), who, request.getComment(), request.getElements().get(0)));
4358
return response;
4459
}
4560

@@ -109,7 +124,10 @@ public String findRawSCLByUUIDAndVersion(@PathParam(TYPE_PATH_PARAM) SclType typ
109124
public void update(@PathParam(TYPE_PATH_PARAM) SclType type,
110125
@PathParam(ID_PATH_PARAM) UUID id,
111126
@Valid UpdateRequest request) {
112-
compasSclDataService.update(type, id, request.getChangeSetType(), request.getElements().get(0));
127+
String who = jsonWebToken.getClaim(userInfoProperties.who());
128+
LOGGER.trace("Username used for Who {}", who);
129+
130+
compasSclDataService.update(type, id, request.getChangeSetType(), who, request.getComment(), request.getElements().get(0));
113131
}
114132

115133
@DELETE

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/model/CreateRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class CreateRequest {
2626
@XmlElement(name = "Name", namespace = SCL_DATA_SERVICE_V1_NS_URI, required = true)
2727
private String name;
2828

29+
@Schema(description = "Comment that will be added to the new history record.")
30+
@XmlElement(name = "Comment", namespace = SCL_DATA_SERVICE_V1_NS_URI)
31+
private String comment;
32+
2933
@Size(min = 1, max = 1, message = "{org.lfenergy.compas.XmlAnyElementValid.moreElements.message}")
3034
@XmlAnyElementValid(elementName = "SCL", elementNamespace = SCL_NS_URI)
3135
@Schema(description = "Can contain one element, named 'SCL', containing a SCL XML Definition")
@@ -40,6 +44,14 @@ public void setName(String name) {
4044
this.name = name;
4145
}
4246

47+
public String getComment() {
48+
return comment;
49+
}
50+
51+
public void setComment(String comment) {
52+
this.comment = comment;
53+
}
54+
4355
public List<Element> getElements() {
4456
return elements;
4557
}

app/src/main/java/org/lfenergy/compas/scl/data/rest/v1/model/UpdateRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public class UpdateRequest {
2828
@XmlElement(name = "ChangeSet", namespace = SCL_DATA_SERVICE_V1_NS_URI, required = true)
2929
private ChangeSetType changeSetType;
3030

31+
@Schema(description = "Comment that will be added to the new history record.")
32+
@XmlElement(name = "Comment", namespace = SCL_DATA_SERVICE_V1_NS_URI)
33+
private String comment;
34+
3135
@XmlAnyElement
3236
@Size(min = 1, max = 1, message = "{org.lfenergy.compas.XmlAnyElementValid.moreElements.message}")
3337
@Schema(description = "Can contain one element, named 'SCL', containing a SCL XML Definition")
@@ -42,6 +46,14 @@ public void setChangeSetType(ChangeSetType changeSetType) {
4246
this.changeSetType = changeSetType;
4347
}
4448

49+
public String getComment() {
50+
return comment;
51+
}
52+
53+
public void setComment(String comment) {
54+
this.comment = comment;
55+
}
56+
4557
public List<Element> getElements() {
4658
return elements;
4759
}

app/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
compas.userinfo.name.claimname = ${USERINFO_NAME_CLAIMNAME:name}
6+
compas.userinfo.who.claimname = ${USERINFO_WHO_CLAIMNAME:name}
67

78
quarkus.http.cors = false
89
quarkus.http.root-path = /compas-scl-data-service/

app/src/test/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclDataResourceAsEditorTest.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import io.quarkus.test.junit.QuarkusTest;
88
import io.quarkus.test.junit.mockito.InjectMock;
99
import io.quarkus.test.security.TestSecurity;
10+
import io.quarkus.test.security.jwt.Claim;
11+
import io.quarkus.test.security.jwt.JwtSecurity;
1012
import io.restassured.http.ContentType;
1113
import org.junit.jupiter.api.Test;
1214
import org.lfenergy.compas.core.commons.ElementConverter;
@@ -33,8 +35,14 @@
3335

3436
@QuarkusTest
3537
@TestHTTPEndpoint(CompasSclDataResource.class)
36-
@TestSecurity(user = "test-reader", roles = {"SCD_" + READ_ROLE, "SCD_" + CREATE_ROLE, "SCD_" + UPDATE_ROLE, "SCD_" + DELETE_ROLE})
38+
@TestSecurity(user = "test-editor", roles = {"SCD_" + READ_ROLE, "SCD_" + CREATE_ROLE, "SCD_" + UPDATE_ROLE, "SCD_" + DELETE_ROLE})
39+
@JwtSecurity(claims = {
40+
// Default the claim "name" is configured for Who, so we will set this claim for the test.
41+
@Claim(key = "name", value = CompasSclDataResourceAsEditorTest.USERNAME)
42+
})
3743
class CompasSclDataResourceAsEditorTest {
44+
public static final String USERNAME = "Test Editor";
45+
3846
@InjectMock
3947
private CompasSclDataService compasSclDataService;
4048

@@ -192,14 +200,16 @@ void create_WhenCalled_ThenServiceCalledAndUUIDRetrieved() {
192200
var uuid = UUID.randomUUID();
193201
var type = SclType.SCD;
194202
var name = "StationName";
203+
var comment = "Some comments";
195204
var scl = readSCL();
196205

197206
var request = new CreateRequest();
198207
request.setName(name);
208+
request.setComment(comment);
199209
request.setElements(new ArrayList<>());
200210
request.getElements().add(scl);
201211

202-
when(compasSclDataService.create(eq(type), eq(name), any(Element.class))).thenReturn(uuid);
212+
when(compasSclDataService.create(eq(type), eq(name), eq(USERNAME), eq(comment), any(Element.class))).thenReturn(uuid);
203213

204214
var response = given()
205215
.pathParam(TYPE_PATH_PARAM, type)
@@ -212,22 +222,24 @@ void create_WhenCalled_ThenServiceCalledAndUUIDRetrieved() {
212222
.response();
213223

214224
assertEquals(uuid.toString(), response.xmlPath().getString("CreateResponse.Id"));
215-
verify(compasSclDataService, times(1)).create(eq(type), eq(name), any(Element.class));
225+
verify(compasSclDataService, times(1)).create(eq(type), eq(name), eq(USERNAME), eq(comment), any(Element.class));
216226
}
217227

218228
@Test
219229
void update_WhenCalled_ThenServiceCalledAndNewUUIDRetrieved() {
220230
var uuid = UUID.randomUUID();
221231
var type = SclType.SCD;
222232
var changeSetType = ChangeSetType.MAJOR;
233+
var comment = "Some comments";
223234
var scl = readSCL();
224235

225236
var request = new UpdateRequest();
226237
request.setChangeSetType(changeSetType);
238+
request.setComment(comment);
227239
request.setElements(new ArrayList<>());
228240
request.getElements().add(scl);
229241

230-
doNothing().when(compasSclDataService).update(eq(type), eq(uuid), eq(changeSetType), any(Element.class));
242+
doNothing().when(compasSclDataService).update(eq(type), eq(uuid), eq(changeSetType), eq(USERNAME), eq(comment), any(Element.class));
231243

232244
given()
233245
.pathParam(TYPE_PATH_PARAM, type)
@@ -238,7 +250,7 @@ void update_WhenCalled_ThenServiceCalledAndNewUUIDRetrieved() {
238250
.then()
239251
.statusCode(204);
240252

241-
verify(compasSclDataService, times(1)).update(eq(type), eq(uuid), eq(changeSetType), any(Element.class));
253+
verify(compasSclDataService, times(1)).update(eq(type), eq(uuid), eq(changeSetType), eq(USERNAME), eq(comment), any(Element.class));
242254
}
243255

244256
@Test

app/src/test/java/org/lfenergy/compas/scl/data/rest/v1/CompasSclDataResourceAsReaderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ void create_WhenCalled_ThenServiceCalledAndUUIDRetrieved() {
196196
var uuid = UUID.randomUUID();
197197
var type = getType();
198198
var name = "StationName";
199+
var comment = "Some comments";
199200
var scl = readSCL();
200201

201202
var request = new CreateRequest();
202203
request.setName(name);
204+
request.setComment(comment);
203205
request.setElements(new ArrayList<>());
204206
request.getElements().add(scl);
205207

206-
when(compasSclDataService.create(eq(type), eq(name), any(Element.class))).thenReturn(uuid);
207-
208208
given()
209209
.pathParam(TYPE_PATH_PARAM, type)
210210
.contentType(ContentType.XML)
@@ -221,15 +221,15 @@ void update_WhenCalled_ThenServiceCalledAndNewUUIDRetrieved() {
221221
var uuid = UUID.randomUUID();
222222
var type = getType();
223223
var changeSetType = ChangeSetType.MAJOR;
224+
var comment = "Some comments";
224225
var scl = readSCL();
225226

226227
var request = new UpdateRequest();
227228
request.setChangeSetType(changeSetType);
229+
request.setComment(comment);
228230
request.setElements(new ArrayList<>());
229231
request.getElements().add(scl);
230232

231-
doNothing().when(compasSclDataService).update(eq(type), eq(uuid), eq(changeSetType), any(Element.class));
232-
233233
given()
234234
.pathParam(TYPE_PATH_PARAM, type)
235235
.pathParam(ID_PATH_PARAM, uuid)

repository-basex/src/test/java/org/lfenergy/compas/scl/data/repository/CompasSclDataBaseXRepositoryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,15 @@ private String getIdFromHeader(Element scl) {
291291
var header = processor.getSclHeader(scl)
292292
.orElseThrow(() ->
293293
new CompasSclDataServiceException(HEADER_NOT_FOUND_ERROR_CODE, "Header not found in SCL!"));
294-
return processor.getAttributeValue(header, SCL_HEADER_ID_ATTR)
294+
return processor.getAttributeValue(header, SCL_ID_ATTR)
295295
.orElse("");
296296
}
297297

298298
private String getVersionFromHeader(Element scl) {
299299
var header = processor.getSclHeader(scl)
300300
.orElseThrow(() ->
301301
new CompasSclDataServiceException(HEADER_NOT_FOUND_ERROR_CODE, "Header not found in SCL!"));
302-
return processor.getAttributeValue(header, SCL_HEADER_VERSION_ATTR)
302+
return processor.getAttributeValue(header, SCL_VERSION_ATTR)
303303
.orElse("");
304304
}
305305

@@ -309,8 +309,8 @@ private Element readSCL(UUID uuid, Version version) {
309309

310310
var scl = converter.convertToElement(inputStream, SCL_ELEMENT_NAME, SCL_NS_URI);
311311
var header = processor.getSclHeader(scl).orElseGet(() -> processor.addSclHeader(scl));
312-
header.setAttribute(SCL_HEADER_ID_ATTR, uuid.toString());
313-
header.setAttribute(SCL_HEADER_VERSION_ATTR, version.toString());
312+
header.setAttribute(SCL_ID_ATTR, uuid.toString());
313+
header.setAttribute(SCL_VERSION_ATTR, version.toString());
314314
return scl;
315315
}
316316
}

repository/src/main/java/org/lfenergy/compas/scl/data/SclDataServiceConstants.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ public class SclDataServiceConstants {
1313
public static final String SCL_NS_URI = "http://www.iec.ch/61850/2003/SCL";
1414
public static final String SCL_NS_PREFIX = "";
1515
public static final String SCL_ELEMENT_NAME = "SCL";
16+
public static final String SCL_ID_ATTR = "id";
17+
public static final String SCL_VERSION_ATTR = "version";
18+
public static final String SCL_REVISION_ATTR = "revision";
19+
1620
public static final String SCL_HEADER_ELEMENT_NAME = "Header";
1721
public static final String SCL_PRIVATE_ELEMENT_NAME = "Private";
1822
public static final String SCL_PRIVATE_TYPE_ATTR = "type";
19-
public static final String SCL_HEADER_ID_ATTR = "id";
20-
public static final String SCL_HEADER_VERSION_ATTR = "version";
23+
public static final String SCL_HISTORY_ELEMENT_NAME = "History";
24+
public static final String SCL_HITEM_ELEMENT_NAME = "Hitem";
25+
public static final String SCL_WHO_ATTR = "who";
26+
public static final String SCL_WHEN_ATTR = "when";
27+
public static final String SCL_WHAT_ATTR = "what";
2128

2229
public static final String COMPAS_EXTENSION_NS_URI = "https://www.lfenergy.org/compas/extension/v1";
2330
public static final String COMPAS_EXTENSION_NS_PREFIX = "compas";

0 commit comments

Comments
 (0)