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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.examples.grpc;
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 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;
34 import org.junit.Test;
35 import org.onap.policy.apex.auth.clieditor.ApexCommandLineEditorMain;
36 import org.onap.policy.apex.service.engine.main.ApexMain;
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.
44 public class TestApexGrpcExample {
46 public void testGrpcExample() throws Exception {
48 final String[] cliArgs = new String[] {
50 "src/main/resources/policy/APEXgRPCPolicy.apex",
52 "target/APEXgRPCPolicyModel.log",
54 "target/classes/APEXgRPCPolicy.json"
58 new ApexCommandLineEditorMain(cliArgs);
61 final String[] apexArgs = {
65 "src/main/resources/examples/config/APEXgRPC/ApexConfig.json",
67 "target/classes/APEXgRPCPolicy.json"
71 final GrpcTestServerSim sim = new GrpcTestServerSim();
73 final Client client = ClientBuilder.newClient();
74 final ApexMain apexMain = new ApexMain(apexArgs);
76 await().atMost(5000, TimeUnit.MILLISECONDS).until(() -> apexMain.isAlive());
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();
84 Response response = client.target(getLoggedEventUrl).request("application/json").get();
86 String responseEntity = response.readEntity(String.class);
88 String expectedLoggedOutputEvent = Files
89 .readString(Paths.get("src/main/resources/examples/events/APEXgRPC/LogEvent.json")).replaceAll("\r", "");
90 assertEquals(expectedLoggedOutputEvent, responseEntity);