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