2 * ============LICENSE_START=======================================================
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
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.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter));
99 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
101 String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
102 , OPTION_NAME, CATEGORY_NAME);
104 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
106 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
108 Assert.assertFalse(result.getErrors().isEmpty());
109 Assert.assertEquals(result.getErrors().size(), 1);
110 Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
113 private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
114 List<CategoryParameter> aList = new ArrayList<>();
115 aList.add(categoryParameter);
120 public void createCategoryParameterOptions_nonExistingOptionsForCategory() {
121 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
123 List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
124 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
126 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
128 Assert.assertTrue(result.getErrors().isEmpty());
131 @Test(expectedExceptions = { UnfoundedCategoryException.class })
132 public void createCategoryParameterOptions_wrongNumberOfCategoryParameters() {
133 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
134 List<CategoryParameter> aList = Collections.emptyList();
136 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
138 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
143 @Test(expectedExceptions = { UnfoundedCategoryException.class })
144 public void deleteCategoryOption_wrongNumberOfParameters() {
145 CategoryParameterOption option = null;
146 List<CategoryParameter> aList = Collections.emptyList();
148 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
150 testSubject.deleteCategoryOption(CATEGORY_NAME, option);
156 public void deleteCategoryOption_happyPath() {
157 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
158 CategoryParameterOption categoryParameterOption =
159 new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter);
160 categoryParameter.getOptions().add(categoryParameterOption);
161 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
163 doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
165 testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
167 verify(dataAccessService, times(1))
168 .deleteDomainObject(any(), any());
172 public void getCategoryParametersTest() {
173 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
174 CategoryParameterOption categoryParameterOption =
175 new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter);
176 categoryParameter.getOptions().add(categoryParameterOption);
177 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
179 doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
181 CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
183 Assert.assertFalse(response.getCategoryParameters().isEmpty());
184 Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
186 verify(dataAccessService, times(1))
187 .getList(any(), anyString(), any(), any());
191 public void updateCategoryParameterOption_domainObjectGetsSavedSuccessfully() {
192 CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
193 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
194 categoryParameter.getOptions().add(
195 new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, categoryParameter));
196 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
198 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
200 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
202 verify(dataAccessService, times(1))
203 .saveDomainObject(any(), any());
206 @Test(expectedExceptions = { ForbiddenException.class })
207 public void updateCategoryParameterOption_shouldFailUpdateForbidden() {
208 CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("1", CATEGORY_NAME);
209 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, false);
210 categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter));
211 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
213 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
215 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
220 @Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
221 public void updateCategoryParameterOption_CategoryNotFound() {
222 CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
224 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
225 categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter));
226 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
228 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
230 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
235 @Test(expectedExceptions = { AlreadyExistOptionNameException.class })
236 public void updateCategoryParameterOption_OptionNameExists() {
237 CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
239 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
240 CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
241 categoryParameter.getOptions().add(
242 new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter));
243 categoryParameter.getOptions().add(
244 new CategoryParameterOption(APP_ID_SDC, OPTION_NAME, anotherCategoryParameter));
245 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
247 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
249 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
254 private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
255 CategoryParameter categoryParameter = new CategoryParameter();
256 categoryParameter.setName(categoryName);
257 categoryParameter.setIdSupported(idSupported);
258 return categoryParameter;