2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 - 2019 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.onap.portalsdk.core.service.DataAccessService;
26 import org.onap.vid.category.AddCategoryOptionResponse;
27 import org.onap.vid.category.AddCategoryOptionsRequest;
28 import org.onap.vid.category.CategoryParameterOptionRep;
29 import org.onap.vid.category.CategoryParametersResponse;
30 import org.onap.vid.model.CategoryParameter;
31 import org.onap.vid.model.CategoryParameter.Family;
32 import org.onap.vid.model.CategoryParameterOption;
33 import org.onap.vid.services.CategoryParameterServiceImpl.AlreadyExistOptionNameException;
34 import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryException;
35 import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryOptionException;
36 import org.testng.Assert;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.BeforeSuite;
39 import org.testng.annotations.Test;
41 import javax.ws.rs.ForbiddenException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.anyString;
48 import static org.mockito.Mockito.*;
49 import static org.mockito.MockitoAnnotations.initMocks;
51 public class CategoryParameterServiceImplTest {
53 private static final String CATEGORY_NAME = "SAMPLE_CATEGORY_NAME";
54 private static final String OPTION_NAME = "SAMPLE_OPTION_NAME";
55 private static final String UNIQUE_OPTION_NAME = "UNIQUE_OPTION_NAME";
56 private static final String APP_ID_VID = "VID";
57 private static final String APP_ID_SDC = "SDC";
58 private static final String QUERY_STRING_FOR_CATEGORY_NAME = String.format(" where name = '%s' ", CATEGORY_NAME);
62 private DataAccessService dataAccessService;
65 private CategoryParameterServiceImpl testSubject;
68 public void before() {
73 public void resetMocks() {
74 reset(dataAccessService);
78 public void createCategoryParameterOptions_happyPath() {
79 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
80 optionsRequest.options.add(UNIQUE_OPTION_NAME);
81 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
82 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
84 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
86 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
88 Assert.assertTrue(result.getErrors().isEmpty());;
89 verify(dataAccessService, times(1))
90 .saveDomainObject(any(), any());
94 public void createCategoryParameterOptions_existingOptionsForCategory() {
95 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
96 optionsRequest.options.add(OPTION_NAME);
97 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
98 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
99 categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
100 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
102 String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
103 , OPTION_NAME, CATEGORY_NAME);
105 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
107 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
109 Assert.assertFalse(result.getErrors().isEmpty());
110 Assert.assertEquals(result.getErrors().size(), 1);
111 Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
114 private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
115 List<CategoryParameter> aList = new ArrayList<>();
116 aList.add(categoryParameter);
121 public void createCategoryParameterOptions_nonExistingOptionsForCategory() {
122 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
124 List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
125 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
127 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
129 Assert.assertTrue(result.getErrors().isEmpty());
132 @Test(expectedExceptions = { UnfoundedCategoryException.class })
133 public void createCategoryParameterOptions_wrongNumberOfCategoryParameters() {
134 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
135 List<CategoryParameter> aList = Collections.emptyList();
137 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
139 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
144 @Test(expectedExceptions = { UnfoundedCategoryException.class })
145 public void deleteCategoryOption_wrongNumberOfParameters() {
146 CategoryParameterOption option = null;
147 List<CategoryParameter> aList = Collections.emptyList();
149 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
151 testSubject.deleteCategoryOption(CATEGORY_NAME, option);
157 public void deleteCategoryOption_happyPath() {
158 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
159 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
160 CategoryParameterOption categoryParameterOption =
161 new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter);
162 categoryParameter.addOption(categoryParameterOption);
163 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
165 doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
167 testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
169 verify(dataAccessService, times(1))
170 .deleteDomainObject(any(), any());
174 public void getCategoryParametersTest() {
175 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
176 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
177 CategoryParameterOption categoryParameterOption =
178 new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter);
179 categoryParameter.addOption(categoryParameterOption);
180 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
182 doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
184 CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
186 Assert.assertFalse(response.getCategoryParameters().isEmpty());
187 Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
189 verify(dataAccessService, times(1))
190 .getList(any(), anyString(), any(), any());
194 public void updateCategoryParameterOption_domainObjectGetsSavedSuccessfully() {
195 CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
196 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
197 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
198 categoryParameter.addOption(
199 new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter));
200 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
202 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
204 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
206 verify(dataAccessService, times(1))
207 .saveDomainObject(any(), any());
210 @Test(expectedExceptions = { ForbiddenException.class })
211 public void updateCategoryParameterOption_shouldFailUpdateForbidden() {
212 CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("1", CATEGORY_NAME);
213 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, false);
214 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, false);
215 categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
216 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
218 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
220 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
225 @Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
226 public void updateCategoryParameterOption_CategoryNotFound() {
227 CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
229 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
230 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
231 categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
232 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
234 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
236 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
241 @Test(expectedExceptions = { AlreadyExistOptionNameException.class })
242 public void updateCategoryParameterOption_OptionNameExists() {
243 CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
245 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
246 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
247 categoryParameter.addOption(
248 new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter));
249 categoryParameter.addOption(
250 new CategoryParameterOption(APP_ID_SDC, OPTION_NAME, anotherCategoryParameter));
251 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
253 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
255 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
260 private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
261 CategoryParameter categoryParameter = new CategoryParameter();
262 categoryParameter.setName(categoryName);
263 categoryParameter.setIdSupported(idSupported);
264 return categoryParameter;