Fixed SONAR issues
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / MonitoringUploadsManagerImplTest.java
1 package org.openecomp.sdc.vendorsoftwareproduct.impl;
2
3 import org.mockito.InjectMocks;
4 import org.mockito.Mock;
5 import org.mockito.MockitoAnnotations;
6 import org.openecomp.core.enrichment.types.MonitoringUploadType;
7 import org.openecomp.sdc.common.errors.CoreException;
8 import org.openecomp.sdc.logging.api.Logger;
9 import org.openecomp.sdc.logging.api.LoggerFactory;
10 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
11 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
12 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
13 import org.openecomp.sdc.versioning.dao.types.Version;
14 import org.testng.Assert;
15 import org.testng.annotations.BeforeMethod;
16 import org.testng.annotations.Test;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21 import java.util.Arrays;
22 import java.util.Optional;
23
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.anyObject;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29
30 public class MonitoringUploadsManagerImplTest {
31
32   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
33
34   private static final String USER1 = "ComponentsUploadTestUser";
35   private static final String COMPONENT_ID = "COMPONENT_ID";
36   private static final String VSP_ID = "vspId";
37   private static final Version VERSION = new Version(0, 1);
38   private static final String trapFileName = "MMSC.zip";
39   private static final String pollFileName = "MNS OAM FW.zip";
40   private static final String vesFileName = "vesTest-yml_only.zip";
41   private static final String invalidVesFileName = "invalid_ves_file.zip";
42   private static final String notZipFileName = "notZipFile";
43   private static final String zipWithFoldersFileName = "zipFileWithFolder.zip";
44   private static final String emptyZipFileName = "emptyZip.zip";
45   private static final String ZIP_DIR = "/vspmanager/zips/";
46
47   @Mock
48   private ComponentArtifactDao componentArtifactDaoMock;
49   @InjectMocks
50   private MonitoringUploadsManagerImpl moitoringUploadsManager;
51
52   @BeforeMethod
53   public void setUp() throws Exception {
54     MockitoAnnotations.initMocks(this);
55   }
56
57   @Test(expectedExceptions = CoreException.class)
58   public void testUploadEmptyZip() {
59     InputStream zis = getFileInputStream(ZIP_DIR + emptyZipFileName);
60     moitoringUploadsManager.upload(zis, emptyZipFileName, VSP_ID, VERSION, COMPONENT_ID,
61         MonitoringUploadType.SNMP_TRAP, USER1);
62   }
63
64   @Test
65   public void testUploadInvalidZip() {
66     URL url = this.getClass().getResource("/notZipFile");
67     try {
68       moitoringUploadsManager
69           .upload(url.openStream(), notZipFileName, VSP_ID, VERSION, COMPONENT_ID,
70               MonitoringUploadType.VES_EVENTS, USER1);
71       Assert.fail();
72     } catch (Exception exception) {
73       log.debug("",exception);
74       Assert.assertEquals(exception.getMessage(), "Invalid zip file");
75     }
76   }
77
78   @Test
79   public void testUploadZipWithFolders() {
80     InputStream zis = getFileInputStream(ZIP_DIR + zipWithFoldersFileName);
81
82     try {
83       moitoringUploadsManager
84           .upload(zis, zipWithFoldersFileName, VSP_ID, VERSION, COMPONENT_ID,
85               MonitoringUploadType.SNMP_TRAP, USER1);
86       Assert.fail();
87     } catch (Exception exception) {
88       log.debug("",exception);
89       Assert.assertEquals(exception.getMessage(), "Zip file should not contain folders");
90     }
91   }
92
93   @Test
94   public void testUploadVEsEventZipWithNonYamlFiles() {
95     InputStream zis = getFileInputStream(ZIP_DIR + invalidVesFileName);
96
97     try {
98       moitoringUploadsManager
99           .upload(zis, invalidVesFileName, VSP_ID, VERSION, COMPONENT_ID,
100               MonitoringUploadType.VES_EVENTS, USER1);
101       Assert.fail();
102     } catch (Exception exception) {
103       log.debug("",exception);
104       Assert.assertEquals(exception.getMessage(),
105           "Wrong VES EVENT Artifact was uploaded - all files contained in Artifact must be YAML " +
106               "files (using .yaml/.yml extensions)");
107     }
108   }
109
110
111   @Test
112   public void testListMonitoringFilenames() {
113     ComponentMonitoringUploadEntity artifact1 =
114         new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact1");
115     artifact1.setType(MonitoringUploadType.SNMP_TRAP);
116     artifact1.setArtifactName(trapFileName);
117
118     ComponentMonitoringUploadEntity artifact2 =
119         new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact2");
120     artifact2.setType(MonitoringUploadType.SNMP_POLL);
121     artifact2.setArtifactName(pollFileName);
122
123     ComponentMonitoringUploadEntity artifact3 =
124         new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact3");
125     artifact3.setType(MonitoringUploadType.VES_EVENTS);
126     artifact3.setArtifactName(vesFileName);
127
128     doReturn(Arrays.asList(artifact1, artifact2, artifact3))
129         .when(componentArtifactDaoMock).list(anyObject());
130
131     MonitoringUploadStatus monitoringUploadStatus =
132         moitoringUploadsManager.listFilenames(VSP_ID, VERSION, COMPONENT_ID, USER1);
133
134     Assert.assertEquals(monitoringUploadStatus.getSnmpTrap(), trapFileName);
135     Assert.assertEquals(monitoringUploadStatus.getSnmpPoll(), pollFileName);
136     Assert.assertEquals(monitoringUploadStatus.getVesEvent(), vesFileName);
137   }
138
139   @Test (expectedExceptions = CoreException.class)
140   public void testDeleteComponentMibWhenNone() {
141     doReturn(Optional.empty()).when(componentArtifactDaoMock).getByType(any());
142     moitoringUploadsManager
143         .delete(VSP_ID, VERSION, COMPONENT_ID, MonitoringUploadType.SNMP_POLL, USER1);
144
145     verify(componentArtifactDaoMock, never()).delete(anyObject());
146   }
147
148   @Test
149   public void testDeleteComponentMonitoringUpload() {
150     doReturn(Optional
151         .of(new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifactId")))
152         .when
153             (componentArtifactDaoMock).getByType(anyObject());
154
155     moitoringUploadsManager
156         .delete(VSP_ID, VERSION, COMPONENT_ID, MonitoringUploadType.SNMP_POLL, USER1);
157
158     verify(componentArtifactDaoMock).delete(anyObject());
159   }
160
161
162   private InputStream getFileInputStream(String fileName) {
163     URL url = this.getClass().getResource(fileName);
164     try {
165       return url.openStream();
166     } catch (IOException exception) {
167       log.debug("",exception);
168       return null;
169     }
170   }
171 }