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