From 8349a946073283d89eb342940a6859dae4e7d7dd Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Fri, 1 Dec 2023 17:07:40 +0100 Subject: [PATCH] fix: added a check that the data set key is empty to prevent incorrect overwriting of value sets --- .../auth/attributes/AttributeValueKey.java | 4 ++++ .../auth/attributes/ValueSetFactory.java | 2 +- .../auth/attributes/ValueSetFactoryTest.java | 24 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactoryTest.java diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/AttributeValueKey.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/AttributeValueKey.java index 02dd77b623..8dfec3aa30 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/AttributeValueKey.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/AttributeValueKey.java @@ -56,6 +56,10 @@ public AttributeValueKey clone() { return new AttributeValueKey(ao, aot, role, type); } + public boolean isEmpty() { + return ao == null && aot == null && role == null && type == null; + } + @Override public boolean equals(Object object) { if (!(object instanceof AttributeValueKey)) { diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactory.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactory.java index a7a12136c0..dc05586edd 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactory.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactory.java @@ -8,7 +8,7 @@ public class ValueSetFactory { public static AttributeValueSet create(String value, QuerySolution qs, AttributeValueKey dataSetKey) { Optional type = getSetElementsType(qs); - if (!type.isPresent() || dataSetKey == null) { + if (!type.isPresent() || dataSetKey == null || dataSetKey.isEmpty()) { return new MutableAttributeValueSet(value); } else { AttributeValueKey avcKey = getAttributeValueSetKey(dataSetKey, type.get()); diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactoryTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactoryTest.java new file mode 100644 index 0000000000..8707a330ef --- /dev/null +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/auth/attributes/ValueSetFactoryTest.java @@ -0,0 +1,24 @@ +package edu.cornell.mannlib.vitro.webapp.auth.attributes; + +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import org.apache.jena.query.QuerySolutionMap; +import org.apache.jena.rdf.model.ResourceFactory; +import org.junit.Test; + +public class ValueSetFactoryTest { + + @Test + public void testCreate() { + QuerySolutionMap qs = new QuerySolutionMap(); + qs.add("setElementsType", ResourceFactory.createPlainLiteral("some type")); + AttributeValueKey key = new AttributeValueKey(); + String oldValue = "value"; + AttributeValueSet valueSet1 = ValueSetFactory.create(oldValue, qs, key); + assertTrue(valueSet1.contains(oldValue)); + String newValue = "new value"; + AttributeValueSet valueSet2 = ValueSetFactory.create(newValue, qs, key); + assertNotEquals(valueSet1, valueSet2); + } +}