Merge "Refactor of Intersection class"
[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 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;
40
41 import javax.ws.rs.ForbiddenException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
45
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;
50
51 public class CategoryParameterServiceImplTest {
52
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);
59
60
61     @Mock
62     private DataAccessService dataAccessService;
63
64     @InjectMocks
65     private CategoryParameterServiceImpl testSubject;
66
67     @BeforeSuite
68     public void before() {
69         initMocks(this);
70     }
71
72     @BeforeMethod
73     public void resetMocks() {
74         reset(dataAccessService);
75     }
76
77     @Test
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);
83
84         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
85
86         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
87
88         Assert.assertTrue(result.getErrors().isEmpty());;
89         verify(dataAccessService, times(1))
90             .saveDomainObject(any(), any());
91     }
92
93     @Test
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);
100
101         String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
102             , OPTION_NAME, CATEGORY_NAME);
103
104         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
105
106         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
107
108         Assert.assertFalse(result.getErrors().isEmpty());
109         Assert.assertEquals(result.getErrors().size(), 1);
110         Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
111     }
112
113     private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
114         List<CategoryParameter> aList = new ArrayList<>();
115         aList.add(categoryParameter);
116         return aList;
117     }
118
119     @Test
120     public void createCategoryParameterOptions_nonExistingOptionsForCategory()  {
121         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
122
123         List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
124         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
125
126         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
127
128         Assert.assertTrue(result.getErrors().isEmpty());
129     }
130
131     @Test(expectedExceptions = { UnfoundedCategoryException.class })
132     public void createCategoryParameterOptions_wrongNumberOfCategoryParameters()  {
133         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
134         List<CategoryParameter> aList = Collections.emptyList();
135
136         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
137
138         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
139
140         Assert.fail();
141     }
142
143     @Test(expectedExceptions = { UnfoundedCategoryException.class })
144     public void deleteCategoryOption_wrongNumberOfParameters() {
145         CategoryParameterOption option = null;
146         List<CategoryParameter> aList = Collections.emptyList();
147
148         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
149
150         testSubject.deleteCategoryOption(CATEGORY_NAME, option);
151
152         Assert.fail();
153     }
154
155     @Test
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);
162
163         doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
164
165         testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
166
167         verify(dataAccessService, times(1))
168             .deleteDomainObject(any(), any());
169     }
170
171     @Test
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);
178
179         doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
180
181         CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
182
183         Assert.assertFalse(response.getCategoryParameters().isEmpty());
184         Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
185
186         verify(dataAccessService, times(1))
187             .getList(any(), anyString(), any(), any());
188     }
189
190     @Test
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);
197
198         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
199
200         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
201
202         verify(dataAccessService, times(1))
203             .saveDomainObject(any(), any());
204     }
205
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);
212
213         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
214
215         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
216
217         Assert.fail();
218     }
219
220     @Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
221     public void updateCategoryParameterOption_CategoryNotFound() {
222         CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
223
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);
227
228         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
229
230         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
231
232         Assert.fail();
233     }
234
235     @Test(expectedExceptions = { AlreadyExistOptionNameException.class })
236     public void updateCategoryParameterOption_OptionNameExists() {
237         CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
238
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);
246
247         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
248
249         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
250
251         Assert.fail();
252     }
253
254     private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
255         CategoryParameter categoryParameter = new CategoryParameter();
256         categoryParameter.setName(categoryName);
257         categoryParameter.setIdSupported(idSupported);
258         return categoryParameter;
259     }
260 }