Fix some dependencies flagged in scans
[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-2021 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 com.google.protobuf.TextFormat.ParseException;
28 import com.google.protobuf.util.JsonFormat;
29 import io.grpc.ManagedChannel;
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.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc;
42 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub;
43 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput.Builder;
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.network.NetworkUtil;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50
51 public class CdsSimulatorTest {
52     private static final StandardCoder coder = new StandardCoder();
53
54     private CdsSimulator sim;
55
56     /**
57      * Starts the simulator, allocating a unique port for each test so we don't have to
58      * wait for the prior server to shut down.
59      */
60     @Before
61     public void setUp() throws Exception {
62         int port = NetworkUtil.allocPort();
63         sim = new CdsSimulator(Util.LOCALHOST, port);
64         sim.start();
65     }
66
67     @After
68     public void tearDown() {
69         sim.stop();
70     }
71
72     @Test
73     public void test() throws Exception {
74         String reqstr = IOUtils.toString(getClass().getResource("cds/cds.request.json"), StandardCharsets.UTF_8);
75         Builder builder = ExecutionServiceInput.newBuilder();
76         JsonFormat.parser().ignoringUnknownFields().merge(reqstr, builder);
77         ExecutionServiceInput request = builder.build();
78         ManagedChannel channel = NettyChannelBuilder.forAddress(Util.LOCALHOST, sim.getPort()).usePlaintext().build();
79
80         try {
81             final CompletableFuture<ExecutionServiceOutput> future = new CompletableFuture<>();
82             final CountDownLatch completed = new CountDownLatch(1);
83
84             BluePrintProcessingServiceStub asyncStub = BluePrintProcessingServiceGrpc.newStub(channel);
85
86             StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<ExecutionServiceOutput>() {
87                 @Override
88                 public void onNext(ExecutionServiceOutput output) {
89                     future.complete(output);
90                 }
91
92                 @Override
93                 public void onError(Throwable throwable) {
94                     future.completeExceptionally(throwable);
95                 }
96
97                 @Override
98                 public void onCompleted() {
99                     completed.countDown();
100                 }
101             };
102
103             StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
104             try {
105                 // publish the message
106                 requestObserver.onNext(request);
107
108                 // indicate that the request is done
109                 requestObserver.onCompleted();
110
111             } catch (RuntimeException e) {
112                 requestObserver.onError(e);
113             }
114
115             // wait for it to complete
116             assertTrue(completed.await(5, TimeUnit.SECONDS));
117
118             ExecutionServiceOutput result = future.get();
119             assertEquals(200, result.getStatus().getCode());
120
121         } finally {
122             channel.shutdown();
123         }
124     }
125
126     @Test
127     public void testGetResponse() throws IOException, CoderException, ParseException {
128         CdsSimulator cdsSimulator = new CdsSimulator(Util.LOCALHOST, sim.getPort());
129         String reqstr = ResourceUtils.getResourceAsString(
130             "org/onap/policy/simulators/cds/cds.request.json");
131         String responseqstr = ResourceUtils.getResourceAsString(
132             "org/onap/policy/simulators/cds/pm_control-create-subscription.json");
133         ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class);
134         org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder esoBuilder =
135             ExecutionServiceOutput.newBuilder();
136         JsonFormat.parser().ignoringUnknownFields().merge(responseqstr, esoBuilder);
137         assertEquals(esoBuilder.toString(), cdsSimulator.getResponse(request, 0).toString());
138     }
139 }