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.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter));
101 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
103 String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
104 , OPTION_NAME, CATEGORY_NAME);
106 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
108 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
110 Assert.assertFalse(result.getErrors().isEmpty());
111 Assert.assertEquals(result.getErrors().size(), 1);
112 Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
115 private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
116 List<CategoryParameter> aList = new ArrayList<>();
117 aList.add(categoryParameter);
122 public void createCategoryParameterOptions_nonExistingOptionsForCategory() {
123 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
125 List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
126 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
128 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
130 Assert.assertTrue(result.getErrors().isEmpty());
133 @Test(expectedExceptions = { UnfoundedCategoryException.class })
134 public void createCategoryParameterOptions_wrongNumberOfCategoryParameters() {
135 AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
136 List<CategoryParameter> aList = Collections.emptyList();
138 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
140 AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
145 @Test(expectedExceptions = { UnfoundedCategoryException.class })
146 public void deleteCategoryOption_wrongNumberOfParameters() {
147 CategoryParameterOption option = null;
148 List<CategoryParameter> aList = Collections.emptyList();
150 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
152 testSubject.deleteCategoryOption(CATEGORY_NAME, option);
158 public void deleteCategoryOption_happyPath() {
159 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
160 CategoryParameterOption categoryParameterOption =
161 new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter);
162 categoryParameter.getOptions().add(categoryParameterOption);
163 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
165 doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
167 testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
169 verify(dataAccessService, times(1))
170 .deleteDomainObject(anyObject(), anyObject());
174 public void getCategoryParametersTest() {
175 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
176 CategoryParameterOption categoryParameterOption =
177 new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter);
178 categoryParameter.getOptions().add(categoryParameterOption);
179 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
181 doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
183 CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
185 Assert.assertFalse(response.getCategoryParameters().isEmpty());
186 Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
188 verify(dataAccessService, times(1))
189 .getList(anyObject(), anyString(), anyString(), anyObject());
193 public void updateCategoryParameterOption_domainObjectGetsSavedSuccessfully() {
194 CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
195 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
196 categoryParameter.getOptions().add(
197 new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, categoryParameter));
198 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
200 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
202 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
204 verify(dataAccessService, times(1))
205 .saveDomainObject(anyObject(), anyObject());
208 @Test(expectedExceptions = { ForbiddenException.class })
209 public void updateCategoryParameterOption_shouldFailUpdateForbidden() {
210 CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("1", CATEGORY_NAME);
211 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, false);
212 categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter));
213 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
215 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
217 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
222 @Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
223 public void updateCategoryParameterOption_CategoryNotFound() {
224 CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
226 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
227 categoryParameter.getOptions().add(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, categoryParameter));
228 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
230 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
232 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
237 @Test(expectedExceptions = { AlreadyExistOptionNameException.class })
238 public void updateCategoryParameterOption_OptionNameExists() {
239 CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
241 CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
242 categoryParameter.getOptions().add(
243 new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, categoryParameter));
244 categoryParameter.getOptions().add(
245 new CategoryParameterOption(APP_ID_SDC, OPTION_NAME, categoryParameter));
246 List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
248 doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
250 AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
255 private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
256 CategoryParameter categoryParameter = new CategoryParameter();
257 categoryParameter.setName(categoryName);
258 categoryParameter.setIdSupported(idSupported);
259 return categoryParameter;