1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*-
* ============LICENSE_START=======================================================
* VID
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2018 Nokia. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.vid.services;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.ws.rs.ForbiddenException;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.onap.portalsdk.core.service.DataAccessService;
import org.onap.vid.category.AddCategoryOptionResponse;
import org.onap.vid.category.AddCategoryOptionsRequest;
import org.onap.vid.category.CategoryParameterOptionRep;
import org.onap.vid.category.CategoryParametersResponse;
import org.onap.vid.model.CategoryParameter;
import org.onap.vid.model.CategoryParameter.Family;
import org.onap.vid.model.CategoryParameterOption;
import org.onap.vid.services.CategoryParameterServiceImpl.AlreadyExistOptionNameException;
import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryException;
import org.onap.vid.services.CategoryParameterServiceImpl.UnfoundedCategoryOptionException;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class CategoryParameterServiceImplTest {
private static final String CATEGORY_NAME = "SAMPLE_CATEGORY_NAME";
private static final String OPTION_NAME = "SAMPLE_OPTION_NAME";
private static final String UNIQUE_OPTION_NAME = "UNIQUE_OPTION_NAME";
private static final String APP_ID_VID = "VID";
private static final String APP_ID_SDC = "SDC";
private static final String QUERY_STRING_FOR_CATEGORY_NAME = String.format(" where name = '%s' ", CATEGORY_NAME);
@Mock
private DataAccessService dataAccessService;
@InjectMocks
private CategoryParameterServiceImpl testSubject;
@BeforeSuite
public void before() {
initMocks(this);
}
@BeforeMethod
public void resetMocks() {
reset(dataAccessService);
}
@Test
public void createCategoryParameterOptions_happyPath() {
AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
optionsRequest.options.add(UNIQUE_OPTION_NAME);
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
Assert.assertTrue(result.getErrors().isEmpty());;
verify(dataAccessService, times(1))
.saveDomainObject(anyObject(), anyObject());
}
@Test
public void createCategoryParameterOptions_existingOptionsForCategory() {
AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
optionsRequest.options.add(OPTION_NAME);
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
String expectedError = String.format(CategoryParameterServiceImpl.OPTION_ALREADY_EXIST_FOR_CATEGORY
, OPTION_NAME, CATEGORY_NAME);
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
Assert.assertFalse(result.getErrors().isEmpty());
Assert.assertEquals(result.getErrors().size(), 1);
Assert.assertTrue(result.getErrors().stream().allMatch(expectedError::equals));
}
private List<CategoryParameter> createCategoryParametersList(CategoryParameter categoryParameter) {
List<CategoryParameter> aList = new ArrayList<>();
aList.add(categoryParameter);
return aList;
}
@Test
public void createCategoryParameterOptions_nonExistingOptionsForCategory() {
AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
List<CategoryParameter> aList = createCategoryParametersList(new CategoryParameter());
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
Assert.assertTrue(result.getErrors().isEmpty());
}
@Test(expectedExceptions = { UnfoundedCategoryException.class })
public void createCategoryParameterOptions_wrongNumberOfCategoryParameters() {
AddCategoryOptionsRequest optionsRequest = new AddCategoryOptionsRequest();
List<CategoryParameter> aList = Collections.emptyList();
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.createCategoryParameterOptions(CATEGORY_NAME, optionsRequest);
Assert.fail();
}
@Test(expectedExceptions = { UnfoundedCategoryException.class })
public void deleteCategoryOption_wrongNumberOfParameters() {
CategoryParameterOption option = null;
List<CategoryParameter> aList = Collections.emptyList();
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
testSubject.deleteCategoryOption(CATEGORY_NAME, option);
Assert.fail();
}
@Test
public void deleteCategoryOption_happyPath() {
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameterOption categoryParameterOption =
new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter);
categoryParameter.addOption(categoryParameterOption);
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
testSubject.deleteCategoryOption(CATEGORY_NAME, categoryParameterOption);
verify(dataAccessService, times(1))
.deleteDomainObject(anyObject(), anyObject());
}
@Test
public void getCategoryParametersTest() {
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameterOption categoryParameterOption =
new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter);
categoryParameter.addOption(categoryParameterOption);
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
doReturn(aList).when(dataAccessService).getList(anyObject(), anyString(), anyString(), anyObject());
CategoryParametersResponse response = testSubject.getCategoryParameters(Family.PARAMETER_STANDARDIZATION);
Assert.assertFalse(response.getCategoryParameters().isEmpty());
Assert.assertTrue(response.getCategoryParameters().containsKey(CATEGORY_NAME));
verify(dataAccessService, times(1))
.getList(anyObject(), anyString(), anyString(), anyObject());
}
@Test
public void updateCategoryParameterOption_domainObjectGetsSavedSuccessfully() {
CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
categoryParameter.addOption(
new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter));
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
verify(dataAccessService, times(1))
.saveDomainObject(anyObject(), anyObject());
}
@Test(expectedExceptions = { ForbiddenException.class })
public void updateCategoryParameterOption_shouldFailUpdateForbidden() {
CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("1", CATEGORY_NAME);
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, false);
CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, false);
categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
Assert.fail();
}
@Test(expectedExceptions = { UnfoundedCategoryOptionException.class })
public void updateCategoryParameterOption_CategoryNotFound() {
CategoryParameterOptionRep optionRep = new CategoryParameterOptionRep("SOME_UNRELATED_ID", CATEGORY_NAME);
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
categoryParameter.addOption(new CategoryParameterOption(APP_ID_VID, OPTION_NAME, anotherCategoryParameter));
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRep);
Assert.fail();
}
@Test(expectedExceptions = { AlreadyExistOptionNameException.class })
public void updateCategoryParameterOption_OptionNameExists() {
CategoryParameterOptionRep optionRepExisting = new CategoryParameterOptionRep(APP_ID_VID, OPTION_NAME);
CategoryParameter categoryParameter = createCategoryParameter(CATEGORY_NAME, true);
CategoryParameter anotherCategoryParameter = createCategoryParameter(CATEGORY_NAME, true);
categoryParameter.addOption(
new CategoryParameterOption(APP_ID_VID, UNIQUE_OPTION_NAME, anotherCategoryParameter));
categoryParameter.addOption(
new CategoryParameterOption(APP_ID_SDC, OPTION_NAME, anotherCategoryParameter));
List<CategoryParameter> aList = createCategoryParametersList(categoryParameter);
doReturn(aList).when(dataAccessService).getList(CategoryParameter.class, QUERY_STRING_FOR_CATEGORY_NAME, null, null);
AddCategoryOptionResponse result = testSubject.updateCategoryParameterOption(CATEGORY_NAME, optionRepExisting);
Assert.fail();
}
private CategoryParameter createCategoryParameter(String categoryName, boolean idSupported) {
CategoryParameter categoryParameter = new CategoryParameter();
categoryParameter.setName(categoryName);
categoryParameter.setIdSupported(idSupported);
return categoryParameter;
}
}
|