2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.activityspec.be.impl;
19 import org.mockito.InjectMocks;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.Spy;
23 import org.onap.sdc.activityspec.api.rest.types.ActivitySpecAction;
24 import org.onap.sdc.activityspec.be.dao.ActivitySpecDao;
25 import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity;
26 import org.onap.sdc.activityspec.be.datatypes.ActivitySpecParameter;
27 import org.onap.sdc.activityspec.errors.ActivitySpecNotFoundException;
28 import org.openecomp.core.dao.UniqueValueDao;
29 import org.openecomp.sdc.common.errors.CoreException;
30 import org.openecomp.sdc.common.errors.SdcRuntimeException;
31 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
32 import org.openecomp.sdc.versioning.ItemManager;
33 import org.openecomp.sdc.versioning.VersioningManager;
34 import org.openecomp.sdc.versioning.dao.types.Version;
35 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
36 import org.openecomp.sdc.versioning.types.Item;
37 import org.testng.Assert;
38 import org.testng.annotations.AfterMethod;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.List;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.*;
49 import static org.onap.sdc.activityspec.utils.ActivitySpecConstant.*;
51 public class ActivitySpecManagerImplTest {
53 private static final String STRING_TYPE = "String";
54 private static final String TEST_ERROR_MSG = "Test Error";
55 private ActivitySpecEntity input;
56 private static final Version VERSION01 = new Version("12345");
57 private static final String ID = "ID1";
61 private ActivitySpecManagerImpl activitySpecManager;
64 private ItemManager itemManagerMock;
67 private VersioningManager versionManagerMock;
70 private ActivitySpecDao activitySpecDaoMock;
72 //This is used to mock UniqueValueUtil. This should not be removed.
74 private UniqueValueDao uniqueValueDaoMock;
77 public void setUp() throws Exception {
78 MockitoAnnotations.initMocks(this);
82 public void tearDown() {
83 activitySpecManager = null;
87 public void testCreate() {
89 SessionContextProviderFactory.getInstance().createInterface().create("testUser", "testTenant");
91 ActivitySpecEntity activitySpecToCreate = new ActivitySpecEntity();
92 activitySpecToCreate.setName("startserver");
93 activitySpecToCreate.setDescription("start the server");
94 activitySpecToCreate.setVersion(VERSION01);
96 List<String> categoryList = new ArrayList<>();
97 categoryList.add("category1");
98 categoryList.add("category2");
99 activitySpecToCreate.setCategoryList(categoryList);
101 ActivitySpecParameter inputParams = new ActivitySpecParameter("dbhost", STRING_TYPE);
102 inputParams.setValue("localhost");
103 ActivitySpecParameter inputParams1 = new ActivitySpecParameter("dbname", STRING_TYPE);
104 inputParams.setValue("prod");
105 List<ActivitySpecParameter> inputs = new ArrayList<>();
106 inputs.add(inputParams);
107 inputs.add(inputParams1);
108 activitySpecToCreate.setInputs(inputs);
110 ActivitySpecParameter outputParams = new ActivitySpecParameter("status", STRING_TYPE);
111 outputParams.setValue("started");
112 List<ActivitySpecParameter> outputs = new ArrayList<>();
113 outputs.add(outputParams);
114 activitySpecToCreate.setOutputs(outputs);
116 activitySpecToCreate.setId("ID1");
117 activitySpecToCreate.setVersion(VERSION01);
119 Item item = new Item();
120 doReturn(item).when(itemManagerMock).create(anyObject());
122 ActivitySpecEntity activitySpec = activitySpecManager.createActivitySpec(activitySpecToCreate);
124 Assert.assertNotNull(activitySpec);
125 activitySpec.setId("ID1");
126 activitySpec.setStatus(VersionStatus.Draft.name());
127 assertActivitySpecEquals(activitySpec, activitySpecToCreate);
132 public void testList() {
133 ActivitySpecEntity activitySpec = new ActivitySpecEntity();
134 activitySpec.setName("stopServer");
135 doReturn(Arrays.asList(activitySpec)).when(itemManagerMock).list(anyObject());
136 final Collection<Item> activitySpecs = activitySpecManager.list("Certified");
137 Assert.assertEquals(activitySpecs.size(), 1);
141 public void testListInvalidFilter() {
142 final Collection<Item> activitySpecs = activitySpecManager.list("invalid_status");
143 Assert.assertEquals(activitySpecs.size(), 0);
147 public void testListNoFilter() {
148 final Collection<Item> activitySpecs = activitySpecManager.list(null);
149 Assert.assertEquals(activitySpecs.size(), 0);
153 public void testGet() {
154 input = new ActivitySpecEntity();
156 input.setVersion(VERSION01);
158 doReturn(Arrays.asList(VERSION01)).when(versionManagerMock).list(anyObject());
159 doReturn(input).when(activitySpecDaoMock).get(anyObject());
160 VERSION01.setStatus(VersionStatus.Draft);
161 doReturn(VERSION01).when(versionManagerMock).get(anyObject(), anyObject());
162 ActivitySpecEntity retrieved = activitySpecManager.get(input);
163 assertActivitySpecEquals(retrieved, input);
164 Assert.assertEquals(retrieved.getStatus(), VersionStatus.Draft.name());
167 input.setVersion(new Version(VERSION_ID_DEFAULT_VALUE));
168 retrieved = activitySpecManager.get(input);
169 assertActivitySpecEquals(retrieved, input);
170 Assert.assertEquals(retrieved.getStatus(), VersionStatus.Draft.name());
174 public void testGetActivitySpecDaoFail() {
175 input = new ActivitySpecEntity();
177 input.setVersion(VERSION01);
178 doReturn(Arrays.asList(VERSION01)).when(versionManagerMock).list(anyObject());
179 doReturn(input).when(activitySpecDaoMock).get(anyObject());
180 doThrow(new SdcRuntimeException(TEST_ERROR_MSG)).when(activitySpecDaoMock).get(anyObject());
182 activitySpecManager.get(input);
184 } catch (ActivitySpecNotFoundException exception) {
185 Assert.assertEquals(exception.getMessage(), ACTIVITY_SPEC_NOT_FOUND);
190 public void testListVersionFail() {
191 input = new ActivitySpecEntity();
193 input.setVersion(VERSION01);
194 input.getVersion().setId(VERSION_ID_DEFAULT_VALUE);
195 doThrow(new SdcRuntimeException(TEST_ERROR_MSG)).when(versionManagerMock).list(anyObject());
197 activitySpecManager.get(input);
199 } catch (ActivitySpecNotFoundException exception) {
200 Assert.assertEquals(exception.getMessage(), ACTIVITY_SPEC_NOT_FOUND);
205 public void testInvalidDeprecate() {
207 activitySpecManager.actOnAction(ID, VERSION01.getId(), ActivitySpecAction.DEPRECATE);
208 } catch (CoreException exception) {
209 Assert.assertEquals(exception.getMessage(), INVALID_STATE);
214 public void testInvalidDelete() {
216 activitySpecManager.actOnAction(ID, VERSION01.getId(), ActivitySpecAction.DELETE);
217 } catch (CoreException exception) {
218 Assert.assertEquals(exception.getMessage(), INVALID_STATE);
223 public void testCertify() {
224 doReturn(Arrays.asList(VERSION01)).when(versionManagerMock).list(anyObject());
225 doReturn(VERSION01).when(versionManagerMock).get(anyObject(), anyObject());
226 activitySpecManager.actOnAction(ID, VERSION01.getId(), ActivitySpecAction.CERTIFY);
228 verify(versionManagerMock).updateVersion(ID, VERSION01);
229 verify(itemManagerMock).updateVersionStatus(ID, VersionStatus.Certified, VersionStatus.Draft);
230 verify(versionManagerMock).publish(anyObject(), anyObject(), anyObject());
234 public void testInvalidCertify() {
236 activitySpecManager.actOnAction(ID, VERSION01.getId(), ActivitySpecAction.CERTIFY);
237 } catch (CoreException exception) {
238 Assert.assertEquals(exception.getMessage(), INVALID_STATE);
243 public void testGetVersionFailOnStatusChangeAction() {
244 doReturn(Arrays.asList(VERSION01)).when(versionManagerMock).list(anyObject());
245 doThrow(new SdcRuntimeException(TEST_ERROR_MSG)).when(versionManagerMock).get(anyObject(), anyObject());
247 activitySpecManager.actOnAction(ID, VERSION01.getId(), ActivitySpecAction.CERTIFY);
249 } catch (ActivitySpecNotFoundException exception) {
250 Assert.assertEquals(exception.getMessage(), ACTIVITY_SPEC_NOT_FOUND);
255 public void testDeprecate() {
256 VERSION01.setStatus(VersionStatus.Certified);
257 Version retrivedVersion = new Version("12");
258 retrivedVersion.setStatus(VersionStatus.Certified);
259 doReturn(Arrays.asList(VERSION01)).when(versionManagerMock).list(anyObject());
260 doReturn(retrivedVersion).when(versionManagerMock).get(anyObject(), anyObject());
261 activitySpecManager.actOnAction(ID, VERSION_ID_DEFAULT_VALUE, ActivitySpecAction.DEPRECATE);
263 verify(versionManagerMock).updateVersion(ID, retrivedVersion);
264 verify(itemManagerMock).updateVersionStatus(ID, VersionStatus.Deprecated, VersionStatus.Certified);
265 verify(versionManagerMock).publish(anyObject(), anyObject(), anyObject());
269 public void testDelete() {
270 ActivitySpecEntity activitySpec = new ActivitySpecEntity();
271 VERSION01.setStatus(VersionStatus.Deprecated);
272 activitySpec.setName("stopServer");
273 activitySpec.setVersion(VERSION01);
275 Version retrivedVersion = new Version("12");
276 retrivedVersion.setStatus(VersionStatus.Deprecated);
278 doReturn(Arrays.asList(VERSION01)).when(versionManagerMock).list(anyObject());
279 doReturn(retrivedVersion).when(versionManagerMock).get(anyObject(), anyObject());
280 doReturn(activitySpec).when(activitySpecDaoMock).get(anyObject());
281 activitySpecManager.actOnAction(ID, VERSION_ID_DEFAULT_VALUE, ActivitySpecAction.DELETE);
283 verify(versionManagerMock).updateVersion(ID, VERSION01);
284 verify(itemManagerMock).updateVersionStatus(ID, VersionStatus.Deleted, VersionStatus.Deprecated);
285 verify(versionManagerMock).publish(anyObject(), anyObject(), anyObject());
288 private void assertActivitySpecEquals(ActivitySpecEntity actual, ActivitySpecEntity expected) {
289 Assert.assertEquals(actual.getId(), expected.getId());
290 Assert.assertEquals(actual.getName(), expected.getName());
291 Assert.assertEquals(actual.getDescription(), expected.getDescription());
292 Assert.assertEquals(actual.getCategoryList(), expected.getCategoryList());
293 Assert.assertEquals(actual.getInputs(), expected.getInputs());
294 Assert.assertEquals(actual.getOutputs(), expected.getOutputs());