Make CategoryParameter.getOptions() unmodifiable
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / CategoryParameterServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.vid.services;
22
23 import static org.mockito.Matchers.anyObject;
24 import static org.mockito.Matchers.anyString;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.reset;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.MockitoAnnotations.initMocks;
30
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import javax.ws.rs.ForbiddenException;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.onap.portalsdk.core.service.DataAccessService;
38 import org.onap.vid.category.AddCategoryOptionResponse;
39 import org.onap.vid.category.AddCategoryOptionsRequest;
40 import org.onap.vid.category.CategoryParameterOptionRep;
41 import org.onap.vid.category.CategoryParametersResponse;
42 import org.onap.vid.model.CategoryParameter;
43 import org.onap.vid.model.CategoryParameter.Family;
44 import org.onap.vid.model.CategoryParameterOption;
45 import org.onap.vid.services.CategoryParameterServiceImpl.AlreadyExistOptionNameException;
46 import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryException;
47 import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryOptionException;
48 import org.testng.Assert;
49 import org.testng.annotations.BeforeMethod;
50 import org.testng.annotations.BeforeSuite;
51 import org.testng.annotations.Test;
52
53 public class CategoryParameterServiceImplTest {
54
55     private static final String CATEGORY_NAME = "SAMPLE_CATEGORY_NAME";
56     private static final String OPTION_NAME = "SAMPLE_OPTION_NAME";
57     private static final String UNIQUE_OPTION_NAME = "UNIQUE_OPTION_NAME";
58     private static final String APP_ID_VID = "VID";
59     private static final String APP_ID_SDC = "SDC";
60     private static final String QUERY_STRING_FOR_CATEGORY_NAME = String.format(" where name = '%s' ", CATEGORY_NAME);
61
62
63     @Mock
64     private DataAccessService dataAccessService;
65
66     @InjectMocks
67     private CategoryParameterServiceImpl testSubject;
68
69     @BeforeSuite
70     public void before() {
71         initMocks(this);
72     }
73
74     @BeforeMethod
75     public void resetMocks() {
76         reset(dataAccessService);
77     }
78
79     @Test
80     public void createCategoryParameterOptions_happyPath()  {
81         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
82         optionsRequest.options.add(UNIQUE_OPTION_NAME);
83         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
84         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
85
86         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
87
88         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
89
90         Assert.assertTrue(result.getErrors().isEmpty());;
91         verify(dataAccessService, times(1))
92             .saveDomainObject(anyObject(), anyObject());
93     }
94
95     @Test
96     public void createCategoryParameterOptions_existingOptionsForCategory()  {
97         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
98         optionsRequest.options.add(OPTION_NAME);
99         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
100         CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
101         categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
102         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
103
104         String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
105             , OPTION_NAME, CATEGORY_NAME);
106
107         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
108
109         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
110
111         Assert.assertFalse(result.getErrors().isEmpty());
112         Assert.assertEquals(result.getErrors().size(), 1);
113         Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
114     }
115
116     private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
117         List<CategoryParameter> aList = new ArrayList<>();
118         aList.add(categoryParameter);
119         return aList;
120     }
121
122     @Test
123     public void createCategoryParameterOptions_nonExistingOptionsForCategory()  {
124         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
125
126         List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
127         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
128
129         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
130
131         Assert.assertTrue(result.getErrors().isEmpty());
132     }
133
134     @Test(expectedExceptions = { UnfoundedCategoryException.class })
135     public void createCategoryParameterOptions_wrongNumberOfCategoryParameters()  {
136         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
137         List<CategoryParameter> aList = Collections.emptyList();
138
139         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
140
141         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
142
143         Assert.fail();
144     }
145
146     @Test(expectedExceptions = { UnfoundedCategoryException.class })
147     public void deleteCategoryOption_wrongNumberOfParameters() {
148         CategoryParameterOption option = null;
149         List<CategoryParameter> aList = Collections.emptyList();
150
151         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
152
153         testSubject.deleteCategoryOption(CATEGORY_NAME, option);
154
155         Assert.fail();
156     }
157
158     @Test
159     public void deleteCategoryOption_happyPath() {
160         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
161         CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
162         CategoryParameterOption categoryParameterOption =
163             new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter);
164         categoryParameter.addOption(categoryParameterOption);
165         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
166
167         doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
168
169         testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
170
171         verify(dataAccessService, times(1))
172             .deleteDomainObject(anyObject(), anyObject());
173     }
174
175     @Test
176     public void getCategoryParametersTest() {
177         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
178         CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
179         CategoryParameterOption categoryParameterOption =
180             new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter);
181         categoryParameter.addOption(categoryParameterOption);
182         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
183
184         doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
185
186         CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
187
188         Assert.assertFalse(response.getCategoryParameters().isEmpty());
189         Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
190
191         verify(dataAccessService, times(1))
192             .getList(anyObject(), anyString(), anyString(), anyObject());
193     }
194
195     @Test
196     public void updateCategoryParameterOption_domainObjectGetsSavedSuccessfully() {
197         CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
198         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
199         CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
200         categoryParameter.addOption(
201             new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter));
202         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
203
204         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
205
206         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
207
208         verify(dataAccessService, times(1))
209             .saveDomainObject(anyObject(), anyObject());
210     }
211
212     @Test(expectedExceptions = { ForbiddenException.class })
213     public void updateCategoryParameterOption_shouldFailUpdateForbidden() {
214         CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("1", CATEGORY_NAME);
215         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, false);
216         CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, false);
217         categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
218         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
219
220         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
221
222         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
223
224         Assert.fail();
225     }
226
227     @Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
228     public void updateCategoryParameterOption_CategoryNotFound() {
229         CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
230
231         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
232         CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
233         categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
234         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
235
236         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
237
238         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
239
240         Assert.fail();
241     }
242
243     @Test(expectedExceptions = { AlreadyExistOptionNameException.class })
244     public void updateCategoryParameterOption_OptionNameExists() {
245         CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
246
247         CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
248         CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
249         categoryParameter.addOption(
250             new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter));
251         categoryParameter.addOption(
252             new CategoryParameterOption(APP_ID_SDC, OPTION_NAME, anotherCategoryParameter));
253         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
254
255         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
256
257         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
258
259         Assert.fail();
260     }
261
262     private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
263         CategoryParameter categoryParameter = new CategoryParameter();
264         categoryParameter.setName(categoryName);
265         categoryParameter.setIdSupported(idSupported);
266         return categoryParameter;
267     }
268 }