Skip to content

Commit

Permalink
Merge pull request #166 from com-pas/labels_to_ext_manager
Browse files Browse the repository at this point in the history
Extended the Extension Managers with methods for Labels.
  • Loading branch information
juancho0202 authored Sep 15, 2022
2 parents 87922f4 + f22f880 commit 205e97c
Show file tree
Hide file tree
Showing 24 changed files with 259 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
* For a description of the fields, see the XSD.
*/
public enum CompasExtensionsField {
SCL_NAME_EXTENSION("SclName"),
SCL_FILETYPE_EXTENSION("SclFileType"),
SCL_NAME("SclName"),
SCL_FILE_TYPE("SclFileType"),
LABELS("Labels"),
FLOW("Flow"),
CONNECTIVITY_NODE("ConnectivityNode"),
BAY("Bay"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_NAME_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_NAME;

class AbstractCompasExtensionsManagerTest {
private AbstractCompasExtensionsManager manager = new AbstractCompasExtensionsManager() {
};

@Test
void getCompasElement_WhenNullPassedAsList_ThenEmptyOptionalReturned() {
var result = manager.getCompasElement(null, SCL_NAME_EXTENSION);
var result = manager.getCompasElement(null, SCL_NAME);

assertFalse(result.isPresent());
}

@Test
void getCompasElement_WhenCalledWithListContainingSclName_ThenSclNameReturned() {
var elements = createElements(true);
var result = manager.getCompasElement(elements, SCL_NAME_EXTENSION);
var result = manager.getCompasElement(elements, SCL_NAME);

assertTrue(result.isPresent());
assertEquals("project", result.get().getValue());
Expand All @@ -36,22 +36,22 @@ void getCompasElement_WhenCalledWithListContainingSclName_ThenSclNameReturned()
@Test
void getCompasElement_WhenCalledWithListNotContainingSclName_ThenEmptyOptionalReturned() {
var elements = createElements(false);
var result = manager.getCompasElement(elements, SCL_NAME_EXTENSION);
var result = manager.getCompasElement(elements, SCL_NAME);

assertFalse(result.isPresent());
}

@Test
void getCompasValue_WhenNullPassedAsList_ThenEmptyOptionalReturned() {
var result = manager.getCompasValue(null, SCL_NAME_EXTENSION, String.class);
var result = manager.getCompasValue(null, SCL_NAME, String.class);

assertFalse(result.isPresent());
}

@Test
void getCompasValue_WhenCalledWithListContainingSclName_ThenSclNameReturned() {
var elements = createElements(true);
var result = manager.getCompasValue(elements, SCL_NAME_EXTENSION, String.class);
var result = manager.getCompasValue(elements, SCL_NAME, String.class);

assertTrue(result.isPresent());
assertEquals("project", result.get());
Expand All @@ -60,7 +60,7 @@ void getCompasValue_WhenCalledWithListContainingSclName_ThenSclNameReturned() {
@Test
void getCompasValue_WhenCalledWithListNotContainingSclName_ThenEmptyOptionalReturned() {
var elements = createElements(false);
var result = manager.getCompasValue(elements, SCL_NAME_EXTENSION, String.class);
var result = manager.getCompasValue(elements, SCL_NAME, String.class);

assertFalse(result.isPresent());
}
Expand All @@ -69,7 +69,7 @@ private List<Object> createElements(boolean withSclName) {
var elements = new ArrayList<>();

if (withSclName) {
var qname = new QName(CompasExtensionsConstants.COMPAS_EXTENSION_NS_URI, SCL_NAME_EXTENSION.getFieldName());
var qname = new QName(CompasExtensionsConstants.COMPAS_EXTENSION_NS_URI, SCL_NAME.getFieldName());
var jaxbElement = new JAXBElement<>(qname, String.class, "project");
elements.add(jaxbElement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_FILETYPE_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_NAME_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.*;

class CompasExtensionsFieldTest {
@Test
void enum_WhenRetrievingTheFieldName_ThenTheCorrectStringIsReturned() {
assertEquals("SclName", SCL_NAME_EXTENSION.getFieldName());
assertEquals("SclFileType", SCL_FILETYPE_EXTENSION.getFieldName());
assertEquals("SclName", SCL_NAME.getFieldName());
assertEquals("SclFileType", SCL_FILE_TYPE.getFieldName());
assertEquals("Labels", LABELS.getFieldName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import org.lfenergy.compas.scl.extensions.commons.AbstractCompasExtensionsManager;
import org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField;
import org.lfenergy.compas.scl.extensions.model.SclFileType;
import org.lfenergy.compas.scl.extensions.model.TCompasLabels;
import org.lfenergy.compas.scl2003.model.SCL;
import org.lfenergy.compas.scl2003.model.TPrivate;

import javax.xml.bind.JAXBElement;
import java.util.Optional;

import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsConstants.COMPAS_SCL_EXTENSION_TYPE;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_FILETYPE_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_NAME_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.*;

public class CompasExtensionsManager extends AbstractCompasExtensionsManager {
public Optional<TPrivate> getCompasPrivate(SCL scl) {
Expand All @@ -27,12 +27,16 @@ public Optional<TPrivate> getCompasPrivate(SCL scl) {
return Optional.empty();
}

public Optional<String> getCompasSclName(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, SCL_NAME_EXTENSION, String.class);
public Optional<TCompasLabels> getCompasLabels(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, LABELS, TCompasLabels.class);
}

public Optional<SclFileType> getCompasSclFileType(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, SCL_FILETYPE_EXTENSION, SclFileType.class);
return getCompasValue(compasPrivate, SCL_FILE_TYPE, SclFileType.class);
}

public Optional<String> getCompasSclName(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, SCL_NAME, String.class);
}

private <T> Optional<T> getCompasValue(TPrivate compasPrivate, CompasExtensionsField field, Class<T> clazz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsConstants.COMPAS_SCL_EXTENSION_TYPE;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_FILETYPE_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_NAME_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_FILE_TYPE;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_NAME;
import static org.lfenergy.compas.scl2003.util.ReadTestFile.readSCL;

class CompasExtensionsManagerTest {
Expand Down Expand Up @@ -44,7 +44,7 @@ void getCompasPrivate_WhenCalledWithoutPrivate_ThenNoCompasPrivateReturned() {

@Test
void getCompasElement_WhenCalledNullPassed_ThenNoNameReturned() {
var compasElement = manager.getCompasElement(null, SCL_NAME_EXTENSION);
var compasElement = manager.getCompasElement(null, SCL_NAME);

assertFalse(compasElement.isPresent());
}
Expand All @@ -55,7 +55,7 @@ void getCompasElement_WhenCalledWithoutSclName_ThenNoSclNameReturned() {

var compasPrivate = manager.getCompasPrivate(scl);
compasPrivate.ifPresentOrElse(tPrivate -> {
var compasElement = manager.getCompasElement(tPrivate, SCL_NAME_EXTENSION);
var compasElement = manager.getCompasElement(tPrivate, SCL_NAME);

assertFalse(compasElement.isPresent());
}, () -> fail(COMPAS_PRIVATE_NOT_FOUND));
Expand All @@ -67,7 +67,7 @@ void getCompasElement_WhenCalledWithSclName_ThenSclNameReturned() {

var compasPrivate = manager.getCompasPrivate(scl);
compasPrivate.ifPresentOrElse(tPrivate -> {
var compasElement = manager.getCompasElement(tPrivate, SCL_NAME_EXTENSION);
var compasElement = manager.getCompasElement(tPrivate, SCL_NAME);

assertTrue(compasElement.isPresent());
assertEquals("project", compasElement.get().getValue().toString());
Expand All @@ -80,7 +80,7 @@ void getCompasElement_WhenCalledWithoutFileType_ThenNoFileTypeReturned() {

var compasPrivate = manager.getCompasPrivate(scl);
compasPrivate.ifPresentOrElse(tPrivate -> {
var compasElement = manager.getCompasElement(tPrivate, SCL_FILETYPE_EXTENSION);
var compasElement = manager.getCompasElement(tPrivate, SCL_FILE_TYPE);

assertFalse(compasElement.isPresent());
}, () -> fail(COMPAS_PRIVATE_NOT_FOUND));
Expand All @@ -92,7 +92,7 @@ void getCompasElement_WhenCalledWithFileType_ThenFileTypeReturned() {

var compasPrivate = manager.getCompasPrivate(scl);
compasPrivate.ifPresentOrElse(tPrivate -> {
var compasElement = manager.getCompasElement(tPrivate, SCL_FILETYPE_EXTENSION);
var compasElement = manager.getCompasElement(tPrivate, SCL_FILE_TYPE);

assertTrue(compasElement.isPresent());
assertEquals(SclFileType.CID, compasElement.get().getValue());
Expand Down Expand Up @@ -163,6 +163,38 @@ void getCompasSclFileType_WhenCalledWithoutType_ThenNoTypeReturned() {
}, () -> fail(COMPAS_PRIVATE_NOT_FOUND));
}

@Test
void getCompasLabels_WhenCalledNullPassed_ThenNoLabelsReturned() {
var value = manager.getCompasLabels(null);

assertFalse(value.isPresent());
}

@Test
void getCompasLabels_WhenCalledWithLabels_ThenLabelsReturned() {
var scl = readSCL("scl_with_compas_private.scd");
var compasPrivate = manager.getCompasPrivate(scl);

compasPrivate.ifPresentOrElse(tPrivate -> {
var labels = manager.getCompasLabels(tPrivate);

assertTrue(labels.isPresent());
assertEquals("Label1", labels.get().getLabel().get(0));
}, () -> fail(COMPAS_PRIVATE_NOT_FOUND));
}

@Test
void getCompasLabels_WhenCalledWithoutLabels_ThenNoLabelsReturned() {
var scl = readSCL("scl_without_labels_compas_private.scd");

var compasPrivate = manager.getCompasPrivate(scl);
compasPrivate.ifPresentOrElse(tPrivate -> {
var labels = manager.getCompasLabels(tPrivate);

assertFalse(labels.isPresent());
}, () -> fail(COMPAS_PRIVATE_NOT_FOUND));
}

@Test
void createCompasPrivate_WhenCalled_ThenNewCompasPrivateReturned() {
var compasPrivate = manager.createCompasPrivate();
Expand Down
9 changes: 6 additions & 3 deletions scl2003/src/test/resources/scl/scl_with_compas_private.scd
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<!-- SPDX-FileCopyrightText: 2021 Alliander N.V. -->
<!-- -->
<!-- SPDX-License-Identifier: Apache-2.0 -->
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:ns2="https://www.lfenergy.org/compas/extension/v1">
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:compas="https://www.lfenergy.org/compas/extension/v1">
<Private type="compas-scl">
<ns2:SclName>project</ns2:SclName>
<ns2:SclFileType>CID</ns2:SclFileType>
<compas:SclName>project</compas:SclName>
<compas:SclFileType>CID</compas:SclFileType>
<compas:Labels>
<compas:Label>Label1</compas:Label>
</compas:Labels>
</Private>
<Header id="370e5d89-df3a-4d9b-a651-3dc5cb28bee1" version="1.0.0"/>
</SCL>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- SPDX-FileCopyrightText: 2021 Alliander N.V. -->
<!-- -->
<!-- SPDX-License-Identifier: Apache-2.0 -->
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:ns2="https://www.lfenergy.org/compas/extension/v1">
<SCL xmlns="http://www.iec.ch/61850/2003/SCL">
<Header id="370e5d89-df3a-4d9b-a651-3dc5cb28bee1" version="1.0.0"/>
</SCL>
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<!-- SPDX-FileCopyrightText: 2021 Alliander N.V. -->
<!-- -->
<!-- SPDX-License-Identifier: Apache-2.0 -->
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:ns2="https://www.lfenergy.org/compas/extension/v1">
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:compas="https://www.lfenergy.org/compas/extension/v1">
<Private type="compas-scl">
<ns2:SclName>project</ns2:SclName>
<compas:SclName>project</compas:SclName>
<compas:Labels>
<compas:Label>Label1</compas:Label>
</compas:Labels>
</Private>
<Header id="370e5d89-df3a-4d9b-a651-3dc5cb28bee1" version="1.0.0"/>
</SCL>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- SPDX-FileCopyrightText: 2021 Alliander N.V. -->
<!-- -->
<!-- SPDX-License-Identifier: Apache-2.0 -->
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:compas="https://www.lfenergy.org/compas/extension/v1">
<Private type="compas-scl">
<compas:SclName>project</compas:SclName>
<compas:SclFileType>CID</compas:SclFileType>
</Private>
<Header id="370e5d89-df3a-4d9b-a651-3dc5cb28bee1" version="1.0.0"/>
</SCL>
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<!-- SPDX-FileCopyrightText: 2021 Alliander N.V. -->
<!-- -->
<!-- SPDX-License-Identifier: Apache-2.0 -->
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:ns2="https://www.lfenergy.org/compas/extension/v1">
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:compas="https://www.lfenergy.org/compas/extension/v1">
<Private type="compas-scl">
<ns2:SclFileType>CID</ns2:SclFileType>
<compas:SclFileType>CID</compas:SclFileType>
<compas:Labels>
<compas:Label>Label1</compas:Label>
</compas:Labels>
</Private>
<Header id="370e5d89-df3a-4d9b-a651-3dc5cb28bee1" version="1.0.0"/>
</SCL>
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import org.lfenergy.compas.scl.extensions.commons.AbstractCompasExtensionsManager;
import org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField;
import org.lfenergy.compas.scl.extensions.model.SclFileType;
import org.lfenergy.compas.scl.extensions.model.TCompasLabels;
import org.lfenergy.compas.scl2007b.model.SCL;
import org.lfenergy.compas.scl2007b.model.TPrivate;

import javax.xml.bind.JAXBElement;
import java.util.Optional;

import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsConstants.COMPAS_SCL_EXTENSION_TYPE;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_FILETYPE_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.SCL_NAME_EXTENSION;
import static org.lfenergy.compas.scl.extensions.commons.CompasExtensionsField.*;

public class CompasExtensionsManager extends AbstractCompasExtensionsManager {
public Optional<TPrivate> getCompasPrivate(SCL scl) {
Expand All @@ -34,12 +34,16 @@ private <T> Optional<T> getCompasValue(TPrivate compasPrivate, CompasExtensionsF
return Optional.empty();
}

public Optional<SclFileType> getCompasSclFileType(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, SCL_FILE_TYPE, SclFileType.class);
}

public Optional<String> getCompasSclName(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, SCL_NAME_EXTENSION, String.class);
return getCompasValue(compasPrivate, SCL_NAME, String.class);
}

public Optional<SclFileType> getCompasSclFileType(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, SCL_FILETYPE_EXTENSION, SclFileType.class);
public Optional<TCompasLabels> getCompasLabels(TPrivate compasPrivate) {
return getCompasValue(compasPrivate, LABELS, TCompasLabels.class);
}

public TPrivate createCompasPrivate() {
Expand Down
Loading

0 comments on commit 205e97c

Please sign in to comment.