2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vsp.rest.services;
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;
45 import javax.ws.rs.core.Response;
46 import java.io.ByteArrayInputStream;
47 import java.util.UUID;
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;
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({MonitoringUploadsManagerImpl.class, MonitoringUploadsManagerFactory.class, ComponentManagerFactory.class})
55 public class ComponentMonitoringUploadsImplTest {
57 private Logger logger = LoggerFactory.getLogger(ComponentMonitoringUploadsImplTest.class);
60 private ComponentManagerFactory componentManagerFactory;
63 private ComponentManager componentManager;
66 private MonitoringUploadsManagerFactory uploadsFactory;
69 private MonitoringUploadsManager uploadsManager;
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";
81 mockStatic(ComponentManagerFactory.class);
82 when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
83 when(componentManagerFactory.createInterface()).thenReturn(componentManager);
85 mockStatic(MonitoringUploadsManagerFactory.class);
86 when(MonitoringUploadsManagerFactory.getInstance()).thenReturn(uploadsFactory);
87 when(uploadsFactory.createInterface()).thenReturn(uploadsManager);
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);
98 } catch (Exception e) {
99 logger.error(e.getMessage(), e);
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();
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());
114 catch (Exception ex) {
115 logger.error("test failed due to exception", ex);
116 Assert.fail("exception caught " + ex.getMessage());
122 public void testDelete() {
123 ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
124 String type = MonitoringUploadType.SNMP_POLL.toString();
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());
130 catch (Exception ex) {
131 logger.error("test failed due to exception", ex);
132 Assert.fail("exception caught " + ex.getMessage());
137 public void testList() {
138 ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
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());
148 catch (Exception ex) {
149 logger.error("test failed due to exception", ex);
150 Assert.fail("exception caught " + ex.getMessage());