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.openecomp.activityspec.be.impl;
19 import java.util.Collection;
20 import org.mockito.InjectMocks;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.Spy;
23 import org.openecomp.activityspec.api.rest.types.ActivitySpecAction;
24 import org.openecomp.activityspec.be.dao.ActivitySpecDao;
25 import org.openecomp.activityspec.be.dao.types.ActivitySpecEntity;
26 import org.openecomp.activityspec.be.datatypes.ActivitySpecParameter;
27 import org.openecomp.core.dao.UniqueValueDao;
28 import org.openecomp.activityspec.mocks.ActivitySpecDaoMock;
29 import org.openecomp.activityspec.mocks.ItemManagerMock;
30 import org.openecomp.activityspec.mocks.UniqueValueDaoMock;
31 import org.openecomp.activityspec.mocks.VersionManagerMock;
32 import org.openecomp.sdc.common.errors.CoreException;
33 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
34 import org.openecomp.sdc.versioning.ItemManager;
35 import org.openecomp.sdc.versioning.VersioningManager;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
38 import org.openecomp.sdc.versioning.types.Item;
39 import org.testng.Assert;
40 import org.testng.annotations.AfterMethod;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
44 import java.util.ArrayList;
45 import java.util.List;
47 public class ActivitySpecManagerImplTest {
49 private static final String STRING_TYPE = "String";
50 ActivitySpecEntity activitySpec;
51 private ActivitySpecEntity retrieved;
52 private ActivitySpecEntity input;
53 private ActivitySpecEntity activitySpecToCreate;
57 private ActivitySpecManagerImpl activitySpecManager;
60 private ActivitySpecDao activitySpecDaoMock = new ActivitySpecDaoMock();
63 private ItemManager itemManagerMock = new ItemManagerMock();
66 private VersioningManager versionManagerMock = new VersionManagerMock() {
69 private UniqueValueDao uniqueValueDaoMock = new UniqueValueDaoMock();
70 private ActivitySpecEntity retrivedAfterNameUpdate;
74 MockitoAnnotations.initMocks(this);
75 activitySpecManager = new ActivitySpecManagerImpl(itemManagerMock, versionManagerMock,
76 activitySpecDaoMock, uniqueValueDaoMock);
80 public void tearDown() {
81 activitySpecManager = null;
85 public static final Version VERSION01 = new Version("12345");
88 public void testCreate() {
90 SessionContextProviderFactory.getInstance().createInterface().create("testUser", "testTenant");
92 activitySpecToCreate = new ActivitySpecEntity();
93 activitySpecToCreate.setName("startserver");
94 activitySpecToCreate.setDescription("start the server");
95 activitySpecToCreate.setVersion(VERSION01);
97 List<String> categoryList = new ArrayList<>();
98 categoryList.add("category1");
99 categoryList.add("category2");
100 activitySpecToCreate.setCategoryList(categoryList);
102 ActivitySpecParameter inputParams = new ActivitySpecParameter("dbhost", STRING_TYPE);
103 inputParams.setValue("localhost");
104 ActivitySpecParameter inputParams1 = new ActivitySpecParameter("dbname", STRING_TYPE);
105 inputParams.setValue("prod");
106 List<ActivitySpecParameter> inputs = new ArrayList<>();
107 inputs.add(inputParams);
108 inputs.add(inputParams1);
109 activitySpecToCreate.setInputParameters(inputs);
111 ActivitySpecParameter outputParams = new ActivitySpecParameter("status", STRING_TYPE);
112 outputParams.setValue("started");
113 List<ActivitySpecParameter> outputs = new ArrayList<>();
114 outputs.add(outputParams);
115 activitySpecToCreate.setOutputParameters(outputs);
117 activitySpec = activitySpecManager.createActivitySpec
118 (activitySpecToCreate);
120 Assert.assertNotNull(activitySpec);
121 activitySpecToCreate.setId(activitySpec.getId());
122 activitySpecToCreate.setVersion(VERSION01);
123 assertActivitySpecEquals(activitySpec, activitySpecToCreate);
126 @Test(dependsOnMethods = "testCreate")
127 public void testList () {
129 final Collection<Item> activitySpecs = activitySpecManager.list("Certified");
130 Assert.assertEquals(activitySpecs.size(), 1);
133 @Test(dependsOnMethods = "testCreate")
134 public void testGet () {
136 input = new ActivitySpecEntity();
137 input.setId(activitySpec.getId());
138 input.setVersion(activitySpec.getVersion());
139 retrieved = activitySpecManager.get(input);
140 assertActivitySpecEquals(retrieved, activitySpec);
141 Assert.assertEquals(retrieved.getStatus(), VersionStatus.Draft.name());
143 input.setVersion(new Version("LATEST"));
144 retrieved = activitySpecManager.get(input);
145 assertActivitySpecEquals(retrieved, activitySpec);
146 Assert.assertEquals(retrieved.getStatus(), VersionStatus.Draft.name());
149 @Test(dependsOnMethods = "testGet")
150 public void testInvalidDeprecate () {
152 activitySpecManager.actOnAction(retrieved.getId(),
153 VERSION01.getId(), ActivitySpecAction.DEPRECATE);
155 catch (CoreException exception) {
156 Assert.assertEquals(exception.code().id(), "STATUS_NOT_"+VersionStatus.Certified.name()
161 @Test(dependsOnMethods = "testGet")
162 public void testInvalidDelete () {
164 activitySpecManager.actOnAction(retrieved.getId(),
165 VERSION01.getId(), ActivitySpecAction.DELETE);
167 catch (CoreException exception) {
168 Assert.assertEquals(exception.code().id(), "STATUS_NOT_"+VersionStatus.Deprecated.name()
173 @Test(dependsOnMethods = "testGet")
174 public void testUpdate () {
176 retrieved.setDescription("Updated_install");
177 activitySpecManager.update(retrieved);
179 final ActivitySpecEntity retrivedAfterUpdate = activitySpecManager.get(input);
180 assertActivitySpecEquals(retrivedAfterUpdate, activitySpecToCreate);
183 ActivitySpecEntity activitySpecToUpdate = new ActivitySpecEntity();
184 activitySpecToUpdate.setId(activitySpec.getId());
185 activitySpecToUpdate.setName("Updated_start_server");
186 activitySpecToUpdate.setVersion(activitySpec.getVersion());
188 activitySpecManager.update(activitySpecToUpdate);
190 retrivedAfterNameUpdate = activitySpecManager.get(input);
191 assertActivitySpecEquals(retrivedAfterNameUpdate, activitySpecToUpdate);
192 Assert.assertEquals(retrivedAfterNameUpdate.getStatus(), VersionStatus.Draft.name());
195 @Test(dependsOnMethods = "testUpdate")
196 public void testCertify () {
197 activitySpecManager.actOnAction(retrivedAfterNameUpdate.getId(),
198 VERSION01.getId(), ActivitySpecAction.CERTIFY);
200 final ActivitySpecEntity retrivedAfterCertify = activitySpecManager.get(retrivedAfterNameUpdate);
201 assertActivitySpecEquals(retrivedAfterCertify, retrivedAfterNameUpdate );
202 Assert.assertEquals(retrivedAfterCertify.getStatus(), VersionStatus.Certified.name());
205 @Test(dependsOnMethods = "testCertify")
206 public void testInvalidCertify () {
208 activitySpecManager.actOnAction(retrieved.getId(),
209 VERSION01.getId(), ActivitySpecAction.CERTIFY);
211 catch (CoreException exception) {
212 Assert.assertEquals(exception.code().id(), "STATUS_NOT_"+VersionStatus.Draft.name()
217 @Test(dependsOnMethods = "testCertify")
218 public void testDeprecate () {
219 activitySpecManager.actOnAction(retrivedAfterNameUpdate.getId(),
220 retrivedAfterNameUpdate.getVersion().getId(), ActivitySpecAction.DEPRECATE);
222 final ActivitySpecEntity retrivedAfterDeprecate = activitySpecManager.get(retrivedAfterNameUpdate);
223 assertActivitySpecEquals(retrivedAfterDeprecate, retrivedAfterNameUpdate );
224 Assert.assertEquals(retrivedAfterDeprecate.getStatus(), VersionStatus.Deprecated.name());
227 @Test(dependsOnMethods = "testDeprecate")
228 public void testDelete () {
229 activitySpecManager.actOnAction(retrivedAfterNameUpdate.getId(),
230 retrivedAfterNameUpdate.getVersion().getId(), ActivitySpecAction.DELETE);
232 final ActivitySpecEntity retrivedAfterDelete = activitySpecManager.get(retrivedAfterNameUpdate);
233 assertActivitySpecEquals(retrivedAfterDelete, retrivedAfterNameUpdate );
234 Assert.assertEquals(retrivedAfterDelete.getStatus(), VersionStatus.Deleted.name());
237 private void assertActivitySpecEquals(ActivitySpecEntity actual, ActivitySpecEntity expected) {
238 Assert.assertEquals(actual.getId(), expected.getId());
239 Assert.assertEquals(actual.getName(), expected.getName());
240 Assert.assertEquals(actual.getDescription(), expected.getDescription());
241 Assert.assertEquals(actual.getCategoryList(), expected.getCategoryList());
242 Assert.assertEquals(actual.getInputParameters(), expected.getInputParameters());
243 Assert.assertEquals(actual.getOutputParameters(), expected.getOutputParameters());