d6a1ca35212e64ebb52b3d361c5107ffd376f998
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package sandbox;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.Writer;
28
29 import java.net.URISyntaxException;
30
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Set;
36
37 import org.apache.nifi.components.PropertyDescriptor;
38 import org.apache.nifi.processor.ProcessContext;
39 import org.apache.nifi.processor.ProcessSession;
40 import org.apache.nifi.processor.Relationship;
41 import org.apache.nifi.processor.exception.ProcessException;
42
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.contrib.java.lang.system.EnvironmentVariables;
46 import org.junit.rules.TemporaryFolder;
47
48 import org.onap.dcae.genprocessor.App;
49 import org.onap.dcae.genprocessor.CompSpec;
50 import org.onap.dcae.genprocessor.DCAEProcessor;
51 import org.onap.dcae.genprocessor.OnboardingAPIClient;
52 import org.onap.dcae.genprocessor.Utils;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * Unit test for simple App.
59  */
60 public class AppTest {
61     static final Logger LOG = LoggerFactory.getLogger(AppTest.class);
62
63     @Rule
64     public final EnvironmentVariables evars = new EnvironmentVariables();
65
66     @Rule
67     public TemporaryFolder tfolder = new TemporaryFolder();
68
69
70     @Test
71     public void testUtils() {
72         new Utils();
73         assertEquals(Utils.formatNameForJavaClass("part1.a-bee"), "Part1ABee");
74         HashMap<String, String> mx = new HashMap<>();
75         mx.put("name", "SomeJar");
76         mx.put("version", "2.0");
77         mx.put("description", "desc");
78         CompSpec cs = new CompSpec();
79         cs.unpackSelf(mx);
80         assertEquals(Utils.formatNameForJar(cs), "SomeJar-2.0");
81     }
82
83
84     @Test
85     public void testDcaeProcessor() throws ProcessException {
86         DCAEProcessor px = new DCAEProcessor() {
87             public String getName() {
88                 return (null);
89             }
90
91             public String getVersion() {
92                 return (null);
93             }
94
95             public String getComponentId() {
96                 return (null);
97             }
98
99             public String getComponentUrl() {
100                 return (null);
101             }
102
103             protected List<PropertyDescriptor> buildSupportedPropertyDescriptors() {
104                 return (new LinkedList<>());
105             }
106
107             protected Set<Relationship> buildRelationships() {
108                 return (new HashSet<>());
109             }
110
111             public DCAEProcessor xxx() {
112                 getSupportedPropertyDescriptors();
113                 getSupportedPropertyDescriptors();
114                 return (this);
115             }
116             }.xxx();
117         px.ping();
118         px.onTrigger((ProcessContext)null, (ProcessSession)null);
119         px.getRelationships();
120         px.getRelationships();
121     }
122
123
124     @Test
125     public void testPaths() throws InterruptedException, IOException, URISyntaxException {
126         /* some trivial cases */
127         new OnboardingAPIClient();
128         try {
129             OnboardingAPIClient.getComponents("6:invalidURI");
130         } catch (OnboardingAPIClient.OnboardingAPIClientError oace) {
131             // expected case
132         }
133         try {
134             OnboardingAPIClient.getComponent(null);
135         } catch (OnboardingAPIClient.OnboardingAPIClientError oace) {
136             // expected case
137         }
138         /* background one shot failure cases */
139         evars.clear("GENPROC_SLEEP_SEC");
140         App.main(new String[0]);
141         evars.set("GENPROC_SLEEP_SEC", "0");
142         String wdir = tfolder.newFolder("work").getPath();
143         evars.set("GENPROC_WORKING_DIR", wdir);
144         String onboardingdir = tfolder.newFolder("onboarding").getPath();
145         evars.set("GENPROC_ONBOARDING_API_HOST", (new File(onboardingdir)).toURI().toURL().toString());
146         String compfile = onboardingdir + "/compone";
147         try (Writer w = new FileWriter(compfile)) {
148             w.write("{ \"id\": \"1\", \"spec\": { \"name\": \"one-collector\","
149                 + " \"version\": \"1.0.0\", \"description\": \"desc\","
150                 + " \"parameters\": [{\"name\": \"p1\", \"value\": \"v1\","
151                 + " \"description\": \"d1\"}], \"streams\":"
152                 + " {\"publishes\":[{\"format\": \"f1\", \"version\": \"v1\","
153                 + " \"type\": \"t1\", \"config_key\": \"ck1\"}],"
154                 + " \"subscribes\":[{\"format\": \"f2\", \"version\": \"v2\","
155                 + " \"type\": \"t2\", \"config_key\": \"ck2\"}]}},"
156                 + " \"selfUrl\": \"file:" + compfile + "\"}");
157         }
158         try (Writer w = new FileWriter(onboardingdir + "/components")) {
159             w.write("{\"components\": [{\"id\": \"1\", \"name\": \"one\","
160                 + " \"version\": \"1.0.0\", \"description\": \"desc\","
161                 + " \"componentType\": \"apple\", \"owner\": \"John Doe\","
162                 + " \"componentUrl\": \"file:" + compfile + "\","
163                 + " \"whenAdded\": \"never\" }]}");
164         }
165         String indexfile = tfolder.newFile("index").getPath();
166         try (Writer w = new FileWriter(indexfile)) {
167             w.write("[]");
168         }
169         evars.set("GENPROC_JAR_INDEX_URL", (new File(indexfile)).toURI().toURL().toString());
170         App.main(new String[0]);
171         /* help case */
172         App.main(new String[] { "-h" });
173         /* load case */
174         App.main(new String[] { "load" });
175         /* gen case */
176         App.main(new String[] { "gen" });
177     }
178 }