2 * Copyright (C) 2019 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.sli.adaptors.grpc.cds;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Mockito.doReturn;
23 import com.google.common.collect.Maps;
24 import io.grpc.inprocess.InProcessChannelBuilder;
25 import io.grpc.inprocess.InProcessServerBuilder;
26 import io.grpc.stub.StreamObserver;
27 import io.grpc.testing.GrpcCleanupRule;
28 import io.grpc.util.MutableHandlerRegistry;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
35 import java.util.concurrent.atomic.AtomicReference;
36 import org.junit.After;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.junit.runners.JUnit4;
43 import org.mockito.Mockito;
44 import org.onap.ccsdk.apps.controllerblueprints.common.api.ActionIdentifiers;
45 import org.onap.ccsdk.apps.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
46 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput;
47 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput;
48 import org.onap.ccsdk.sli.adaptors.grpc.GrpcProperties;
49 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
52 @RunWith(JUnit4.class)
53 public class BlueprintProcessingClientTest {
56 public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
58 private BlueprintProcessingClient client;
60 private final SvcLogicContext svcLogicContext = new SvcLogicContext();
61 private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
62 private final List<String> messagesDelivered = new ArrayList<>();
63 private final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
64 private final AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef = new AtomicReference<>();
67 public void setUp() throws Exception {
69 String serverName = InProcessServerBuilder.generateName();
70 grpcCleanup.register(InProcessServerBuilder.forName(serverName)
71 .fallbackHandlerRegistry(serviceRegistry).directExecutor().build().start());
73 BlueprintProcessingHandler handler = new BlueprintProcessingHandler();
76 new BlueprintProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(),
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 GrpcProperties props = Mockito.mock(GrpcProperties.class);
119 doReturn(999).when(props).getPort();
120 doReturn("localhost").when(props).getUrl();
121 new BlueprintProcessingClient(props).stop();
126 public void testSendMessageFail() throws Exception {
127 Map<String, String> input = Maps.newHashMap();
128 input.put("is_force", "true");
129 input.put("ttl", "1");
130 input.put("blueprint_name", "test");
131 input.put("blueprint_version", "1.0.0");
132 input.put("action", "test-action");
133 input.put("mode", "sync");
134 input.put("payload", "");
135 input.put("prefix", "res");
137 QueryStatus status = client.sendRequest(input, svcLogicContext);
139 Assert.assertEquals(QueryStatus.FAILURE, status);
144 public void testSendMessage() throws Exception {
145 ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
146 ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
148 ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
149 ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
151 Map<String, String> input = Maps.newHashMap();
152 input.put("is_force", "true");
153 input.put("ttl", "1");
154 input.put("blueprint_name", "test");
155 input.put("blueprint_version", "1.0.0");
156 input.put("action", "test-action");
157 input.put("mode", "sync");
158 input.put("payload", "{}");
159 input.put("prefix", "res");
161 client.sendRequest(input, svcLogicContext);
163 // request message sent and delivered for one time
164 assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
165 assertEquals(Collections.singletonList("test-action"), messagesDelivered);
167 // let server complete.
168 responseObserverRef.get().onCompleted();