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

Add (BASKET) support for UNIQUE and SET CONSTRAINTS #119

Merged
36 changes: 30 additions & 6 deletions src/main/java/ch/interlis/iox_j/validator/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ private void iterateThroughAllObjects(){
for (String basketId : objectPool.getBasketIds()){
// iterate through iomObjects
Iterator<IomObject> objectIterator = (objectPool.getObjectsOfBasketId(basketId)).valueIterator();
updateCurrentBasket(basketId);
while (objectIterator.hasNext()){
IomObject iomObj = objectIterator.next();
if(iomObj!=null){
Expand Down Expand Up @@ -869,11 +870,34 @@ private void iterateThroughAllObjects(){
}
}
}
for(SetConstraint setConstraint:setConstraints.keySet()){
if(setConstraint.perBasket()){
validateSetConstraint(setConstraint);
}
}
}
for(SetConstraint setConstraint:setConstraints.keySet()){
validateSetConstraint(setConstraint);
if(!setConstraint.perBasket()) {
validateSetConstraint(setConstraint);
}
}
}

private void updateCurrentBasket(String basketId) {
currentBasketId = basketId;
for (UniquenessConstraint uniquenessConstraint: seenUniqueConstraintValues.keySet()) {
if (uniquenessConstraint.perBasket()){
seenUniqueConstraintValues.get(uniquenessConstraint).clear();
}
}

for (SetConstraint setConstraint: setConstraints.keySet()){
if(setConstraint.perBasket() && setConstraints.get(setConstraint) != null){
setConstraints.get(setConstraint).clear();
}
}
}

