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