Merge "Enhance gRPC Simulator:"
[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  * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import io.grpc.ManagedChannel;
28 import io.grpc.internal.DnsNameResolverProvider;
29 import io.grpc.internal.PickFirstLoadBalancerProvider;
30 import io.grpc.netty.NettyChannelBuilder;
31 import io.grpc.stub.StreamObserver;
32 import java.io.IOException;
33 import java.nio.charset.StandardCharsets;
34 import java.util.concurrent.CompletableFuture;
35 import java.util.concurrent.CountDownLatch;
36 import java.util.concurrent.TimeUnit;
37 import org.apache.commons.io.IOUtils;
38 import org.json.simple.parser.ParseException;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc;
43 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub;
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
45 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.common.utils.resources.ResourceUtils;
49
50 public class CdsSimulatorTest {
51     private static final StandardCoder coder = new StandardCoder();
52
53     private CdsSimulator sim;
54
55     @Before
56     public void setUp() throws Exception {
57         sim = Util.buildCdsSim();
58     }
59
60     @After
61     public void tearDown() {
62         sim.stop();
63     }
64
65     @Test
66     public void test() throws Exception {
67         String reqstr = IOUtils.toString(getClass().getResource("cds/cds.request.json"), StandardCharsets.UTF_8);
68         ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class);
69         ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", sim.getPort())
70             .nameResolverFactory(new DnsNameResolverProvider())
71             .loadBalancerFactory(new PickFirstLoadBalancerProvider()).usePlaintext().build();
72
73         try {
74             final CompletableFuture<ExecutionServiceOutput> future = new CompletableFuture<>();
75             final CountDownLatch completed = new CountDownLatch(1);
76
77             BluePrintProcessingServiceStub asyncStub = BluePrintProcessingServiceGrpc.newStub(channel);
78
79             StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<ExecutionServiceOutput>() {
80                 @Override
81                 public void onNext(ExecutionServiceOutput output) {
82                     future.complete(output);
83                 }
84
85                 @Override
86                 public void onError(Throwable throwable) {
87                     future.completeExceptionally(throwable);
88                 }
89
90                 @Override
91                 public void onCompleted() {
92                     completed.countDown();
93                 }
94             };
95
96             StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
97             try {
98                 // publish the message
99                 requestObserver.onNext(request);
100
101                 // indicate that the request is done
102                 requestObserver.onCompleted();
103
104             } catch (RuntimeException e) {
105                 requestObserver.onError(e);
106             }
107
108             // wait for it to complete
109             assertTrue(completed.await(5, TimeUnit.SECONDS));
110
111             ExecutionServiceOutput result = future.get();
112             assertEquals(200, result.getStatus().getCode());
113
114         } finally {
115             channel.shutdown();
116         }
117     }
118
119     @Test
120     public void testGetResponseString() throws IOException, CoderException, ParseException {
121         CdsSimulator cdsSimulator = new CdsSimulator("localhost", sim.getPort());
122         String reqstr = ResourceUtils.getResourceAsString(
123             "org/onap/policy/simulators/cds/cds.request.json");
124         String responseqstr = ResourceUtils.getResourceAsString(
125             "org/onap/policy/simulators/cds/pm_control-create-subscription.json");
126         ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class);
127         assertEquals(responseqstr, cdsSimulator.getResponseString(request, 0));
128     }
129 }