Changes for checkstyle 8.32
[policy/apex-pdp.git] / examples / examples-grpc / src / test / java / org / onap / policy / apex / examples / grpc / TestApexGrpcExample.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
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.examples.grpc;
22
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
25
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.concurrent.TimeUnit;
29 import javax.ws.rs.client.Client;
30 import javax.ws.rs.client.ClientBuilder;
31 import javax.ws.rs.core.Response;
32 import org.junit.Test;
33 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
34 import org.onap.policy.apex.service.engine.main.ApexMain;
35
36 /**
37  * Test class to run an example policy for APEX-CDS interaction over gRPC. Event received on
38  * unauthenticated.DCAE_CL_OUTPUT DMaaP topic (dummy REST Endpoint here) triggers the policy Based on the event, a
39  * create/delete subscription gRPC request is triggered to the CDS (a dummy gRPC server here). Response received from
40  * CDS is used to send a final output Log event on POLICY_CL_MGT topic.
41  */
42 public class TestApexGrpcExample {
43     @Test
44     public void testGrpcExample() throws Exception {
45         // @formatter:off
46         final String[] cliArgs = new String[] {
47             "-c",
48             "src/main/resources/policy/APEXgRPCPolicy.apex",
49             "-l",
50             "target/APEXgRPCPolicyModel.log",
51             "-o",
52             "target/classes/APEXgRPCPolicy.json"
53         };
54         // @formatter:on
55
56         new ApexCommandLineEditorMain(cliArgs);
57
58         // @formatter:off
59         final String[] apexArgs = {
60             "-rfr",
61             "target/classes",
62             "-c",
63             "src/main/resources/examples/config/APEXgRPC/ApexConfig.json",
64             "-m",
65             "target/classes/APEXgRPCPolicy.json"
66         };
67         // @formatter:on
68
69         final GrpcTestServerSim sim = new GrpcTestServerSim();
70
71         final Client client = ClientBuilder.newClient();
72         final ApexMain apexMain = new ApexMain(apexArgs);
73
74         await().atMost(5000, TimeUnit.MILLISECONDS).until(() -> apexMain.isAlive());
75
76         String getLoggedEventUrl = "http://localhost:54321/GrpcTestRestSim/sim/event/getLoggedEvent";
77         // wait for success response code to be received, until a timeout
78         await().atMost(20000, TimeUnit.MILLISECONDS).until(() -> {
79             return 200 == client.target(getLoggedEventUrl).request("application/json").get().getStatus();
80         });
81         apexMain.shutdown();
82         Response response = client.target(getLoggedEventUrl).request("application/json").get();
83         sim.tearDown();
84         String responseEntity = response.readEntity(String.class);
85
86         String expectedLoggedOutputEvent = Files
87             .readString(Paths.get("src/main/resources/examples/events/APEXgRPC/LogEvent.json")).replaceAll("\r", "");
88         assertEquals(expectedLoggedOutputEvent, responseEntity);
89     }
90 }