private void validateConstraints(IomObject iomObj, Viewable classOfIomObj) {
if(!ValidationConfig.OFF.equals(constraintValidation)){
Viewable classOfCurrentObj=classOfIomObj;
Expand Down Expand Up @@ -977,7 +1001,6 @@ private void validateConstraintsOfEmbeddedAssociation(RoleDef role, IomObject io
if (propc >= 1) {
IomObject embeddedLinkObj = iomObj.getattrobj(role.getName(), 0);
AssociationDef roleOwner = (AssociationDef) role.getContainer();

Viewable classOfCurrentObj = roleOwner;
if (classOfCurrentObj != null) {
String iomObjOid = iomObj.getobjectoid();
Expand Down Expand Up @@ -1083,8 +1106,8 @@ private void validateUniquenessConstraint(IomObject parentObject,IomObject iomOb
if(!ValidationConfig.OFF.equals(constraintValidation)){
String constraintName = getScopedName(uniquenessConstraint);
String checkUniqueConstraint=null;
if(!enforceConstraintValidation){
checkUniqueConstraint=validationConfig.getConfigValue(constraintName, ValidationConfig.CHECK);
if(!enforceConstraintValidation) {
checkUniqueConstraint = validationConfig.getConfigValue(constraintName, ValidationConfig.CHECK);
}
if(ValidationConfig.OFF.equals(checkUniqueConstraint)){
if(!configOffOufputReduction.contains(ValidationConfig.CHECK+":"+constraintName)){
Expand Down Expand Up @@ -1301,8 +1324,9 @@ private void validateSetConstraint(SetConstraint setConstraint) {
}
logMsg(checkConstraint,msg);
} else {
if(!setConstraintOufputReduction.contains(setConstraint+":"+constraintName)){
setConstraintOufputReduction.add(setConstraint+":"+constraintName);
String constraintIdentifier = setConstraint+":"+constraintName+(setConstraint.perBasket() ? ":Basket("+currentBasketId+")" : "");
if(!setConstraintOufputReduction.contains(constraintIdentifier)){
setConstraintOufputReduction.add(constraintIdentifier);
logMsg(checkConstraint,rsrc.getString("validateSetConstraint.setConstraintIsNotTrue"), getDisplayName(setConstraint));
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/data/validator/SetConstraints24.ili
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
INTERLIS 2.4;

MODEL ModelA AT "mailto:test@localhost" VERSION "2023-09-05" =
TOPIC TopicA =
CLASS ClassA =
attr1 : MANDATORY 0..999;
SET CONSTRAINT SetConstraintGlobal: INTERLIS.objectCount(ALL) <= 3;
SET CONSTRAINT (BASKET) SetConstraintBasket: INTERLIS.objectCount(ALL) <= 2;
END ClassA;
END TopicA;
END ModelA.
11 changes: 11 additions & 0 deletions src/test/data/validator/UniqueConstraints24.ili
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
INTERLIS 2.4;

MODEL ModelA AT "mailto:test@localhost" VERSION "2023-09-05" =
TOPIC TopicA =
CLASS ClassA =
attr1 : MANDATORY 0..999;
UNIQUE UniqueConstraintGlobal: attr1;
UNIQUE (BASKET) UniqueConstraintBasket: attr1;
END ClassA;
END TopicA;
END ModelA.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ch.interlis.iox_j.validator;

import ch.interlis.iox.IoxLogEvent;
import org.junit.Assert;

import java.util.List;

public class LogCollectorAssertions {
public static void AssertContainsError(String text, int count, LogCollector logCollector){
Assert.assertEquals( String.format("Wrong number of error logs containing <%s> found.", text), count, CountOccurrences(text, logCollector.getErrs()));
Philippluca marked this conversation as resolved.
Show resolved Hide resolved
}

public static void AssertContainsWarning(String text, int count, LogCollector logCollector){
Assert.assertEquals(String.format("Wrong number of warning logs containing <%s> found.", text), count, CountOccurrences(text, logCollector.getWarn()));
}

public static void AssertContainsInfo(String text, int count, LogCollector logCollector){
Assert.assertEquals(String.format("Wrong number of info logs containing <%s> found.", text), count, CountOccurrences(text, logCollector.getInfo()));
}

private static int CountOccurrences(String text, List<IoxLogEvent> logEvents){
int i = 0;
for (IoxLogEvent event : logEvents) {
if (event.getEventMsg().contains(text)){
i++;
}
}
return i;
}
}
108 changes: 108 additions & 0 deletions src/test/java/ch/interlis/iox_j/validator/SetConstraints24Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package ch.interlis.iox_j.validator;

import ch.ehi.basics.settings.Settings;
import ch.interlis.ili2c.config.Configuration;
import ch.interlis.ili2c.config.FileEntry;
import ch.interlis.ili2c.config.FileEntryKind;
import ch.interlis.ili2c.metamodel.TransferDescription;
import ch.interlis.iom_j.Iom_jObject;
import ch.interlis.iox_j.EndBasketEvent;
import ch.interlis.iox_j.EndTransferEvent;
import ch.interlis.iox_j.ObjectEvent;
import ch.interlis.iox_j.StartBasketEvent;
import ch.interlis.iox_j.StartTransferEvent;
import ch.interlis.iox_j.logging.LogEventFactory;
import org.junit.Before;
import org.junit.Test;

import java.util.UUID;

import static org.junit.Assert.assertNotNull;

public class SetConstraints24Test {

private TransferDescription td=null;
private Validator validator=null;
private LogCollector logger=null;

private ObjectEvent CreateClassA(int attr1){
Iom_jObject iomJObject = new Iom_jObject("ModelA.TopicA.ClassA", UUID.randomUUID().toString());
iomJObject.addattrvalue("attr1", Integer.toString(attr1));
return new ObjectEvent(iomJObject);
}

@Before
public void setUp() throws Exception {
Configuration ili2cConfig=new Configuration();
FileEntry fileEntry=new FileEntry("src/test/data/validator/SetConstraints24.ili", FileEntryKind.ILIMODELFILE);
ili2cConfig.addFileEntry(fileEntry);
td=ch.interlis.ili2c.Ili2c.runCompiler(ili2cConfig);
assertNotNull(td);

ValidationConfig modelConfig=new ValidationConfig();
logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Settings settings=new Settings();
validator=new Validator(td, modelConfig, logger, errFactory, settings);
}

@Test
public void SetConstraintGlobal_Ok(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(3));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("SetConstraintGlobal",0, logger);
}

@Test
public void SetConstraintGlobal_Fail(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(3));
validator.validate(CreateClassA(4));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("SetConstraintGlobal", 1, logger);
}

@Test
public void SetConstraintBasket_Ok(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("SetConstraintBasket",0, logger);
}

@Test
public void SetConstraintBasket_Fail(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(CreateClassA(3));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(CreateClassA(3));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("SetConstraintBasket", 2, logger);
}
}
108 changes: 108 additions & 0 deletions src/test/java/ch/interlis/iox_j/validator/UniqueConstraints24Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package ch.interlis.iox_j.validator;

import ch.ehi.basics.settings.Settings;
import ch.interlis.ili2c.config.Configuration;
import ch.interlis.ili2c.config.FileEntry;
import ch.interlis.ili2c.config.FileEntryKind;
import ch.interlis.ili2c.metamodel.TransferDescription;
import ch.interlis.iom_j.Iom_jObject;
import ch.interlis.iox_j.EndBasketEvent;
import ch.interlis.iox_j.EndTransferEvent;
import ch.interlis.iox_j.ObjectEvent;
import ch.interlis.iox_j.StartBasketEvent;
import ch.interlis.iox_j.StartTransferEvent;
import ch.interlis.iox_j.logging.LogEventFactory;
import org.junit.Before;
import org.junit.Test;

import java.util.UUID;

import static org.junit.Assert.assertNotNull;

public class UniqueConstraints24Test {

private TransferDescription td=null;
private Validator validator=null;
private LogCollector logger=null;

private ObjectEvent CreateClassA(int attr1){
Iom_jObject iomJObject = new Iom_jObject("ModelA.TopicA.ClassA", UUID.randomUUID().toString());
iomJObject.addattrvalue("attr1", Integer.toString(attr1));
return new ObjectEvent(iomJObject);
}

@Before
public void setUp() throws Exception {
Configuration ili2cConfig=new Configuration();
FileEntry fileEntry=new FileEntry("src/test/data/validator/UniqueConstraints24.ili", FileEntryKind.ILIMODELFILE);
ili2cConfig.addFileEntry(fileEntry);
td=ch.interlis.ili2c.Ili2c.runCompiler(ili2cConfig);
assertNotNull(td);

ValidationConfig modelConfig=new ValidationConfig();
logger=new LogCollector();
LogEventFactory errFactory=new LogEventFactory();
Settings settings=new Settings();
validator=new Validator(td, modelConfig, logger, errFactory, settings);
}

@Test
public void UniqueConstraintGlobal_Ok(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(3));
validator.validate(CreateClassA(4));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("UniqueConstraintGlobal",0, logger);
}

@Test
public void UniqueConstraintGlobal_Fail(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(3));
validator.validate(CreateClassA(1));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("UniqueConstraintGlobal", 1, logger);
}

@Test
public void UniqueConstraintBasket_Ok(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("UniqueConstraintBasket",0, logger);
}

@Test
public void UniqueConstraintBasket_Fail(){
validator.validate(new StartTransferEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(2));
validator.validate(new EndBasketEvent());
validator.validate(new StartBasketEvent("ModelA.TopicA", UUID.randomUUID().toString()));
validator.validate(CreateClassA(1));
validator.validate(CreateClassA(1));
validator.validate(new EndBasketEvent());
validator.validate(new EndTransferEvent());
LogCollectorAssertions.AssertContainsError("UniqueConstraintBasket", 1, logger);
}
}