b083cfad3a0fd3f4a2c87c13d467b967c5485ccb
[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.Before;
20 import org.junit.Test;
21 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
22 import org.openecomp.sdc.common.errors.Messages;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Optional;
26
27 import static junit.framework.TestCase.assertTrue;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30
31
32 public class ManifestParsingTest {
33
34   private Manifest manifest;
35
36   @Before
37   public void setUp(){
38     manifest = new SOL004ManifestOnboarding();
39   }
40
41   @Test
42   public void testSuccessfulParsing() throws IOException {
43     try (InputStream is = getClass()
44         .getResourceAsStream("/vspmanager.csar/manifest/ValidTosca.mf")) {
45       manifest.parse(is);
46       assertTrue(manifest.isValid());
47       assertEquals(manifest.getMetadata().size(), 4);
48       assertEquals(manifest.getSources().size(), 5);
49       Optional<ResourceTypeEnum> resourceTypeEnum = manifest.getType();
50       if(resourceTypeEnum.isPresent()){
51         assertTrue(resourceTypeEnum.get() == ResourceTypeEnum.VF);
52       }
53     }
54   }
55
56   @Test
57   public void testNoMetadataParsing() throws IOException {
58     try (InputStream is = getClass()
59         .getResourceAsStream("/vspmanager.csar/manifest/InvalidTosca1.mf")) {
60       manifest.parse(is);
61       assertFalse(manifest.isValid());
62       assertTrue(manifest.getErrors().stream().anyMatch(error -> error
63           .contains(Messages.MANIFEST_INVALID_LINE.getErrorMessage().substring(0, 10))));
64     }
65   }
66
67   @Test
68   public void testBrokenMDParsing() throws IOException {
69     try (InputStream is = getClass()
70         .getResourceAsStream("/vspmanager.csar/manifest/InvalidTosca2.mf")) {
71      manifest.parse(is);
72       assertFalse(manifest.isValid());
73       assertTrue(manifest.getErrors().stream().anyMatch(error -> error
74           .contains(Messages.MANIFEST_INVALID_LINE.getErrorMessage().substring(0, 10))));
75     }
76   }
77
78   @Test
79   public void testNoMetaParsing() throws IOException {
80     try (InputStream is = getClass()
81         .getResourceAsStream("/vspmanager.csar/manifest/InvalidTosca4.mf")) {
82       manifest.parse(is);
83       assertFalse(manifest.isValid());
84       assertTrue(manifest.getErrors().stream().anyMatch(error -> error
85           .contains(Messages.MANIFEST_NO_METADATA.getErrorMessage().substring(0, 10))));
86     }
87   }
88
89   @Test
90   public void testSuccessfulNonManoParsing() throws IOException {
91     try (InputStream is = getClass()
92             .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoTosca.mf")) {
93       manifest.parse(is);
94       assertTrue(manifest.isValid());
95       assertEquals(manifest.getMetadata().size(), 4);
96       assertEquals(manifest.getSources().size(), 5);
97       assertEquals(manifest.getNonManoSources().size(), 2);
98     }
99   }
100
101   @Test
102   public void testFailfulNonManoParsing() throws IOException {
103     try (InputStream is = getClass()
104             .getResourceAsStream("/vspmanager.csar/manifest/InValidNonManoTosca.mf")) {
105       manifest.parse(is);
106       assertFalse(manifest.isValid());
107     }
108   }
109
110   @Test
111   public void testFailfulNonManoParsingWithGarbadge() throws IOException {
112     try (InputStream is = getClass()
113             .getResourceAsStream("/vspmanager.csar/manifest/InvalidTocsaNonManoGarbadgeAtEnd.mf")) {
114       manifest.parse(is);
115       assertFalse(manifest.isValid());
116     }
117   }
118
119   @Test
120   public void testParseManifestWithNoFile() throws IOException {
121     try (InputStream is = getClass()
122             .getResourceAsStream("/vspmanager.csar/manifest/SOME_WRONG_FILE")) {
123       manifest.parse(is);
124       assertFalse(manifest.isValid());
125     }
126   }
127 }