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 / ComponentMonitoringUploadsImplTest.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.cxf.jaxrs.ext.multipart.Attachment;
24 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
25 import org.apache.http.HttpStatus;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentMatchers;
30 import org.mockito.Mock;
31 import org.openecomp.core.enrichment.types.MonitoringUploadType;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.MonitoringUploadStatusDto;
38
39
40 import javax.ws.rs.core.Response;
41 import java.io.ByteArrayInputStream;
42 import java.util.UUID;
43
44 import static org.mockito.MockitoAnnotations.openMocks;
45 import static org.mockito.Mockito.when;
46
47 public class ComponentMonitoringUploadsImplTest {
48
49   private Logger logger = LoggerFactory.getLogger(ComponentMonitoringUploadsImplTest.class);
50
51   @Mock
52   private ComponentManager componentManager;
53
54   @Mock
55   private MonitoringUploadsManager uploadsManager;
56
57   private final String vspId = UUID.randomUUID().toString();
58   private final String versionId = UUID.randomUUID().toString();
59   private final String componentId = "" + System.currentTimeMillis();
60   private final String user = "cs0008";
61
62   private ComponentMonitoringUploadsImpl bean;
63
64   @Before
65   public void setUp() {
66     try {
67       openMocks(this);
68
69       MonitoringUploadStatus result = new MonitoringUploadStatus();
70       result.setSnmpPoll("p");
71       result.setSnmpTrap("t");
72       result.setVesEvent("v");
73       when(uploadsManager.listFilenames(
74               ArgumentMatchers.eq(vspId),
75               ArgumentMatchers.any(),
76               ArgumentMatchers.eq(componentId))).thenReturn(result);
77
78       this.bean = new ComponentMonitoringUploadsImpl(uploadsManager, componentManager);
79
80     } catch (Exception e) {
81       logger.error(e.getMessage(), e);
82     }
83   }
84
85   @Test
86   public void testUpload() {
87     byte[] bytes = "Hello".getBytes();
88     Attachment a = new Attachment("foo", new ByteArrayInputStream(bytes), new ContentDisposition("filename"));
89     String type = MonitoringUploadType.SNMP_POLL.toString();
90     try {
91       Response rsp = bean.upload(a, vspId, versionId, componentId, type, user);
92       Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
93       Assert.assertNull(rsp.getEntity());
94     }
95     catch (Exception ex) {
96       logger.error("test failed due to exception", ex);
97       Assert.fail("exception caught " + ex.getMessage());
98     }
99   }
100
101
102   @Test
103   public void testDelete() {
104     String type = MonitoringUploadType.SNMP_POLL.toString();
105     try {
106       Response rsp = bean.delete(vspId, versionId, componentId, type, user);
107       Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
108       Assert.assertNull(rsp.getEntity());
109     }
110     catch (Exception ex) {
111       logger.error("test failed due to exception", ex);
112       Assert.fail("exception caught " + ex.getMessage());
113     }
114   }
115
116   @Test
117   public void testList() {
118     try {
119       Response rsp = bean.list(vspId, versionId, componentId, user);
120       Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
121       Assert.assertNotNull(rsp.getEntity());
122       MonitoringUploadStatusDto dto = (MonitoringUploadStatusDto)rsp.getEntity();
123       Assert.assertEquals("p",dto.getSnmpPoll());
124       Assert.assertEquals("v",dto.getVesEvent());
125       Assert.assertEquals("t",dto.getSnmpTrap());
126     }
127     catch (Exception ex) {
128       logger.error("test failed due to exception", ex);
129       Assert.fail("exception caught " + ex.getMessage());
130     }
131   }
132
133 }