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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.examples.grpc;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.Assert.assertEquals;
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;
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.
43 public class TestApexGrpcExample {
45 public void testGrpcExample() throws Exception {
47 final String[] cliArgs = new String[] {
49 "src/main/resources/policy/APEXgRPCPolicy.apex",
51 "target/APEXgRPCPolicyModel.log",
53 "src/main/resources/examples/config/APEXgRPC/ApexConfig.json",
55 "src/main/resources/tosca/ToscaTemplate.json",
57 "target/classes/APEXgRPCPolicy.json"
61 new ApexCliToscaEditorMain(cliArgs);
64 final String[] apexArgs = {
68 "target/classes/APEXgRPCPolicy.json"
72 final GrpcTestServerSim sim = new GrpcTestServerSim();
74 final Client client = ClientBuilder.newClient();
75 final ApexMain apexMain = new ApexMain(apexArgs);
77 await().atMost(5000, TimeUnit.MILLISECONDS).until(() -> apexMain.isAlive());
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();
85 Response response = client.target(getLoggedEventUrl).request("application/json").get();
87 String responseEntity = response.readEntity(String.class);
89 String expectedLoggedOutputEvent = Files
90 .readString(Paths.get("src/main/resources/examples/events/APEXgRPC/LogEvent.json")).replaceAll("\r", "");
91 assertEquals(expectedLoggedOutputEvent, responseEntity);