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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.Writer;
31 import java.net.URISyntaxException;
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;
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;
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;
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;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
67 * Unit test for simple App.
69 public class AppTest {
70 static final Logger LOG = LoggerFactory.getLogger(AppTest.class);
73 public final EnvironmentVariables evars = new EnvironmentVariables();
76 public TemporaryFolder tfolder = new TemporaryFolder();
80 public void testUtils() {
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();
89 assertEquals(Utils.formatNameForJar(cs), "SomeJar-2.0");
91 CompSpec.loadComponentSpec(new File("sandbox/temp.txt"));
92 } catch (RuntimeException e) {
96 fail("Exception is not thrown");
100 public void testGetNameForJavaClass() {
101 CompList.CompShort compShort = new CompList.CompShort();
102 compShort.name = "test";
103 compShort.getNameForJavaClass();
104 compShort.componentUrl = "6:invalidURI";
106 compShort.getComponentUrlAsURI();
107 } catch (RuntimeException e) {
111 fail("Exception is not thrwon");
116 public void testDcaeProcessor() throws ProcessException {
117 DCAEProcessor px = new DCAEProcessor() {
118 public String getName() {
122 public String getVersion() {
126 public String getComponentId() {
130 public String getComponentUrl() {
134 protected List<PropertyDescriptor> buildSupportedPropertyDescriptors() {
135 return (new LinkedList<>());
138 protected Set<Relationship> buildRelationships() {
139 return (new HashSet<>());
142 public DCAEProcessor xxx() {
143 getSupportedPropertyDescriptors();
144 getSupportedPropertyDescriptors();
149 px.onTrigger((ProcessContext)null, (ProcessSession)null);
150 px.getRelationships();
151 px.getRelationships();
156 public void testPaths() throws InterruptedException, IOException, URISyntaxException {
157 /* some trivial cases */
158 new OnboardingAPIClient();
160 OnboardingAPIClient.getComponents("6:invalidURI");
161 } catch (OnboardingAPIClient.OnboardingAPIClientError oace) {
165 OnboardingAPIClient.getComponent(null);
166 } catch (OnboardingAPIClient.OnboardingAPIClientError oace) {
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 + "\"}");
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\" }]}");
196 String indexfile = tfolder.newFile("index").getPath();
197 try (Writer w = new FileWriter(indexfile)) {
200 evars.set("GENPROC_JAR_INDEX_URL", (new File(indexfile)).toURI().toURL().toString());
201 App.main(new String[0]);
203 App.main(new String[] { "-h" });
205 App.main(new String[] { "load" });
207 App.main(new String[] { "gen" });
209 URL[] jarURLs = new URL[1];
211 App.loadFromJars(jarURLs);
212 } catch (NullPointerException e) {
216 fail("Exception is not thrown");
220 public void testAddMethod() {
222 ClassPool pool = ClassPool.getDefault();
223 CtClass base = pool.get(DCAEProcessor.class.getName());
225 CtClass cc = pool.makeClass(String.format("org.onap.dcae.%s", DCAEProcessor.class));
226 cc.setSuperclass(base);
228 ProcessorBuilder.addMethod(cc, "test");
229 } catch (Exception e) {
233 fail("Exception is not thrown");