diff options
5 files changed, 115 insertions, 16 deletions
diff --git a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/types/InternalItem.java b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/types/InternalItem.java index 3d6610b..7b3a6c5 100644 --- a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/types/InternalItem.java +++ b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/types/InternalItem.java @@ -19,7 +19,6 @@ package org.onap.sdc.common.versioning.persistence.types; import java.util.Date; -import java.util.EnumMap; import java.util.Map; import lombok.Getter; import lombok.Setter; @@ -32,8 +31,6 @@ import org.onap.sdc.common.versioning.services.types.VersionStatus; @Getter public class InternalItem extends Item { - private Map<VersionStatus, Integer> versionStatusCounters = new EnumMap<>(VersionStatus.class); - public void setId(String id) { this.id = id; } @@ -50,6 +47,10 @@ public class InternalItem extends Item { this.status = status; } + public void setVersionStatusCounters(Map<VersionStatus, Integer> versionStatusCounters) { + this.versionStatusCounters = versionStatusCounters; + } + public void addVersionStatus(VersionStatus versionStatus) { Integer counter = versionStatusCounters.get(versionStatus); versionStatusCounters.put(versionStatus, counter == null ? 1 : counter + 1); diff --git a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/zusammen/ItemZusammenDao.java b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/zusammen/ItemZusammenDao.java index 8530697..7fa55f0 100644 --- a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/zusammen/ItemZusammenDao.java +++ b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/persistence/zusammen/ItemZusammenDao.java @@ -4,6 +4,7 @@ import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.item.Info; import com.amdocs.zusammen.datatypes.item.Item; import java.util.Collection; +import java.util.EnumMap; import java.util.Map; import java.util.stream.Collectors; import org.onap.sdc.common.versioning.persistence.ItemDao; @@ -62,15 +63,12 @@ public class ItemZusammenDao implements ItemDao { item.setId(zusammenItem.getId().getValue()); item.setName(zusammenItem.getInfo().getName()); item.setDescription(zusammenItem.getInfo().getDescription()); - - zusammenItem.getInfo().getProperties().forEach((key, value) -> addPropertyToItem(key, value, item)); - item.setCreationTime(zusammenItem.getCreationTime()); item.setModificationTime(zusammenItem.getModificationTime()); + zusammenItem.getInfo().getProperties().forEach((key, value) -> addPropertyToItem(key, value, item)); if (item.getStatus() == null) { item.setStatus(ItemStatus.ACTIVE); - update(item); } return item; @@ -88,10 +86,12 @@ public class ItemZusammenDao implements ItemDao { item.setStatus(ItemStatus.valueOf((String) propertyValue)); break; case InfoPropertyName.ITEM_VERSIONS_STATUSES: + Map<VersionStatus, Integer> versionStatusCounters = new EnumMap<>(VersionStatus.class); for (Map.Entry<String, Number> statusCounter : ((Map<String, Number>) propertyValue).entrySet()) { - item.getVersionStatusCounters() + versionStatusCounters .put(VersionStatus.valueOf(statusCounter.getKey()), statusCounter.getValue().intValue()); } + item.setVersionStatusCounters(versionStatusCounters); break; default: item.addProperty(propertyKey, propertyValue); diff --git a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/impl/MajorVersionCalculatorImpl.java b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/impl/MajorVersionCalculatorImpl.java index a98fa58..69d8028 100644 --- a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/impl/MajorVersionCalculatorImpl.java +++ b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/impl/MajorVersionCalculatorImpl.java @@ -1,16 +1,17 @@ package org.onap.sdc.common.versioning.services.impl; -import java.util.HashSet; -import java.util.Set; import org.onap.sdc.common.versioning.services.types.Version; import org.onap.sdc.common.versioning.services.types.VersionCreationMethod; import org.onap.sdc.common.versioning.services.types.VersionStatus; import org.springframework.stereotype.Service; +import java.util.HashSet; +import java.util.Set; + @Service public class MajorVersionCalculatorImpl implements VersionCalculator { - private static final String INITIAL_VERSION = "1.0"; + private static final String INITIAL_VERSION = "0.0"; private static final String VERSION_STRING_VIOLATION_MSG = "Version string must be in the format of: {integer}.{integer}"; @@ -18,7 +19,7 @@ public class MajorVersionCalculatorImpl implements VersionCalculator { public String calculate(String baseVersion, VersionCreationMethod creationMethod) { if (baseVersion == null) { - return INITIAL_VERSION; + baseVersion = INITIAL_VERSION; } String[] versionLevels = baseVersion.split("\\."); @@ -26,10 +27,16 @@ public class MajorVersionCalculatorImpl implements VersionCalculator { throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG); } - int index = Integer.parseInt(versionLevels[0]); - index++; + int majorIndex = Integer.parseInt(versionLevels[0]); + int minorIndex = Integer.parseInt(versionLevels[1]); + if (creationMethod.equals(VersionCreationMethod.major)) { + majorIndex++; + minorIndex = 0; + } else { + minorIndex++; + } - return index + ".0"; + return majorIndex + "." + minorIndex; } @Override diff --git a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/types/Item.java b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/types/Item.java index 796fc1a..1d5338d 100644 --- a/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/types/Item.java +++ b/versioning-lib/src/main/java/org/onap/sdc/common/versioning/services/types/Item.java @@ -18,7 +18,9 @@ package org.onap.sdc.common.versioning.services.types; +import java.util.Collections; import java.util.Date; +import java.util.EnumMap; import java.util.HashMap; import java.util.Map; import lombok.Getter; @@ -30,7 +32,8 @@ public class Item { protected String id; protected Date creationTime; protected Date modificationTime; - protected ItemStatus status; + protected ItemStatus status = ItemStatus.ACTIVE; + protected Map<VersionStatus, Integer> versionStatusCounters = new EnumMap<>(VersionStatus.class); @Setter private String type; @Setter @@ -45,6 +48,10 @@ public class Item { return id == null; } + public Map<VersionStatus, Integer> getVersionStatusCounters() { + return Collections.unmodifiableMap(versionStatusCounters); + } + public void addProperty(String key, Object value) { properties.put(key, value); } diff --git a/versioning-lib/src/test/java/org/onap/sdc/common/versioning/services/impl/MajorVersionCalculatorImplTest.java b/versioning-lib/src/test/java/org/onap/sdc/common/versioning/services/impl/MajorVersionCalculatorImplTest.java new file mode 100644 index 0000000..a40723c --- /dev/null +++ b/versioning-lib/src/test/java/org/onap/sdc/common/versioning/services/impl/MajorVersionCalculatorImplTest.java @@ -0,0 +1,84 @@ +package org.onap.sdc.common.versioning.services.impl; + +import org.junit.Before; +import org.junit.Test; +import org.onap.sdc.common.versioning.services.types.VersionCreationMethod; + +import static org.junit.Assert.assertEquals; + +public class MajorVersionCalculatorImplTest { + + private MajorVersionCalculatorImpl calculator; + private static final String INITIAL_VERSION = "1.0"; + + @Before + public void init() { + calculator = new MajorVersionCalculatorImpl(); + } + + @Test + public void calculateInitialVersionWithNullAndMajor() { + String expectedInitialVersion = calculator.calculate(null, VersionCreationMethod.major); + assertEquals(expectedInitialVersion, INITIAL_VERSION); + } + + @Test + public void calculateInitialVersionWithNullAndMinor() { + String expectedValue = "0.1"; + String actualValue = calculator.calculate(null, VersionCreationMethod.minor); + assertEquals(expectedValue, actualValue); + } + + @Test + public void calculateMajorIncrement() { + String expectedValue = "2.0"; + String actualValue = calculator.calculate(INITIAL_VERSION, VersionCreationMethod.major); + assertEquals(expectedValue, actualValue); + } + + @Test + public void calculateMajorIncrementTwice() { + String expectedValue = "3.0"; + String firstValue = calculator.calculate(INITIAL_VERSION, VersionCreationMethod.major); + String actualValue = calculator.calculate(firstValue, VersionCreationMethod.major); + assertEquals(expectedValue, actualValue); + } + + @Test + public void calculateMinorIncrement() { + String expectedValue = "1.1"; + String actualValue = calculator.calculate(INITIAL_VERSION, VersionCreationMethod.minor); + assertEquals(expectedValue, actualValue); + } + + @Test + public void calculateMinorIncrementTwice() { + String expectedValue = "1.2"; + String firstValue = calculator.calculate(INITIAL_VERSION, VersionCreationMethod.minor); + String actualValue = calculator.calculate(firstValue, VersionCreationMethod.minor); + assertEquals(expectedValue, actualValue); + } + + @Test + public void calculateMinorIncrementEdgeCase() { + String expectedValue = "1.10"; + String initialValue = "1.9"; + String actualValue = calculator.calculate(initialValue, VersionCreationMethod.minor); + assertEquals(expectedValue, actualValue); + } + + @Test + public void calculateMajorIncrementEdgeCase() { + String expectedValue = "2.0"; + String initialValue = "1.7"; + String actualValue = calculator.calculate(initialValue, VersionCreationMethod.major); + assertEquals(expectedValue, actualValue); + } + + @Test(expected = IllegalArgumentException.class) + public void calculateException() { + String initialValue = "1"; + calculator.calculate(initialValue, VersionCreationMethod.minor); + } + +}
\ No newline at end of file |