c6008f03d7a6d60498c7f343e98a3192e643b8be
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
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.tosca.csar;
18
19 import org.junit.Test;
20 import org.openecomp.sdc.common.errors.Messages;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import static junit.framework.TestCase.assertTrue;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28
29
30 public class ManifestParsingTest {
31
32   @Test
33   public void testSuccessfulParsing() throws IOException {
34     try (InputStream is = getClass()
35         .getResourceAsStream("/vspmanager.csar/manifest/ValidTosca.mf")) {
36       Manifest onboardingManifest = OnboardingManifest.parse(is);
37       assertTrue(onboardingManifest.isValid());
38       assertEquals(onboardingManifest.getMetadata().size(), 4);
39       assertEquals(onboardingManifest.getSources().size(), 5);
40     }
41   }
42
43   @Test
44   public void testNoMetadataParsing() throws IOException {
45     try (InputStream is = getClass()
46         .getResourceAsStream("/vspmanager.csar/manifest/InvalidTosca1.mf")) {
47       Manifest onboardingManifest = OnboardingManifest.parse(is);
48       assertFalse(onboardingManifest.isValid());
49       assertTrue(onboardingManifest.getErrors().stream().anyMatch(error -> error
50           .contains(Messages.MANIFEST_INVALID_LINE.getErrorMessage().substring(0, 10))));
51     }
52   }
53
54   @Test
55   public void testBrokenMDParsing() throws IOException {
56     try (InputStream is = getClass()
57         .getResourceAsStream("/vspmanager.csar/manifest/InvalidTosca2.mf")) {
58       Manifest onboardingManifest = OnboardingManifest.parse(is);
59       assertFalse(onboardingManifest.isValid());
60       assertTrue(onboardingManifest.getErrors().stream().anyMatch(error -> error
61           .contains(Messages.MANIFEST_INVALID_LINE.getErrorMessage().substring(0, 10))));
62     }
63   }
64
65   @Test
66   public void testNoMetaParsing() throws IOException {
67     try (InputStream is = getClass()
68         .getResourceAsStream("/vspmanager.csar/manifest/InvalidTosca4.mf")) {
69       Manifest onboardingManifest = OnboardingManifest.parse(is);
70       assertFalse(onboardingManifest.isValid());
71       assertTrue(onboardingManifest.getErrors().stream().anyMatch(error -> error
72           .contains(Messages.MANIFEST_NO_METADATA.getErrorMessage().substring(0, 10))));
73     }
74   }
75
76   @Test
77   public void testSuccessfulNonManoParsing() throws IOException {
78     try (InputStream is = getClass()
79             .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoTosca.mf")) {
80       Manifest onboardingManifest = OnboardingManifest.parse(is);
81       assertTrue(onboardingManifest.isValid());
82       assertEquals(onboardingManifest.getMetadata().size(), 4);
83       assertEquals(onboardingManifest.getSources().size(), 5);
84       assertEquals(onboardingManifest.getNonManoSources().size(), 2);
85     }
86   }
87
88   @Test
89   public void testFailfulNonManoParsing() throws IOException {
90     try (InputStream is = getClass()
91             .getResourceAsStream("/vspmanager.csar/manifest/InValidNonManoTosca.mf")) {
92       Manifest onboardingManifest = OnboardingManifest.parse(is);
93       assertFalse(onboardingManifest.isValid());
94     }
95   }
96
97   @Test
98   public void testFailfulNonManoParsingWithGarbadge() throws IOException {
99     try (InputStream is = getClass()
100             .getResourceAsStream("/vspmanager.csar/manifest/InvalidTocsaNonManoGarbadgeAtEnd.mf")) {
101       Manifest onboardingManifest = OnboardingManifest.parse(is);
102       assertFalse(onboardingManifest.isValid());
103     }
104   }
105
106   @Test
107   public void testParseManifestWithNoFile() throws IOException {
108     try (InputStream is = getClass()
109             .getResourceAsStream("/vspmanager.csar/manifest/SOME_WRONG_FILE")) {
110       Manifest onboardingManifest = OnboardingManifest.parse(is);
111       assertFalse(onboardingManifest.isValid());
112     }
113   }
114 }