From 8d54fdee9b14e5e84b7840c0a7bb98334f22777b Mon Sep 17 00:00:00 2001 From: Joanna Jeremicz Date: Tue, 6 Nov 2018 15:09:13 +0100 Subject: Make CategoryParameter.getOptions() unmodifiable Change-Id: If7331577febe058d4415aab0d2336e60213fd7bb Issue-ID: VID-344 Signed-off-by: Joanna Jeremicz --- .../java/org/onap/vid/model/CategoryParameter.java | 3 ++- .../services/CategoryParameterServiceImplTest.java | 28 +++++++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) (limited to 'vid-app-common/src') diff --git a/vid-app-common/src/main/java/org/onap/vid/model/CategoryParameter.java b/vid-app-common/src/main/java/org/onap/vid/model/CategoryParameter.java index 91617ff42..85caac424 100644 --- a/vid-app-common/src/main/java/org/onap/vid/model/CategoryParameter.java +++ b/vid-app-common/src/main/java/org/onap/vid/model/CategoryParameter.java @@ -22,6 +22,7 @@ package org.onap.vid.model; * ============LICENSE_END========================================================= */ +import java.util.Collections; import java.util.HashSet; import java.util.Objects; import java.util.Set; @@ -82,7 +83,7 @@ public class CategoryParameter extends VidBaseEntity { @OneToMany(fetch = FetchType.EAGER, mappedBy = "categoryParameter") public Set getOptions() { - return options; + return Collections.unmodifiableSet(options); } public void setOptions(Set options) { diff --git a/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java index 6ac7b5447..7992cbb17 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/CategoryParameterServiceImplTest.java @@ -97,7 +97,8 @@ public class CategoryParameterServiceImplTest { AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest(); optionsRequest.options.add(OPTION_NAME); CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); - categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter)); + CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true); + categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter)); List aList = createCategoryParametersList(categoryParameter); String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY @@ -157,9 +158,10 @@ public class CategoryParameterServiceImplTest { @Test public void deleteCategoryOption_happyPath() { CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true); CategoryParameterOption categoryParameterOption = - new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter); - categoryParameter.getOptions().add(categoryParameterOption); + new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter); + categoryParameter.addOption(categoryParameterOption); List aList = createCategoryParametersList(categoryParameter); doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject()); @@ -173,9 +175,10 @@ public class CategoryParameterServiceImplTest { @Test public void getCategoryParametersTest() { CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); + CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true); CategoryParameterOption categoryParameterOption = - new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter); - categoryParameter.getOptions().add(categoryParameterOption); + new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter); + categoryParameter.addOption(categoryParameterOption); List aList = createCategoryParametersList(categoryParameter); doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject()); @@ -193,8 +196,9 @@ public class CategoryParameterServiceImplTest { public void updateCategoryParameterOption_domainObjectGetsSavedSuccessfully() { CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME); CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); - categoryParameter.getOptions().add( - new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, categoryParameter)); + CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true); + categoryParameter.addOption( + new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter)); List aList = createCategoryParametersList(categoryParameter); doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); @@ -209,7 +213,8 @@ public class CategoryParameterServiceImplTest { public void updateCategoryParameterOption_shouldFailUpdateForbidden() { CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("1", CATEGORY_NAME); CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, false); - categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter)); + CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, false); + categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter)); List aList = createCategoryParametersList(categoryParameter); doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); @@ -224,7 +229,8 @@ public class CategoryParameterServiceImplTest { CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME); CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); - categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter)); + CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true); + categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter)); List aList = createCategoryParametersList(categoryParameter); doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null); @@ -240,9 +246,9 @@ public class CategoryParameterServiceImplTest { CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true); CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true); - categoryParameter.getOptions().add( + categoryParameter.addOption( new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter)); - categoryParameter.getOptions().add( + categoryParameter.addOption( new CategoryParameterOption(APP_ID_SDC, OPTION_NAME, anotherCategoryParameter)); List aList = createCategoryParametersList(categoryParameter); -- cgit 1.2.3-korg