aboutsummaryrefslogtreecommitdiffstats
path: root/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
blob: 296ee8d2caf5a4c676152b3013cedba4e0f221fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package org.openecomp.sdcrests.vsp.rest.services;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
import org.apache.http.HttpStatus;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.openecomp.core.enrichment.types.MonitoringUploadType;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManager;
import org.openecomp.sdc.vendorsoftwareproduct.MonitoringUploadsManagerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.impl.MonitoringUploadsManagerImpl;
import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
import org.openecomp.sdcrests.vendorsoftwareproducts.types.MonitoringUploadStatusDto;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.util.UUID;

import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({MonitoringUploadsManagerImpl.class, MonitoringUploadsManagerFactory.class, ComponentManagerFactory.class})
public class ComponentMonitoringUploadsImplTest {

  private Logger logger = LoggerFactory.getLogger(ComponentMonitoringUploadsImplTest.class);

  @Mock
  private ComponentManagerFactory componentManagerFactory;

  @Mock
  private ComponentManager componentManager;

  @Mock
  private MonitoringUploadsManagerFactory uploadsFactory;

  @Mock
  private MonitoringUploadsManager uploadsManager;

  private final String vspId = UUID.randomUUID().toString();
  private final String versionId = UUID.randomUUID().toString();
  private final String componentId = "" + System.currentTimeMillis();
  private final String user = "cs0008";

  @Before
  public void setUp() {
    try {
      initMocks(this);

      mockStatic(ComponentManagerFactory.class);
      when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
      when(componentManagerFactory.createInterface()).thenReturn(componentManager);

      mockStatic(MonitoringUploadsManagerFactory.class);
      when(MonitoringUploadsManagerFactory.getInstance()).thenReturn(uploadsFactory);
      when(uploadsFactory.createInterface()).thenReturn(uploadsManager);

      MonitoringUploadStatus result = new MonitoringUploadStatus();
      result.setSnmpPoll("p");
      result.setSnmpTrap("t");
      result.setVesEvent("v");
      when(uploadsManager.listFilenames(
              ArgumentMatchers.eq(vspId),
              ArgumentMatchers.any(),
              ArgumentMatchers.eq(componentId))).thenReturn(result);

    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
  }

  @Test
  public void testUpload() {
    ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
    byte[] bytes = "Hello".getBytes();
    Attachment a = new Attachment("foo", new ByteArrayInputStream(bytes), new ContentDisposition("filename"));
    String type = MonitoringUploadType.SNMP_POLL.toString();
    try {
      Response rsp = bean.upload(a, vspId, versionId, componentId, type, user);
      Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
      Assert.assertNull(rsp.getEntity());
    }
    catch (Exception ex) {
      logger.error("test failed due to exception", ex);
      Assert.fail("exception caught " + ex.getMessage());
    }
  }


  @Test
  public void testDelete() {
    ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
    String type = MonitoringUploadType.SNMP_POLL.toString();
    try {
      Response rsp = bean.delete(vspId, versionId, componentId, type, user);
      Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
      Assert.assertNull(rsp.getEntity());
    }
    catch (Exception ex) {
      logger.error("test failed due to exception", ex);
      Assert.fail("exception caught " + ex.getMessage());
    }
  }

  @Test
  public void testList() {
    ComponentMonitoringUploadsImpl bean = new ComponentMonitoringUploadsImpl();
    try {
      Response rsp = bean.list(vspId, versionId, componentId, user);
      Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
      Assert.assertNotNull(rsp.getEntity());
      MonitoringUploadStatusDto dto = (MonitoringUploadStatusDto)rsp.getEntity();
      Assert.assertEquals("p",dto.getSnmpPoll());
      Assert.assertEquals("v",dto.getVesEvent());
      Assert.assertEquals("t",dto.getSnmpTrap());
    }
    catch (Exception ex) {
      logger.error("test failed due to exception", ex);
      Assert.fail("exception caught " + ex.getMessage());
    }
  }

}