Improve testing stability
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / test / java / org / openecomp / sdcrests / vsp / rest / services / DeploymentFlavorsImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.vsp.rest.services;
22
23 import org.apache.http.HttpStatus;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentMatchers;
28 import org.mockito.Mock;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.DeploymentFlavorManager;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorCreationDto;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorRequestDto;
40 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
41
42 import javax.ws.rs.core.Response;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.UUID;
46
47 import static org.mockito.MockitoAnnotations.openMocks;
48 import static org.mockito.Mockito.when;
49
50 public class DeploymentFlavorsImplTest {
51
52   private Logger logger = LoggerFactory.getLogger(DeploymentFlavorsImplTest.class);
53
54   @Mock
55   private DeploymentFlavorManager deploymentFlavorManager;
56
57   private final String vspId = UUID.randomUUID().toString();
58   private final String versionId = UUID.randomUUID().toString();
59   private final String deploymentFlavorId = "" + System.currentTimeMillis();
60   private final String user = "cs0008";
61
62   private DeploymentFlavorsImpl dfi;
63
64   @Before
65   public void setUp() {
66     try {
67       openMocks(this);
68
69       DeploymentFlavorEntity e = new DeploymentFlavorEntity();
70       e.setId(deploymentFlavorId);
71       e.setVspId(vspId);
72       e.setVersion(new Version(versionId));
73
74       Collection<DeploymentFlavorEntity> lst = Collections.singletonList(e);
75       when(deploymentFlavorManager.listDeploymentFlavors(
76               ArgumentMatchers.eq(vspId),
77               ArgumentMatchers.any())).thenReturn(lst);
78
79       when(deploymentFlavorManager.createDeploymentFlavor(
80               ArgumentMatchers.any())).thenReturn(e);
81
82       CompositionEntityResponse<DeploymentFlavor> r = new CompositionEntityResponse<>();
83       r.setId(vspId);
84       when(deploymentFlavorManager.getDeploymentFlavor(
85               ArgumentMatchers.eq(vspId),
86               ArgumentMatchers.any(),
87               ArgumentMatchers.eq(deploymentFlavorId))).thenReturn(r);
88
89       CompositionEntityType tpe = CompositionEntityType.component;
90       CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
91       when(deploymentFlavorManager.updateDeploymentFlavor(
92               ArgumentMatchers.any())).thenReturn(data);
93
94
95
96       when(deploymentFlavorManager.getDeploymentFlavorSchema(
97               ArgumentMatchers.eq(vspId),
98               ArgumentMatchers.any())).thenReturn(r);
99
100       dfi = new DeploymentFlavorsImpl(deploymentFlavorManager);
101
102     } catch (Exception e) {
103       logger.error(e.getMessage(), e);
104     }
105   }
106
107   @Test
108   public void testList() {
109     Response rsp = dfi.list(vspId, versionId, user);
110     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
111     Object e = rsp.getEntity();
112     Assert.assertNotNull(e);
113     @SuppressWarnings("unchecked")
114     GenericCollectionWrapper<DeploymentFlavorCreationDto> results = (GenericCollectionWrapper<DeploymentFlavorCreationDto>) e;
115     Assert.assertEquals("result length", 1, results.getListCount());
116   }
117
118   @Test
119   public void testCreate() {
120
121     DeploymentFlavorRequestDto dto = new DeploymentFlavorRequestDto();
122     dto.setDescription("hello");
123     dto.setModel("model");
124     dto.setFeatureGroupId("fgi");
125
126     Response rsp = dfi.create(dto, vspId, versionId, user);
127     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
128     Object e = rsp.getEntity();
129     Assert.assertNotNull(e);
130     try {
131       DeploymentFlavorCreationDto responseDto = (DeploymentFlavorCreationDto)e;
132       Assert.assertEquals(deploymentFlavorId, responseDto.getDeploymentFlavorId());
133     } catch (ClassCastException ex) {
134       Assert.fail("unexpected class for DTO " + e.getClass().getName());
135     }
136   }
137
138
139   @Test
140   public void testDelete() {
141     Response rsp = dfi.delete(vspId, versionId, deploymentFlavorId, user);
142     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
143     Assert.assertNull(rsp.getEntity());
144   }
145
146
147   @Test
148   public void testGet() {
149     Response rsp = dfi.get(vspId, versionId, deploymentFlavorId, user);
150     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
151     Assert.assertNotNull(rsp.getEntity());
152   }
153
154   @Test
155   public void testGetSchema() {
156     Response rsp = dfi.get(vspId, versionId, deploymentFlavorId, user);
157     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
158     Assert.assertNotNull(rsp.getEntity());
159   }
160
161   @Test
162   public void testUpdate() {
163     DeploymentFlavorRequestDto dto = new DeploymentFlavorRequestDto();
164     Response rsp = dfi.update(dto, vspId, versionId, deploymentFlavorId, user);
165     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
166     Assert.assertNull(rsp.getEntity());
167   }
168
169 }