4e86f664d665fdb91de6717c9565b4b78f65df69
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.mso.global_tests;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.DirectoryStream;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.Iterator;
30
31 import org.jboss.shrinkwrap.api.Archive;
32 import org.jboss.shrinkwrap.api.ShrinkWrap;
33 import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
34 import org.jboss.shrinkwrap.api.spec.JavaArchive;
35 import org.jboss.shrinkwrap.api.spec.WebArchive;
36
37 public class ArquillianPackagerForITCases {
38
39     public static Archive<?> createPackageFromExistingOne(String path, String globPattern, String newPackageName) {
40         Path dir = Paths.get(path);
41
42         try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, globPattern)) {
43             Iterator<Path> it = stream.iterator();
44             if (it.hasNext()) {
45
46                 if (newPackageName.endsWith(".war")) {
47                     File archive = it.next().toFile();
48                     WebArchive webArchive = ShrinkWrap.create(WebArchive.class, newPackageName);
49                     webArchive.merge((ShrinkWrap.createFromZipFile(WebArchive.class, archive)));
50                     return webArchive;
51                 } else if (newPackageName.endsWith(".jar")) {
52                     File archive = it.next().toFile();
53                     JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class, newPackageName);
54                     javaArchive.merge((ShrinkWrap.createFromZipFile(JavaArchive.class, archive)));
55                     return javaArchive;
56                 } else if (newPackageName.endsWith(".ear")) {
57                     File archive = it.next().toFile();
58                     EnterpriseArchive earArchive = ShrinkWrap.create(EnterpriseArchive.class, newPackageName);
59                     earArchive.merge((ShrinkWrap.createFromZipFile(EnterpriseArchive.class, archive)));
60                     return earArchive;
61                 } else {
62                     return null;
63                 }
64
65             } else {
66                 return null;
67             }
68
69         } catch (IOException e) {
70             e.printStackTrace();
71             return null;
72         }
73
74     }
75
76
77 }