296ee8d2caf5a4c676152b3013cedba4e0f221fa
[sdc.git] /
1 package org.openecomp.sdcrests.vsp.rest.services;
2
3 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
4 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
5 import org.apache.http.HttpStatus;
6 import org.junit.Assert;
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.junit.runner.RunWith;
10 import org.mockito.ArgumentMatchers;
11 import org.mockito.Mock;
12 import org.openecomp.core.enrichment.types.MonitoringUploadType;
13 import org.openecomp.sdc.logging.api.Logger;
14 import org.openecomp.sdc.logging.api.LoggerFactory;
15 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
16 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
17 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
18 import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManagerFactory;
19 import org.openecomp.sdc.vendorsoftwareproduct.impl.MonitoringUploadsManagerImpl;
20 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
21 import org.openecomp.sdcrests.vendorsoftwareproducts.types.MonitoringUploadStatusDto;
22 import org.powermock.core.classloader.annotations.PrepareForTest;
23 import org.powermock.modules.junit4.PowerMockRunner;
24
25 import javax.ws.rs.core.Response;
26 import java.io.ByteArrayInputStream;
27 import java.util.UUID;
28
29 import static org.mockito.MockitoAnnotations.initMocks;
30 import static org.powermock.api.mockito.PowerMockito.mockStatic;
31 import static org.powermock.api.mockito.PowerMockito.when;
32
33 @RunWith(PowerMockRunner.class)
34 @PrepareForTest({MonitoringUploadsManagerImpl.class, MonitoringUploadsManagerFactory.class, ComponentManagerFactory.class})
35 public class ComponentMonitoringUploadsImplTest {
36
37   private Logger logger = LoggerFactory.getLogger(ComponentMonitoringUploadsImplTest.class);
38
39   @Mock
40   private ComponentManagerFactory componentManagerFactory;
41
42   @Mock
43   private ComponentManager componentManager;
44
45   @Mock
46   private MonitoringUploadsManagerFactory uploadsFactory;
47
48   @Mock
49   private MonitoringUploadsManager uploadsManager;
50
51   private final String vspId = UUID.randomUUID().toString();
52   private final String versionId = UUID.randomUUID().toString();
53   private final String componentId = "" + System.currentTimeMillis();
54   private final String user = "cs0008";
55
56   @Before
57   public void setUp() {
58     try {
59       initMocks(this);
60
61       mockStatic(ComponentManagerFactory.class);
62       when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
63       when(componentManagerFactory.createInterface()).thenReturn(componentManager);
64
65       mockStatic(MonitoringUploadsManagerFactory.class);
66       when(MonitoringUploadsManagerFactory.getInstance()).thenReturn(uploadsFactory);
67       when(uploadsFactory.createInterface()).thenReturn(uploadsManager);
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     } catch (Exception e) {
79       logger.error(e.getMessage(), e);
80     }
81   }
82
83   @Test
84   public void testUpload() {
85     ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
86     byte[] bytes = "Hello".getBytes();
87     Attachment a = new Attachment("foo", new ByteArrayInputStream(bytes), new ContentDisposition("filename"));
88     String type = MonitoringUploadType.SNMP_POLL.toString();
89     try {
90       Response rsp = bean.upload(a, vspId, versionId, componentId, type, user);
91       Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
92       Assert.assertNull(rsp.getEntity());
93     }
94     catch (Exception ex) {
95       logger.error("test failed due to exception", ex);
96       Assert.fail("exception caught " + ex.getMessage());
97     }
98   }
99
100
101   @Test
102   public void testDelete() {
103     ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
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     ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
119     try {
120       Response rsp = bean.list(vspId, versionId, componentId, user);
121       Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
122       Assert.assertNotNull(rsp.getEntity());
123       MonitoringUploadStatusDto dto = (MonitoringUploadStatusDto)rsp.getEntity();
124       Assert.assertEquals("p",dto.getSnmpPoll());
125       Assert.assertEquals("v",dto.getVesEvent());
126       Assert.assertEquals("t",dto.getSnmpTrap());
127     }
128     catch (Exception ex) {
129       logger.error("test failed due to exception", ex);
130       Assert.fail("exception caught " + ex.getMessage());
131     }
132   }
133
134 }