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