Added oparent to sdc main
[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.junit.runner.RunWith;
28 import org.mockito.ArgumentMatchers;
29 import org.mockito.Mock;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.DeploymentFlavorManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.DeploymentFlavorManagerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorCreationDto;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorRequestDto;
42 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
43 import org.powermock.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
45
46 import javax.ws.rs.core.Response;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.UUID;
50
51 import static org.mockito.MockitoAnnotations.initMocks;
52 import static org.powermock.api.mockito.PowerMockito.mockStatic;
53 import static org.powermock.api.mockito.PowerMockito.when;
54
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({DeploymentFlavorsImpl.class, DeploymentFlavorManagerFactory.class})
57 public class DeploymentFlavorsImplTest {
58
59   private Logger logger = LoggerFactory.getLogger(DeploymentFlavorsImplTest.class);
60
61
62   @Mock
63   private DeploymentFlavorManagerFactory deploymentFlavorManagerFactory;
64
65   @Mock
66   private DeploymentFlavorManager deploymentFlavorManager;
67
68   private final String vspId = UUID.randomUUID().toString();
69   private final String versionId = UUID.randomUUID().toString();
70   private final String deploymentFlavorId = "" + System.currentTimeMillis();
71   private final String user = "cs0008";
72
73   @Before
74   public void setUp() {
75     try {
76       initMocks(this);
77
78       mockStatic(DeploymentFlavorManagerFactory.class);
79       when(DeploymentFlavorManagerFactory.getInstance()).thenReturn(deploymentFlavorManagerFactory);
80       when(deploymentFlavorManagerFactory.createInterface()).thenReturn(deploymentFlavorManager);
81
82       DeploymentFlavorEntity e = new DeploymentFlavorEntity();
83       e.setId(deploymentFlavorId);
84       e.setVspId(vspId);
85       e.setVersion(new Version(versionId));
86
87       Collection<DeploymentFlavorEntity> lst = Collections.singletonList(e);
88       when(deploymentFlavorManager.listDeploymentFlavors(
89               ArgumentMatchers.eq(vspId),
90               ArgumentMatchers.any())).thenReturn(lst);
91
92       when(deploymentFlavorManager.createDeploymentFlavor(
93               ArgumentMatchers.any())).thenReturn(e);
94
95       CompositionEntityResponse<DeploymentFlavor> r = new CompositionEntityResponse<>();
96       r.setId(vspId);
97       when(deploymentFlavorManager.getDeploymentFlavor(
98               ArgumentMatchers.eq(vspId),
99               ArgumentMatchers.any(),
100               ArgumentMatchers.eq(deploymentFlavorId))).thenReturn(r);
101
102       CompositionEntityType tpe = CompositionEntityType.component;
103       CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
104       when(deploymentFlavorManager.updateDeploymentFlavor(
105               ArgumentMatchers.any())).thenReturn(data);
106
107
108
109       when(deploymentFlavorManager.getDeploymentFlavorSchema(
110               ArgumentMatchers.eq(vspId),
111               ArgumentMatchers.any())).thenReturn(r);
112
113
114     } catch (Exception e) {
115       logger.error(e.getMessage(), e);
116     }
117   }
118
119   @Test
120   public void testList() {
121     DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
122
123     Response rsp = dfi.list(vspId, versionId, user);
124     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
125     Object e = rsp.getEntity();
126     Assert.assertNotNull(e);
127     @SuppressWarnings("unchecked")
128     GenericCollectionWrapper<DeploymentFlavorCreationDto> results = (GenericCollectionWrapper<DeploymentFlavorCreationDto>) e;
129     Assert.assertEquals("result length", 1, results.getListCount());
130   }
131
132   @Test
133   public void testCreate() {
134
135     DeploymentFlavorRequestDto dto = new DeploymentFlavorRequestDto();
136     dto.setDescription("hello");
137     dto.setModel("model");
138     dto.setFeatureGroupId("fgi");
139
140     DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
141     Response rsp = dfi.create(dto, vspId, versionId, user);
142     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
143     Object e = rsp.getEntity();
144     Assert.assertNotNull(e);
145     try {
146       DeploymentFlavorCreationDto responseDto = (DeploymentFlavorCreationDto)e;
147       Assert.assertEquals(deploymentFlavorId, responseDto.getDeploymentFlavorId());
148     } catch (ClassCastException ex) {
149       Assert.fail("unexpected class for DTO " + e.getClass().getName());
150     }
151   }
152
153
154   @Test
155   public void testDelete() {
156     DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
157     Response rsp = dfi.delete(vspId, versionId, deploymentFlavorId, user);
158     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
159     Assert.assertNull(rsp.getEntity());
160   }
161
162
163   @Test
164   public void testGet() {
165     DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
166     Response rsp = dfi.get(vspId, versionId, deploymentFlavorId, user);
167     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
168     Assert.assertNotNull(rsp.getEntity());
169   }
170
171   @Test
172   public void testGetSchema() {
173     DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
174     Response rsp = dfi.get(vspId, versionId, deploymentFlavorId, user);
175     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
176     Assert.assertNotNull(rsp.getEntity());
177   }
178
179   @Test
180   public void testUpdate() {
181     DeploymentFlavorsImpl dfi = new DeploymentFlavorsImpl();
182     DeploymentFlavorRequestDto dto = new DeploymentFlavorRequestDto();
183     Response rsp = dfi.update(dto, vspId, versionId, deploymentFlavorId, user);
184     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
185     Assert.assertNull(rsp.getEntity());
186   }
187
188 }