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 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;
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;
53 public class CategoryParameterServiceImplTest {
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);
64 private DataAccessService dataAccessService;
67 private CategoryParameterServiceImpl testSubject;
70 public void before() {
75 public void resetMocks() {
76 reset(dataAccessService);
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);
86 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
88 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
90 Assert.assertTrue(result.getErrors().isEmpty());;
91 verify(dataAccessService, times(1))
92 .saveDomainObject(anyObject(), anyObject());
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);
104 String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
105 , OPTION_NAME, CATEGORY_NAME);
107 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
109 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
111 Assert.assertFalse(result.getErrors().isEmpty());
112 Assert.assertEquals(result.getErrors().size(), 1);
113 Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
116 private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
117 List<CategoryParameter> aList = new ArrayList<>();
118 aList.add(categoryParameter);
123 public void createCategoryParameterOptions_nonExistingOptionsForCategory() {
124 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
126 List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
127 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
129 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
131 Assert.assertTrue(result.getErrors().isEmpty());
134 @Test(expectedExceptions = { UnfoundedCategoryException.class })
135 public void createCategoryParameterOptions_wrongNumberOfCategoryParameters() {
136 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
137 List<CategoryParameter> aList = Collections.emptyList();
139 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
141 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
146 @Test(expectedExceptions = { UnfoundedCategoryException.class })
147 public void deleteCategoryOption_wrongNumberOfParameters() {
148 CategoryParameterOption option = null;
149 List<CategoryParameter> aList = Collections.emptyList();
151 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
153 testSubject.deleteCategoryOption(CATEGORY_NAME, option);
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);
167 doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
169 testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
171 verify(dataAccessService, times(1))
172 .deleteDomainObject(anyObject(), anyObject());
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);
184 doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
186 CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
188 Assert.assertFalse(response.getCategoryParameters().isEmpty());
189 Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
191 verify(dataAccessService, times(1))
192 .getList(anyObject(), anyString(), anyString(), anyObject());
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);
204 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
206 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
208 verify(dataAccessService, times(1))
209 .saveDomainObject(anyObject(), anyObject());
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);
220 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
222 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
227 @Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
228 public void updateCategoryParameterOption_CategoryNotFound() {
229 CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
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);
236 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
238 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
243 @Test(expectedExceptions = { AlreadyExistOptionNameException.class })
244 public void updateCategoryParameterOption_OptionNameExists() {
245 CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
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);
255 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
257 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
262 private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
263 CategoryParameter categoryParameter = new CategoryParameter();
264 categoryParameter.setName(categoryName);
265 categoryParameter.setIdSupported(idSupported);
266 return categoryParameter;