CodeCoverage improvement for dcaegen2-platform-mod-genprocessor
[dcaegen2/platform.git] / mod / genprocessor / src / test / java / sandbox / AppTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * Copyright (C) 2022 Huawei. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * 
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package sandbox;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.Writer;
30
31 import java.net.URISyntaxException;
32
33 import java.net.URL;
34 import java.net.URLClassLoader;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Set;
40
41 import javassist.ClassPool;
42 import javassist.CtClass;
43 import org.apache.nifi.components.PropertyDescriptor;
44 import org.apache.nifi.processor.ProcessContext;
45 import org.apache.nifi.processor.ProcessSession;
46 import org.apache.nifi.processor.Relationship;
47 import org.apache.nifi.processor.exception.ProcessException;
48
49 import org.junit.Rule;
50 import org.junit.Test;
51 import org.junit.contrib.java.lang.system.EnvironmentVariables;
52 import org.junit.rules.TemporaryFolder;
53
54 import org.onap.dcae.genprocessor.App;
55 import org.onap.dcae.genprocessor.CompSpec;
56 import org.onap.dcae.genprocessor.DCAEProcessor;
57 import org.onap.dcae.genprocessor.OnboardingAPIClient;
58 import org.onap.dcae.genprocessor.Utils;
59 import org.onap.dcae.genprocessor.CompList;
60 import org.onap.dcae.genprocessor.ProcessorBuilder;
61
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65
66 /**
67  * Unit test for simple App.
68  */
69 public class AppTest {
70     static final Logger LOG = LoggerFactory.getLogger(AppTest.class);
71
72     @Rule
73     public final EnvironmentVariables evars = new EnvironmentVariables();
74
75     @Rule
76     public TemporaryFolder tfolder = new TemporaryFolder();
77
78
79     @Test
80     public void testUtils() {
81         new Utils();
82         assertEquals(Utils.formatNameForJavaClass("part1.a-bee"), "Part1ABee");
83         HashMap<String, String> mx = new HashMap<>();
84         mx.put("name", "SomeJar");
85         mx.put("version", "2.0");
86         mx.put("description", "desc");
87         CompSpec cs = new CompSpec();
88         cs.unpackSelf(mx);
89         assertEquals(Utils.formatNameForJar(cs), "SomeJar-2.0");
90         try {
91             CompSpec.loadComponentSpec(new File("sandbox/temp.txt"));
92         } catch (RuntimeException e) {
93             // expected case
94             return;
95         }
96         fail("Exception is not thrown");
97     }
98
99     @Test
100     public void testGetNameForJavaClass() {
101         CompList.CompShort compShort = new CompList.CompShort();
102         compShort.name = "test";
103         compShort.getNameForJavaClass();
104         compShort.componentUrl = "6:invalidURI";
105         try {
106             compShort.getComponentUrlAsURI();
107         } catch (RuntimeException e) {
108             // expected case
109             return;
110         }
111         fail("Exception is not thrwon");
112     }
113
114
115     @Test
116     public void testDcaeProcessor() throws ProcessException {
117         DCAEProcessor px = new DCAEProcessor() {
118             public String getName() {
119                 return (null);
120             }
121
122             public String getVersion() {
123                 return (null);
124             }
125
126             public String getComponentId() {
127                 return (null);
128             }
129
130             public String getComponentUrl() {
131                 return (null);
132             }
133
134             protected List<PropertyDescriptor> buildSupportedPropertyDescriptors() {
135                 return (new LinkedList<>());
136             }
137
138             protected Set<Relationship> buildRelationships() {
139                 return (new HashSet<>());
140             }
141
142             public DCAEProcessor xxx() {
143                 getSupportedPropertyDescriptors();
144                 getSupportedPropertyDescriptors();
145                 return (this);
146             }
147             }.xxx();
148         px.ping();
149         px.onTrigger((ProcessContext)null, (ProcessSession)null);
150         px.getRelationships();
151         px.getRelationships();
152     }
153
154
155     @Test
156     public void testPaths() throws InterruptedException, IOException, URISyntaxException {
157         /* some trivial cases */
158         new OnboardingAPIClient();
159         try {
160             OnboardingAPIClient.getComponents("6:invalidURI");
161         } catch (OnboardingAPIClient.OnboardingAPIClientError oace) {
162             // expected case
163         }
164         try {
165             OnboardingAPIClient.getComponent(null);
166         } catch (OnboardingAPIClient.OnboardingAPIClientError oace) {
167             // expected case
168         }
169         /* background one shot failure cases */
170         evars.clear("GENPROC_SLEEP_SEC");
171         App.main(new String[0]);
172         evars.set("GENPROC_SLEEP_SEC", "0");
173         String wdir = tfolder.newFolder("work").getPath();
174         evars.set("GENPROC_WORKING_DIR", wdir);
175         String onboardingdir = tfolder.newFolder("onboarding").getPath();
176         evars.set("GENPROC_ONBOARDING_API_HOST", (new File(onboardingdir)).toURI().toURL().toString());
177         String compfile = onboardingdir + "/compone";
178         try (Writer w = new FileWriter(compfile)) {
179             w.write("{ \"id\": \"1\", \"spec\": { \"name\": \"one-collector\","
180                 + " \"version\": \"1.0.0\", \"description\": \"desc\","
181                 + " \"parameters\": [{\"name\": \"p1\", \"value\": \"v1\","
182                 + " \"description\": \"d1\"}], \"streams\":"
183                 + " {\"publishes\":[{\"format\": \"f1\", \"version\": \"v1\","
184                 + " \"type\": \"t1\", \"config_key\": \"ck1\"}],"
185                 + " \"subscribes\":[{\"format\": \"f2\", \"version\": \"v2\","
186                 + " \"type\": \"t2\", \"config_key\": \"ck2\"}]}},"
187                 + " \"selfUrl\": \"file:" + compfile + "\"}");
188         }
189         try (Writer w = new FileWriter(onboardingdir + "/components")) {
190             w.write("{\"components\": [{\"id\": \"1\", \"name\": \"one\","
191                 + " \"version\": \"1.0.0\", \"description\": \"desc\","
192                 + " \"componentType\": \"apple\", \"owner\": \"John Doe\","
193                 + " \"componentUrl\": \"file:" + compfile + "\","
194                 + " \"whenAdded\": \"never\" }]}");
195         }
196         String indexfile = tfolder.newFile("index").getPath();
197         try (Writer w = new FileWriter(indexfile)) {
198             w.write("[]");
199         }
200         evars.set("GENPROC_JAR_INDEX_URL", (new File(indexfile)).toURI().toURL().toString());
201         App.main(new String[0]);
202         /* help case */
203         App.main(new String[] { "-h" });
204         /* load case */
205         App.main(new String[] { "load" });
206         /* gen case */
207         App.main(new String[] { "gen" });
208
209         URL[] jarURLs = new URL[1];
210         try {
211             App.loadFromJars(jarURLs);
212         } catch (NullPointerException e) {
213             // expected case
214             return;
215         }
216         fail("Exception is not thrown");
217     }
218
219     @Test
220     public void testAddMethod() {
221         try {
222             ClassPool pool = ClassPool.getDefault();
223             CtClass base = pool.get(DCAEProcessor.class.getName());
224
225             CtClass cc = pool.makeClass(String.format("org.onap.dcae.%s", DCAEProcessor.class));
226             cc.setSuperclass(base);
227
228             ProcessorBuilder.addMethod(cc, "test");
229         } catch (Exception e) {
230             // expected case
231             return;
232         }
233         fail("Exception is not thrown");
234     }
235 }
236