f8898a7c534bdf5b9a90e776b411d15bfc4778c0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. 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  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.apps.uservice.test.adapt.restclient;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.net.URI;
30
31 import org.glassfish.grizzly.http.server.HttpServer;
32 import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
39 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
41 import org.onap.policy.apex.model.utilities.TextFileUtils;
42 import org.onap.policy.apex.service.engine.main.ApexMain;
43
44
45 public class TestREST2File {
46
47     private static final String BASE_URI = "http://localhost:32801/TestRest2File";
48     private HttpServer server;
49
50     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
51     private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
52
53     private final PrintStream stdout = System.out;
54     private final PrintStream stderr = System.err;
55
56     @Before
57     public void setUp() throws Exception {
58         final ResourceConfig rc = new ResourceConfig(TestRESTClientEndpoint.class);
59         server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
60
61         while (!server.isStarted()) {
62             ThreadUtilities.sleep(50);
63         }
64     }
65
66     @After
67     public void tearDown() throws Exception {
68         server.shutdown();
69     }
70
71     @AfterClass
72     public static void deleteTempFiles() {
73         new File("src/test/resources/events/EventsOut.json").delete();
74     }
75
76     @Test
77     public void testRESTEventsIn() throws MessagingException, ApexException, IOException {
78         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEvent.json"};
79
80         final ApexMain apexMain = new ApexMain(args);
81
82         ThreadUtilities.sleep(1000);
83         apexMain.shutdown();
84
85         final String outputEventText = TextFileUtils.getTextFileAsString("src/test/resources/events/EventsOut.json");
86         assertTrue(outputEventText.contains(
87                 "04\",\n" + "  \"version\": \"0.0.1\",\n" + "  \"nameSpace\": \"org.onap.policy.apex.sample.events\""));
88     }
89
90     @Test
91     public void testFileEmptyEvents() throws MessagingException, ApexException, IOException {
92         System.setOut(new PrintStream(outContent));
93         System.setErr(new PrintStream(errContent));
94
95         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEmptyEvents.json"};
96         final ApexMain apexMain = new ApexMain(args);
97
98         ThreadUtilities.sleep(1000);
99         apexMain.shutdown();
100
101         final String outString = outContent.toString();
102
103         System.setOut(stdout);
104         System.setErr(stderr);
105
106         assertTrue(outString.contains(
107                 "received an empty event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEmptyEvent\""));
108     }
109
110     @Test
111     public void testFileEventsNoURL() throws MessagingException, ApexException, IOException {
112         System.setOut(new PrintStream(outContent));
113         System.setErr(new PrintStream(errContent));
114
115         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventNoURL.json"};
116         final ApexMain apexMain = new ApexMain(args);
117
118         ThreadUtilities.sleep(1000);
119         apexMain.shutdown();
120
121         final String outString = outContent.toString();
122
123         System.setOut(stdout);
124         System.setErr(stderr);
125
126         assertTrue(outString.contains("  no URL has been set for event sending on REST client"));
127     }
128
129     @Test
130     public void testFileEventsBadURL() throws MessagingException, ApexException, IOException {
131         System.setOut(new PrintStream(outContent));
132         System.setErr(new PrintStream(errContent));
133
134         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadURL.json"};
135         final ApexMain apexMain = new ApexMain(args);
136
137         ThreadUtilities.sleep(1000);
138         apexMain.shutdown();
139
140         final String outString = outContent.toString();
141
142         System.setOut(stdout);
143         System.setErr(stderr);
144
145         assertTrue(outString.contains(
146                 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/Bad\" failed with status code 404"));
147     }
148
149     @Test
150     public void testFileEventsBadHTTPMethod() throws MessagingException, ApexException, IOException {
151         System.setOut(new PrintStream(outContent));
152         System.setErr(new PrintStream(errContent));
153
154         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadHTTPMethod.json"};
155         final ApexMain apexMain = new ApexMain(args);
156
157         ThreadUtilities.sleep(1000);
158         apexMain.shutdown();
159
160         final String outString = outContent.toString();
161
162         System.setOut(stdout);
163         System.setErr(stderr);
164
165         assertTrue(outString.contains(
166                 "specified HTTP method of \"POST\" is invalid, only HTTP method \"GET\" is supported for event reception on REST client consumer"));
167     }
168
169     @Test
170     public void testFileEventsBadResponse() throws MessagingException, ApexException, IOException {
171         System.setOut(new PrintStream(outContent));
172         System.setErr(new PrintStream(errContent));
173
174         final String[] args = {"src/test/resources/prodcons/REST2FileJsonEventBadResponse.json"};
175         final ApexMain apexMain = new ApexMain(args);
176
177         ThreadUtilities.sleep(1000);
178         apexMain.shutdown();
179
180         final String outString = outContent.toString();
181
182         System.setOut(stdout);
183         System.setErr(stderr);
184
185         assertTrue(outString.contains(
186                 "reception of event from URL \"http://localhost:32801/TestRest2File/apex/event/GetEventBadResponse\" failed with status code 400 and message \"\""));
187     }
188 }