2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 Bell Canada.
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.so.client.cds;
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
27 import io.grpc.inprocess.InProcessChannelBuilder;
28 import io.grpc.inprocess.InProcessServerBuilder;
29 import io.grpc.stub.StreamObserver;
30 import io.grpc.testing.GrpcCleanupRule;
31 import io.grpc.util.MutableHandlerRegistry;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.concurrent.CountDownLatch;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.atomic.AtomicReference;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.JUnit4;
44 import org.mockito.Mock;
45 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
46 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
47 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
48 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
50 @RunWith(JUnit4.class)
51 public class CDSProcessingClientTest {
54 public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
57 private CDSProcessingListener listener = spy(new TestCDSProcessingListener());
59 private CDSProcessingHandler handler;
60 private CDSProcessingClient client;
63 private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
64 private final List<String> messagesDelivered = new ArrayList<>();
65 private final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
66 private final AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef = new AtomicReference<>();
69 public void setUp() throws Exception {
70 String serverName = InProcessServerBuilder.generateName();
71 grpcCleanup.register(InProcessServerBuilder.forName(serverName)
72 .fallbackHandlerRegistry(serviceRegistry).directExecutor().build().start());
74 handler = new CDSProcessingHandler(listener);
77 new CDSProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(), handler);
79 final BluePrintProcessingServiceImplBase routeChatImpl =
80 new BluePrintProcessingServiceImplBase() {
82 public StreamObserver<ExecutionServiceInput> process(
83 StreamObserver<ExecutionServiceOutput> responseObserver) {
85 responseObserverRef.set(responseObserver);
87 StreamObserver<ExecutionServiceInput> requestObserver = new StreamObserver<ExecutionServiceInput>() {
89 public void onNext(ExecutionServiceInput message) {
90 messagesDelivered.add(message.getActionIdentifiers().getActionName());
94 public void onError(Throwable t) {
99 public void onCompleted() {
100 allRequestsDelivered.countDown();
104 return requestObserver;
108 serviceRegistry.addService(routeChatImpl);
112 public void tearDown() {
117 public void testClientCst() {
118 new CDSProcessingClient(listener);
123 public void testSendMessageFail() throws Exception {
125 ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder().setActionIdentifiers(
126 ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
128 CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
130 responseObserverRef.get().onError(new Throwable("fail test"));
131 verify(listener).onError(any(Throwable.class));
133 assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
137 public void testSendMessage() throws Exception {
139 ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder().setActionIdentifiers(
140 ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
142 ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
143 ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
145 ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
146 ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
148 CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
150 // request message sent and delivered for one time
151 assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
152 assertEquals(Collections.singletonList("request1"), messagesDelivered);
154 // Let the server send out two simple response messages
155 // and verify that the client receives them.
156 responseObserverRef.get().onNext(fakeResponse1);
157 verify(listener).onMessage(fakeResponse1);
158 responseObserverRef.get().onNext(fakeResponse2);
159 verify(listener).onMessage(fakeResponse2);
161 // let server complete.
162 responseObserverRef.get().onCompleted();
164 assertTrue(finishLatch.await(1, TimeUnit.SECONDS));