Fix for Penetration test _ Session and cookie management
[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 - 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
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 anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
99         categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
100         List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
101
102         String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
103             , OPTION_NAME, CATEGORY_NAME);
104
105         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
106
107         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
108
109         Assert.assertFalse(result.getErrors().isEmpty());
110         Assert.assertEquals(result.getErrors().size(), 1);
111         Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
112     }
113
114     private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
115         List<CategoryParameter> aList = new ArrayList<>();
116         aList.add(categoryParameter);
117         return aList;
118     }
119
120     @Test
121     public void createCategoryParameterOptions_nonExistingOptionsForCategory()  {
122         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
123
124         List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
125         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
126
127         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
128
129         Assert.assertTrue(result.getErrors().isEmpty());
130     }
131
132     @Test(expectedExceptions = { UnfoundedCategoryException.class })
133     public void createCategoryParameterOptions_wrongNumberOfCategoryParameters()  {
134         AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
135         List<CategoryParameter> aList = Collections.emptyList();
136
137         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
138
139         AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
140
141         Assert.fail();
142     }
143
144     @Test(expectedExceptions = { UnfoundedCategoryException.class })
145     public void deleteCategoryOption_wrongNumberOfParameters() {
146         CategoryParameterOption option = null;
147         List<CategoryParameter> aList = Collections.emptyList();
148
149         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
150
151         testSubject.deleteCategoryOption(CATEGORY_NAME, option);
152
153         Assert.fail();
154     }
155
156     @Test
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);
164
165         doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
166
167         testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
168
169         verify(dataAccessService, times(1))
170             .deleteDomainObject(any(), any());
171     }
172
173     @Test
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);
181
182         doReturn(aList).when(dataAccessService).getList(any(), anyString(), any(), any());
183
184         CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
185
186         Assert.assertFalse(response.getCategoryParameters().isEmpty());
187         Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
188
189         verify(dataAccessService, times(1))
190             .getList(any(), anyString(), any(), any());
191     }
192
193     @Test
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);
201
202         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
203
204         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
205
206         verify(dataAccessService, times(1))
207             .saveDomainObject(any(), any());
208     }
209
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);
217
218         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
219
220         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
221
222         Assert.fail();
223     }
224
225     @Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
226     public void updateCategoryParameterOption_CategoryNotFound() {
227         CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
228
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);
233
234         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
235
236         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
237
238         Assert.fail();
239     }
240
241     @Test(expectedExceptions = { AlreadyExistOptionNameException.class })
242     public void updateCategoryParameterOption_OptionNameExists() {
243         CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
244
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);
252
253         doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
254
255         AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
256
257         Assert.fail();
258     }
259
260     private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
261         CategoryParameter categoryParameter = new CategoryParameter();
262         categoryParameter.setName(categoryName);
263         categoryParameter.setIdSupported(idSupported);
264         return categoryParameter;
265     }
266 }