2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved.
7 * Modifications Copyright (C) 2023 Nordix Foundation.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.simulators;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
28 import com.google.protobuf.TextFormat.ParseException;
29 import com.google.protobuf.util.JsonFormat;
30 import io.grpc.ManagedChannel;
31 import io.grpc.netty.NettyChannelBuilder;
32 import io.grpc.stub.StreamObserver;
33 import java.io.IOException;
34 import java.nio.charset.StandardCharsets;
35 import java.util.Objects;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.concurrent.CountDownLatch;
38 import java.util.concurrent.TimeUnit;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc;
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub;
45 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
46 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput.Builder;
47 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.network.NetworkUtil;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
53 public class CdsSimulatorTest {
54 private static final StandardCoder coder = new StandardCoder();
56 private CdsSimulator sim;
59 * Starts the simulator, allocating a unique port for each test so we don't have to
60 * wait for the prior server to shut down.
63 public void setUp() throws Exception {
64 int port = NetworkUtil.allocPort();
65 sim = new CdsSimulator(Util.LOCALHOST, port);
70 public void tearDown() {
75 public void test() throws Exception {
76 String reqstr = IOUtils.toString(Objects.requireNonNull(getClass().getResource("cds/cds.request.json")),
77 StandardCharsets.UTF_8);
78 Builder builder = ExecutionServiceInput.newBuilder();
79 JsonFormat.parser().ignoringUnknownFields().merge(reqstr, builder);
80 ExecutionServiceInput request = builder.build();
81 ManagedChannel channel = NettyChannelBuilder.forAddress(Util.LOCALHOST, sim.getPort()).usePlaintext().build();
84 final CompletableFuture<ExecutionServiceOutput> future = new CompletableFuture<>();
85 final CountDownLatch completed = new CountDownLatch(1);
87 BluePrintProcessingServiceStub asyncStub = BluePrintProcessingServiceGrpc.newStub(channel);
89 StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<>() {
91 public void onNext(ExecutionServiceOutput output) {
92 future.complete(output);
96 public void onError(Throwable throwable) {
97 future.completeExceptionally(throwable);
101 public void onCompleted() {
102 completed.countDown();
106 StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
108 // publish the message
109 requestObserver.onNext(request);
111 // indicate that the request is done
112 requestObserver.onCompleted();
114 } catch (RuntimeException e) {
115 requestObserver.onError(e);
118 // wait for it to complete
119 assertTrue(completed.await(5, TimeUnit.SECONDS));
121 ExecutionServiceOutput result = future.get();
122 assertEquals(200, result.getStatus().getCode());
130 public void testGetResponse() throws IOException, CoderException, ParseException {
131 CdsSimulator cdsSimulator = new CdsSimulator(Util.LOCALHOST, sim.getPort());
132 String reqstr = ResourceUtils.getResourceAsString(
133 "org/onap/policy/simulators/cds/cds.request.json");
134 String responseqstr = ResourceUtils.getResourceAsString(
135 "org/onap/policy/simulators/cds/pm_control-create-subscription.json");
136 ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class);
137 org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder esoBuilder =
138 ExecutionServiceOutput.newBuilder();
139 JsonFormat.parser().ignoringUnknownFields().merge(responseqstr, esoBuilder);
140 assertEquals(esoBuilder.toString(), cdsSimulator.getResponse(request, 0).toString());