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

feat: add some unit tests #212

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
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,16 @@ public static boolean sameAs(Reference ref1, Reference ref2) {
}

/**
* Checks if two references are refering to the same element.
* Checks if two references are referring to the same element.
*
* @param ref1 reference 1
* @param ref2 reference 2
* @param compareReferredSemanticId true if referredSemanticId should be comparsed, false otherwise
* @return returns true if both references are refering to the same element, otherwise false
* @param compareReferredSemanticId true if referredSemanticId should be compared, false otherwise
* @return returns true if both references are referring to the same element, otherwise false
*/
public static boolean sameAs(Reference ref1, Reference ref2, boolean compareReferredSemanticId) {
boolean ref1Empty = ref1 == null || ref1.getKeys() == null || ref1.getKeys().isEmpty();
boolean ref2Empty = ref2 == null || ref2.getKeys() == null || ref2.getKeys().isEmpty();
if (ref1Empty && ref2Empty) {
return true;
}
if (ref1Empty != ref2Empty) {
return false;
}
Expand All @@ -259,6 +256,9 @@ public static boolean sameAs(Reference ref1, Reference ref2, boolean compareRefe
if (compareReferredSemanticId && !sameAs(ref1.getReferredSemanticID(), ref2.getReferredSemanticID())) {
return false;
}
if (ref1Empty && ref2Empty) {
return true;
}
if (ref1.getKeys().size() != ref2.getKeys().size()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V.
* Copyright (C) 2023 SAP SE or an SAP affiliate company.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,32 +16,34 @@
*/
package org.eclipse.digitaltwin.aas4j.v3.dataformat.core.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import junitparams.JUnitParamsRunner;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.AASFull;
import org.eclipse.digitaltwin.aas4j.v3.model.Environment;
import org.eclipse.digitaltwin.aas4j.v3.model.KeyTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.Operation;
import org.eclipse.digitaltwin.aas4j.v3.model.Referable;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.ReferenceTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultEnvironment;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultKey;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultReference;
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.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import junitparams.JUnitParamsRunner;
import org.eclipse.digitaltwin.aas4j.v3.model.Operation;
import org.eclipse.digitaltwin.aas4j.v3.model.ReferenceTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection;
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection;
import java.util.ArrayList;

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

@RunWith(JUnitParamsRunner.class)
public class AasUtilsTest {
Expand Down Expand Up @@ -277,6 +280,80 @@ public void whenSameAs_withDifferentKeyTypesButSameValues_success() {
Assert.assertTrue(AasUtils.sameAs(ref1, ref2));
}

@Test
public void whenSameAs_withoutKeys_success() {
Reference ref1 = new DefaultReference.Builder().type(ReferenceTypes.EXTERNAL_REFERENCE).build();
ref1.setKeys(null);
Reference ref2 = new DefaultReference.Builder().type(ReferenceTypes.EXTERNAL_REFERENCE).build();
ref2.setKeys(new ArrayList<>());
Assert.assertTrue(AasUtils.sameAs(ref1, ref2));
}

@Test
public void whenSameAs_withoutKeysAndDifferentTypes_fail() {
Reference ref1 = new DefaultReference.Builder().type(ReferenceTypes.EXTERNAL_REFERENCE).build();
ref1.setKeys(null);
Reference ref2 = new DefaultReference.Builder().type(ReferenceTypes.MODEL_REFERENCE).build();
ref2.setKeys(new ArrayList<>());
Assert.assertFalse(AasUtils.sameAs(ref1, ref2));
}

@Test
public void whenSameAs_withoutKeysAndDifferentSemaniticIDs_fail() {
Reference semanticId1 = new DefaultReference.Builder()
.keys(new DefaultKey.Builder()
.value("value1")
.build())
.build();
Reference semanticId2 = new DefaultReference.Builder()
.keys(new DefaultKey.Builder()
.type(KeyTypes.FRAGMENT_REFERENCE)
.value("value2")
.build())
.build();
Reference ref1 = new DefaultReference.Builder()
.referredSemanticID(semanticId1)
.build();
ref1.setKeys(null);
Reference ref2 = new DefaultReference.Builder()
.referredSemanticID(semanticId2)
.build();
ref2.setKeys(new ArrayList<>());
Assert.assertFalse(AasUtils.sameAs(ref1, ref2, true));
}

@Test
public void whenSameAs_withDifferentKeyTypesButSameValuesAndSemanticIDs_success() {
String value = "0173-1#01-ADS698#010";
Reference semanticId1 = new DefaultReference.Builder()
.keys(new DefaultKey.Builder()
.type(KeyTypes.GLOBAL_REFERENCE)
.value(value)
.build())
.build();
Reference semanticId2 = new DefaultReference.Builder()
.keys(new DefaultKey.Builder()
.type(KeyTypes.FRAGMENT_REFERENCE)
.value(value)
.build())
.build();
Reference ref1 = new DefaultReference.Builder()
.keys(new DefaultKey.Builder()
.type(KeyTypes.GLOBAL_REFERENCE)
.value(value)
.build())
.referredSemanticID(semanticId1)
.build();
Reference ref2 = new DefaultReference.Builder()
.keys(new DefaultKey.Builder()
.type(KeyTypes.FRAGMENT_REFERENCE)
.value(value)
.build())
.referredSemanticID(semanticId2)
.build();
Assert.assertTrue(AasUtils.sameAs(ref1, ref2, true));
}

@Test
public void whenSameAs_withDifferentKeyTypesAndValues_fail() {
Reference ref1 = new DefaultReference.Builder()
Expand Down Expand Up @@ -410,4 +487,26 @@ public void whenAsString_withSubmodelElementList_success() {
String actual = AasUtils.asString(reference);
Assert.assertEquals(expected, actual);
}

@Test
public void whenAsString_withReferredSemanticId_success() {
String value = "0173-1#01-ADS698#010";
Reference reference = new DefaultReference.Builder()
.type(ReferenceTypes.EXTERNAL_REFERENCE)
.referredSemanticID(new DefaultReference.Builder()
.keys(new DefaultKey.Builder()
.type(KeyTypes.GLOBAL_REFERENCE)
.value("foo")
.build())
.type(ReferenceTypes.MODEL_REFERENCE)
.build())
.keys(new DefaultKey.Builder()
.type(KeyTypes.GLOBAL_REFERENCE)
.value(value)
.build())
.build();
String expected = "[ExternalRef- [ModelRef](GlobalReference)foo -](GlobalReference)0173-1#01-ADS698#010";
String actual = AasUtils.asString(reference);
Assert.assertEquals(expected, actual);
}
}