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;
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;
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.
42 public class TestApexGrpcExample {
44 public void testGrpcExample() throws Exception {
46 final String[] cliArgs = new String[] {
48 "src/main/resources/policy/APEXgRPCPolicy.apex",
50 "target/APEXgRPCPolicyModel.log",
52 "target/classes/APEXgRPCPolicy.json"
56 new ApexCommandLineEditorMain(cliArgs);
59 final String[] apexArgs = {
63 "src/main/resources/examples/config/APEXgRPC/ApexConfig.json",
65 "target/classes/APEXgRPCPolicy.json"
69 final GrpcTestServerSim sim = new GrpcTestServerSim();
71 final Client client = ClientBuilder.newClient();
72 final ApexMain apexMain = new ApexMain(apexArgs);
74 await().atMost(5000, TimeUnit.MILLISECONDS).until(() -> apexMain.isAlive());
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();
82 Response response = client.target(getLoggedEventUrl).request("application/json").get();
84 String responseEntity = response.readEntity(String.class);
86 String expectedLoggedOutputEvent = Files
87 .readString(Paths.get("src/main/resources/examples/events/APEXgRPC/LogEvent.json")).replaceAll("\r", "");
88 assertEquals(expectedLoggedOutputEvent, responseEntity);