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