817955efc3fe9c39240962512d882f9aee897cec
[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.vendorsoftwareproduct.dao.ComponentArtifactDao;
9 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
10 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.MonitoringUploadStatus;
11 import org.openecomp.sdc.versioning.dao.types.Version;
12 import org.testng.Assert;
13 import org.testng.annotations.AfterMethod;
14 import org.testng.annotations.BeforeMethod;
15 import org.testng.annotations.Test;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URL;
20 import java.util.Arrays;
21 import java.util.Optional;
22 import java.util.function.Consumer;
23
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.anyObject;
26 import static org.mockito.Mockito.*;
27
28 public class MonitoringUploadsManagerImplTest {
29
30   private static final String COMPONENT_ID = "COMPONENT_ID";
31   private static final String VSP_ID = "vspId";
32   private static final Version VERSION = new Version("version_id");
33   private static final String TRAP_FILE_NAME = "MMSC.zip";
34   private static final String POLL_FILE_NAME = "MNS OAM FW.zip";
35   private static final String VES_FILE_NAME = "vesTest-yml_only.zip";
36   private static final String INVALID_VES_FILE_NAME = "invalid_ves_file.zip";
37   private static final String NOT_ZIP_FILE_NAME = "notZipFile";
38   private static final String ZIP_WITH_FOLDERS_FILE_NAME = "zipFileWithFolder.zip";
39   private static final String EMPTY_ZIP_FILE_NAME = "emptyZip.zip";
40   private static final String ZIP_DIR = "/vspmanager/zips/";
41
42   @Mock
43   private ComponentArtifactDao componentArtifactDaoMock;
44   @InjectMocks
45   private MonitoringUploadsManagerImpl monitoringUploadsManager;
46
47   @BeforeMethod
48   public void setUp() throws Exception {
49     MockitoAnnotations.initMocks(this);
50   }
51
52   @AfterMethod
53   public void tearDown(){
54     monitoringUploadsManager = null;
55   }
56
57   @Test(expectedExceptions = CoreException.class)
58   public void testUploadEmptyZip() {
59     processFile(ZIP_DIR + EMPTY_ZIP_FILE_NAME, inputStream ->
60         monitoringUploadsManager
61             .upload(inputStream, EMPTY_ZIP_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID,
62                 MonitoringUploadType.SNMP_TRAP));
63   }
64
65   @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp =
66       "Invalid zip file")
67   public void testUploadInvalidZip() {
68     processFile("/notZipFile", inputStream ->
69         monitoringUploadsManager
70             .upload(inputStream, NOT_ZIP_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID,
71                 MonitoringUploadType.VES_EVENTS));
72   }
73
74   @Test
75   public void testUploadZipWithFolders() {
76
77     try {
78       processFile(ZIP_DIR + ZIP_WITH_FOLDERS_FILE_NAME, inputStream -> monitoringUploadsManager
79           .upload(inputStream, ZIP_WITH_FOLDERS_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID,
80               MonitoringUploadType.SNMP_TRAP));
81       Assert.fail();
82     } catch (Exception exception) {
83       Assert.assertEquals(exception.getMessage(), "Zip file should not contain folders");
84     }
85   }
86
87   @Test
88   public void testUploadVEsEventZipWithNonYamlFiles() {
89
90     try {
91       processFile(ZIP_DIR + INVALID_VES_FILE_NAME, inputStream -> monitoringUploadsManager
92           .upload(inputStream, INVALID_VES_FILE_NAME, VSP_ID, VERSION, COMPONENT_ID,
93               MonitoringUploadType.VES_EVENTS));
94       Assert.fail();
95     } catch (Exception exception) {
96       Assert.assertEquals(exception.getMessage(),
97           "Wrong VES EVENT Artifact was uploaded - all files contained in Artifact must be YAML " +
98               "files (using .yaml/.yml extensions)");
99     }
100   }
101
102
103   @Test
104   public void testListMonitoringFilenames() {
105     ComponentMonitoringUploadEntity artifact1 =
106         new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact1");
107     artifact1.setType(MonitoringUploadType.SNMP_TRAP);
108     artifact1.setArtifactName(TRAP_FILE_NAME);
109
110     ComponentMonitoringUploadEntity artifact2 =
111         new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact2");
112     artifact2.setType(MonitoringUploadType.SNMP_POLL);
113     artifact2.setArtifactName(POLL_FILE_NAME);
114
115     ComponentMonitoringUploadEntity artifact3 =
116         new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifact3");
117     artifact3.setType(MonitoringUploadType.VES_EVENTS);
118     artifact3.setArtifactName(VES_FILE_NAME);
119
120     doReturn(Arrays.asList(artifact1, artifact2, artifact3))
121         .when(componentArtifactDaoMock).list(anyObject());
122
123     MonitoringUploadStatus monitoringUploadStatus =
124         monitoringUploadsManager.listFilenames(VSP_ID, VERSION, COMPONENT_ID);
125
126     Assert.assertEquals(monitoringUploadStatus.getSnmpTrap(), TRAP_FILE_NAME);
127     Assert.assertEquals(monitoringUploadStatus.getSnmpPoll(), POLL_FILE_NAME);
128     Assert.assertEquals(monitoringUploadStatus.getVesEvent(), VES_FILE_NAME);
129   }
130
131   @Test(expectedExceptions = CoreException.class)
132   public void testDeleteComponentMibWhenNone() {
133     doReturn(Optional.empty()).when(componentArtifactDaoMock).getByType(any());
134     monitoringUploadsManager
135         .delete(VSP_ID, VERSION, COMPONENT_ID, MonitoringUploadType.SNMP_POLL);
136
137     verify(componentArtifactDaoMock, never()).delete(anyObject());
138   }
139
140   @Test
141   public void testDeleteComponentMonitoringUpload() {
142     doReturn(Optional
143         .of(new ComponentMonitoringUploadEntity(VSP_ID, VERSION, COMPONENT_ID, "artifactId")))
144         .when
145             (componentArtifactDaoMock).getByType(anyObject());
146
147     monitoringUploadsManager
148         .delete(VSP_ID, VERSION, COMPONENT_ID, MonitoringUploadType.SNMP_POLL);
149
150     verify(componentArtifactDaoMock).delete(anyObject());
151   }
152
153
154   private void processFile(String fileName, Consumer<InputStream> processor) {
155
156     URL url = this.getClass().getResource(fileName);
157     try (InputStream inputStream = url.openStream()) {
158       processor.accept(inputStream);
159     } catch (IOException e) {
160       throw new RuntimeException("Failed to process file: " + fileName, e);
161     }
162   }
163 }