3576ae81c7052601ecba13431d3d13071ed130d6
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.activityspec.be.impl;
18
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;
43
44 import java.util.ArrayList;
45 import java.util.List;
46
47 public class ActivitySpecManagerImplTest {
48
49   private static final String STRING_TYPE = "String";
50   private ActivitySpecEntity activitySpec;
51   private ActivitySpecEntity retrieved;
52   private ActivitySpecEntity input;
53   private ActivitySpecEntity activitySpecToCreate;
54
55   @Spy
56   @InjectMocks
57   private ActivitySpecManagerImpl activitySpecManager;
58
59
60   private ActivitySpecDao activitySpecDaoMock = new ActivitySpecDaoMock();
61
62
63   private ItemManager itemManagerMock = new ItemManagerMock();
64
65
66   private VersioningManager versionManagerMock = new VersionManagerMock() {
67   };
68
69   private UniqueValueDao uniqueValueDaoMock = new UniqueValueDaoMock();
70   private ActivitySpecEntity retrivedAfterNameUpdate;
71
72   @BeforeMethod
73   public void setUp() {
74     MockitoAnnotations.initMocks(this);
75     activitySpecManager = new ActivitySpecManagerImpl(itemManagerMock, versionManagerMock,
76         activitySpecDaoMock, uniqueValueDaoMock);
77   }
78
79   @AfterMethod
80   public void tearDown() {
81     activitySpecManager = null;
82   }
83
84
85   public static final Version VERSION01 = new Version("12345");
86
87   @Test
88   public void testCreate() {
89
90     SessionContextProviderFactory.getInstance().createInterface().create("testUser", "testTenant");
91
92     activitySpecToCreate = new ActivitySpecEntity();
93     activitySpecToCreate.setName("startserver");
94     activitySpecToCreate.setDescription("start the server");
95     activitySpecToCreate.setVersion(VERSION01);
96
97     List<String> categoryList = new ArrayList<>();
98     categoryList.add("category1");
99     categoryList.add("category2");
100     activitySpecToCreate.setCategoryList(categoryList);
101
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.setInputs(inputs);
110
111     ActivitySpecParameter outputParams = new ActivitySpecParameter("status", STRING_TYPE);
112     outputParams.setValue("started");
113     List<ActivitySpecParameter> outputs = new ArrayList<>();
114     outputs.add(outputParams);
115     activitySpecToCreate.setOutputs(outputs);
116
117     activitySpec = activitySpecManager.createActivitySpec
118         (activitySpecToCreate);
119
120     Assert.assertNotNull(activitySpec);
121     activitySpecToCreate.setId(activitySpec.getId());
122     activitySpecToCreate.setVersion(VERSION01);
123     assertActivitySpecEquals(activitySpec, activitySpecToCreate);
124   }
125
126   @Test(dependsOnMethods = "testCreate")
127   public void testList () {
128     //List
129     final Collection<Item> activitySpecs = activitySpecManager.list("Certified");
130     Assert.assertEquals(activitySpecs.size(), 1);
131   }
132
133   @Test(dependsOnMethods = "testCreate")
134   public void testGet () {
135     //Get
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());
142
143     input.setVersion(new Version("LATEST"));
144     retrieved = activitySpecManager.get(input);
145     assertActivitySpecEquals(retrieved, activitySpec);
146     Assert.assertEquals(retrieved.getStatus(), VersionStatus.Draft.name());
147   }
148
149   @Test(dependsOnMethods = "testGet")
150   public void testInvalidDeprecate () {
151     try {
152       activitySpecManager.actOnAction(retrieved.getId(),
153           VERSION01.getId(), ActivitySpecAction.DEPRECATE);
154     }
155     catch (CoreException exception) {
156       Assert.assertEquals(exception.code().id(), "STATUS_NOT_"+VersionStatus.Certified.name()
157           .toUpperCase());
158     }
159   }
160
161   @Test(dependsOnMethods = "testGet")
162   public void testInvalidDelete () {
163     try {
164       activitySpecManager.actOnAction(retrieved.getId(),
165           VERSION01.getId(), ActivitySpecAction.DELETE);
166     }
167     catch (CoreException exception) {
168       Assert.assertEquals(exception.code().id(), "STATUS_NOT_"+VersionStatus.Deprecated.name()
169           .toUpperCase());
170     }
171   }
172
173   @Test(dependsOnMethods = "testGet")
174   public void testUpdate () {
175     //Update
176     retrieved.setDescription("Updated_install");
177     activitySpecManager.update(retrieved);
178
179     final ActivitySpecEntity retrivedAfterUpdate = activitySpecManager.get(input);
180     assertActivitySpecEquals(retrivedAfterUpdate, activitySpecToCreate);
181
182     //Update Name
183     ActivitySpecEntity activitySpecToUpdate = new ActivitySpecEntity();
184     activitySpecToUpdate.setId(activitySpec.getId());
185     activitySpecToUpdate.setName("Updated_start_server");
186     activitySpecToUpdate.setVersion(activitySpec.getVersion());
187
188     activitySpecManager.update(activitySpecToUpdate);
189
190     retrivedAfterNameUpdate = activitySpecManager.get(input);
191     assertActivitySpecEquals(retrivedAfterNameUpdate, activitySpecToUpdate);
192     Assert.assertEquals(retrivedAfterNameUpdate.getStatus(), VersionStatus.Draft.name());
193   }
194
195   @Test(dependsOnMethods = "testUpdate")
196   public void testCertify () {
197     activitySpecManager.actOnAction(retrivedAfterNameUpdate.getId(),
198         VERSION01.getId(), ActivitySpecAction.CERTIFY);
199
200     final ActivitySpecEntity retrivedAfterCertify = activitySpecManager.get(retrivedAfterNameUpdate);
201     assertActivitySpecEquals(retrivedAfterCertify, retrivedAfterNameUpdate );
202     Assert.assertEquals(retrivedAfterCertify.getStatus(), VersionStatus.Certified.name());
203   }
204
205   @Test(dependsOnMethods = "testCertify")
206   public void testInvalidCertify () {
207     try {
208       activitySpecManager.actOnAction(retrieved.getId(),
209           VERSION01.getId(), ActivitySpecAction.CERTIFY);
210     }
211     catch (CoreException exception) {
212       Assert.assertEquals(exception.code().id(), "STATUS_NOT_"+VersionStatus.Draft.name()
213           .toUpperCase());
214     }
215   }
216
217   @Test(dependsOnMethods = "testCertify")
218   public void testDeprecate () {
219     activitySpecManager.actOnAction(retrivedAfterNameUpdate.getId(),
220         retrivedAfterNameUpdate.getVersion().getId(), ActivitySpecAction.DEPRECATE);
221
222     final ActivitySpecEntity retrivedAfterDeprecate = activitySpecManager.get(retrivedAfterNameUpdate);
223     assertActivitySpecEquals(retrivedAfterDeprecate, retrivedAfterNameUpdate );
224     Assert.assertEquals(retrivedAfterDeprecate.getStatus(), VersionStatus.Deprecated.name());
225   }
226
227   @Test(dependsOnMethods = "testDeprecate")
228   public void testDelete () {
229     activitySpecManager.actOnAction(retrivedAfterNameUpdate.getId(),
230         retrivedAfterNameUpdate.getVersion().getId(), ActivitySpecAction.DELETE);
231
232     final ActivitySpecEntity retrivedAfterDelete = activitySpecManager.get(retrivedAfterNameUpdate);
233     assertActivitySpecEquals(retrivedAfterDelete, retrivedAfterNameUpdate );
234     Assert.assertEquals(retrivedAfterDelete.getStatus(), VersionStatus.Deleted.name());
235   }
236
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.getInputs(), expected.getInputs());
243     Assert.assertEquals(actual.getOutputs(), expected.getOutputs());
244   }
245 }