5f82b7e94128fce6e4c8a791a6bd858f85429fb4
[policy/models.git] / models-interactions / model-simulators / src / test / java / org / onap / policy / simulators / CdsSimulatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.simulators;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
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;
44
45 public class CdsSimulatorTest {
46     private static final StandardCoder coder = new StandardCoder();
47
48     private CdsSimulator sim;
49
50     @Before
51     public void setUp() throws Exception {
52         sim = Util.buildCdsSim();
53     }
54
55     @After
56     public void tearDown() {
57         sim.stop();
58     }
59
60     @Test
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);
64
65         ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", sim.getPort())
66             .nameResolverFactory(new DnsNameResolverProvider())
67             .loadBalancerFactory(new PickFirstLoadBalancerProvider()).usePlaintext().build();
68
69         try {
70             final CompletableFuture<ExecutionServiceOutput> future = new CompletableFuture<>();
71             final CountDownLatch completed = new CountDownLatch(1);
72
73             BluePrintProcessingServiceStub asyncStub = BluePrintProcessingServiceGrpc.newStub(channel);
74
75             StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<ExecutionServiceOutput>() {
76                 @Override
77                 public void onNext(ExecutionServiceOutput output) {
78                     future.complete(output);
79                 }
80
81                 @Override
82                 public void onError(Throwable throwable) {
83                     future.completeExceptionally(throwable);
84                 }
85
86                 @Override
87                 public void onCompleted() {
88                     completed.countDown();
89                 }
90             };
91
92             StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
93             try {
94                 // publish the message
95                 requestObserver.onNext(request);
96
97                 // indicate that the request is done
98                 requestObserver.onCompleted();
99
100             } catch (RuntimeException e) {
101                 requestObserver.onError(e);
102             }
103
104             // wait for it to complete
105             assertTrue(completed.await(5, TimeUnit.SECONDS));
106
107             ExecutionServiceOutput result = future.get();
108             assertEquals(200, result.getStatus().getCode());
109
110         } finally {
111             channel.shutdown();
112         }
113     }
114 }