2 * Copyright (C) 2017 Huawei Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.common.utils;
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;
27 import com.google.common.io.Files;
29 import java.io.IOException;
30 import java.util.HashMap;
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;
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";
45 public void testGetObjectAsMap() {
46 final Map<String, String> obj = new HashMap<>(1);
48 assertTrue(CommonUtil.getObjectAsMap(obj).containsKey(UNDERSCORE_DEFAULT));
52 public void testValidateAndUploadFileContentZip() throws IOException {
53 byte[] file = getFileAsBytes(VALID_ZIP_FILE_PATH);
55 FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, file);
57 assertThat(fch, notNullValue(FileContentHandler.class));
58 assertThat(fch.containsFile("file.one.yaml"), is(true));
59 assertThat(fch.containsFile("file.two.yaml"), is(true));
63 public void testValidateAndUploadFileContentCsar() throws IOException {
64 byte[] file = getFileAsBytes(VALID_CSAR_FILE_PATH);
66 FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.CSAR, file);
68 assertThat(fch, notNullValue(FileContentHandler.class));
69 assertThat(fch.containsFile("file.one.yaml"), is(true));
70 assertThat(fch.containsFile("file.two.yaml"), is(true));
73 @Test(expectedExceptions={CoreException.class})
74 public void testValidateNoFolders() throws IOException {
75 byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
77 FileContentHandler fch = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, file);
79 fail("Should throw CoreException");
83 public void testGetZipContent() throws ZipException {
84 byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
86 FileContentHandler fch = CommonUtil.getZipContent(file);
88 assertThat(fch, notNullValue(FileContentHandler.class));
89 assertThat(fch.getFileList().size(), is(2));
90 assertThat(fch.getFolderList().size(), is(1));
94 public void testValidateAllFilesYaml() throws ZipException {
95 byte[] file = getFileAsBytes(VALID_ZIP_WITH_DIR_FILE_PATH);
96 FileContentHandler fch = CommonUtil.getZipContent(file);
98 boolean result = CommonUtil.validateAllFilesYml(fch);
100 assertThat(result, is(true));
104 public void testValidateNotAllFilesYaml() throws ZipException {
105 byte[] file = getFileAsBytes(VALID_ZIP_WITH_NOT_YAML_FILE_PATH);
106 FileContentHandler fch = CommonUtil.getZipContent(file);
108 boolean result = CommonUtil.validateAllFilesYml(fch);
110 assertThat(result, is(false));
113 private byte[] getFileAsBytes(String fileName) {
116 File file = new File(fileName);
117 data = Files.toByteArray(file);
118 } catch (IOException e) {
119 fail("Couldn't read test file");