2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.simulators;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import io.grpc.ManagedChannel;
27 import io.grpc.internal.DnsNameResolverProvider;
28 import io.grpc.internal.PickFirstLoadBalancerProvider;
29 import io.grpc.netty.NettyChannelBuilder;
30 import io.grpc.stub.StreamObserver;
31 import java.nio.charset.StandardCharsets;
32 import java.util.concurrent.CompletableFuture;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
35 import org.apache.commons.io.IOUtils;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc;
40 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub;
41 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
42 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
43 import org.onap.policy.common.utils.coder.StandardCoder;
45 public class CdsSimulatorTest {
46 private static final StandardCoder coder = new StandardCoder();
48 private CdsSimulator sim;
51 public void setUp() throws Exception {
52 sim = Util.buildCdsSim();
56 public void tearDown() {
61 public void test() throws Exception {
62 String reqstr = IOUtils.toString(getClass().getResource("cds/cds.request.json"), StandardCharsets.UTF_8);
63 ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class);
65 ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", sim.getPort())
66 .nameResolverFactory(new DnsNameResolverProvider())
67 .loadBalancerFactory(new PickFirstLoadBalancerProvider()).usePlaintext().build();
70 final CompletableFuture<ExecutionServiceOutput> future = new CompletableFuture<>();
71 final CountDownLatch completed = new CountDownLatch(1);
73 BluePrintProcessingServiceStub asyncStub = BluePrintProcessingServiceGrpc.newStub(channel);
75 StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<ExecutionServiceOutput>() {
77 public void onNext(ExecutionServiceOutput output) {
78 future.complete(output);
82 public void onError(Throwable throwable) {
83 future.completeExceptionally(throwable);
87 public void onCompleted() {
88 completed.countDown();
92 StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
94 // publish the message
95 requestObserver.onNext(request);
97 // indicate that the request is done
98 requestObserver.onCompleted();
100 } catch (RuntimeException e) {
101 requestObserver.onError(e);
104 // wait for it to complete
105 assertTrue(completed.await(5, TimeUnit.SECONDS));
107 ExecutionServiceOutput result = future.get();
108 assertEquals(200, result.getStatus().getCode());