e08238aa1fd2a0b5824f990560de79d9c433ea90
[sdc.git] /
1 /*
2  * Copyright (C) 2017 Huawei Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.common.utils;
18
19 import static org.hamcrest.CoreMatchers.is;
20 import static org.hamcrest.CoreMatchers.notNullValue;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.junit.Assert.fail;
23 import static org.onap.sdc.tosca.services.CommonUtil.DEFAULT;
24 import static org.onap.sdc.tosca.services.CommonUtil.UNDERSCORE_DEFAULT;
25 import static org.testng.Assert.assertTrue;
26
27 import com.google.common.io.Files;
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.openecomp.core.utilities.file.FileContentHandler;
33 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
34 import org.openecomp.sdc.common.errors.CoreException;
35 import org.openecomp.sdc.common.zip.exception.ZipException;
36 import org.testng.annotations.Test;
37
38 public class CommonUtilTest {
39     private static final String VALID_ZIP_FILE_PATH = "src/test/resources/valid.zip";
40     private static final String VALID_CSAR_FILE_PATH = "src/test/resources/valid.csar";
41     private static final String VALID_ZIP_WITH_NOT_YAML_FILE_PATH = "src/test/resources/valid_zip_with_not_yaml_file.zip";
42     private static final String VALID_ZIP_WITH_DIR_FILE_PATH = "src/test/resources/valid_zip_with_dir.zip";
43
44     @Test
45     public void testGetObjectAsMap() {
46         final Map<String, String> obj = new HashMap<>(1);
47         obj.put(DEFAULT, "");
48         assertTrue(CommonUtil.getObjectAsMap(obj).containsKey(UNDERSCORE_DEFAULT));
49     }
50
51     @Test
52     public void testValidateAndUploadFileContentZip() throws IOException {
53         byte[] file = getFileAsBytes(VALID_ZIP_FILE_PATH);
54
55         FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, file);
56
57         assertThat(fch, notNullValue(FileContentHandler.class));
58         assertThat(fch.containsFile("file.one.yaml"), is(true));
59         assertThat(fch.containsFile("file.two.yaml"), is(true));
60     }
61
62     @Test
63     public void testValidateAndUploadFileContentCsar() throws IOException {
64         byte[] file = getFileAsBytes(VALID_CSAR_FILE_PATH);
65
66         FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.CSAR, file);
67
68         assertThat(fch, notNullValue(FileContentHandler.class));
69         assertThat(fch.containsFile("file.one.yaml"), is(true));
70         assertThat(fch.containsFile("file.two.yaml"), is(true));
71     }
72
73     @Test(expectedExceptions={CoreException.class})
74     public void testValidateNoFolders() throws IOException {
75         byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
76
77         FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, file);
78
79         fail("Should throw CoreException");
80     }
81
82     @Test
83     public void testGetZipContent() throws ZipException {
84         byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
85
86         FileContentHandler fch = CommonUtil.getZipContent(file);
87
88         assertThat(fch, notNullValue(FileContentHandler.class));
89         assertThat(fch.getFileList().size(), is(2));
90         assertThat(fch.getFolderList().size(), is(1));
91     }
92
93     @Test
94     public void testValidateAllFilesYaml() throws ZipException {
95         byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
96         FileContentHandler fch = CommonUtil.getZipContent(file);
97
98         boolean result = CommonUtil.validateAllFilesYml(fch);
99
100         assertThat(result, is(true));
101     }
102
103     @Test
104     public void testValidateNotAllFilesYaml() throws ZipException {
105         byte[] file = getFileAsBytes(VALID_ZIP_WITH_NOT_YAML_FILE_PATH);
106         FileContentHandler fch = CommonUtil.getZipContent(file);
107
108         boolean result = CommonUtil.validateAllFilesYml(fch);
109
110         assertThat(result, is(false));
111     }
112
113     private byte[] getFileAsBytes(String fileName) {
114         byte[] data = null;
115         try {
116             File file = new File(fileName);
117             data = Files.toByteArray(file);
118         } catch (IOException e) {
119             fail("Couldn't read test file");
120         }
121         return data;
122     }
123 }