ea012d3d998fbd03120c72a3305d929870c922c6
[ccsdk/sli/adaptors.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.sli.adaptors.grpc.cds;
17
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Mockito.doReturn;
22
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;
32 import java.util.Map;
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.cds.controllerblueprints.common.api.ActionIdentifiers;
45 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
46 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
47 import org.onap.ccsdk.cds.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;
51
52 @RunWith(JUnit4.class)
53 public class BlueprintProcessingClientTest {
54
55     @Rule
56     public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
57
58     private BlueprintProcessingClient client;
59
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<>();
65
66     @Before
67     public void setUp() throws Exception {
68
69         String serverName = InProcessServerBuilder.generateName();
70         grpcCleanup.register(InProcessServerBuilder.forName(serverName)
71             .fallbackHandlerRegistry(serviceRegistry).directExecutor().build().start());
72
73         BlueprintProcessingHandler handler = new BlueprintProcessingHandler();
74
75         client =
76             new BlueprintProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(),
77                 handler);
78
79         final BluePrintProcessingServiceImplBase routeChatImpl =
80             new BluePrintProcessingServiceImplBase() {
81                 @Override
82                 public StreamObserver<ExecutionServiceInput> process(
83                     StreamObserver<ExecutionServiceOutput> responseObserver) {
84
85                     responseObserverRef.set(responseObserver);
86
87                     StreamObserver<ExecutionServiceInput> requestObserver = new StreamObserver<ExecutionServiceInput>() {
88                         @Override
89                         public void onNext(ExecutionServiceInput message) {
90                             messagesDelivered.add(message.getActionIdentifiers().getActionName());
91                         }
92
93                         @Override
94                         public void onError(Throwable t) {
95
96                         }
97
98                         @Override
99                         public void onCompleted() {
100                             allRequestsDelivered.countDown();
101                         }
102                     };
103
104                     return requestObserver;
105                 }
106             };
107
108         serviceRegistry.addService(routeChatImpl);
109     }
110
111     @After
112     public void tearDown() {
113         client.stop();
114     }
115
116     @Test
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();
122     }
123
124
125     @Test
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");
136
137         QueryStatus status = client.sendRequest(input, svcLogicContext);
138
139         Assert.assertEquals(QueryStatus.FAILURE, status);
140
141     }
142
143     @Test
144     public void testSendMessage() throws Exception {
145         ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
146             ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
147
148         ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder().setActionIdentifiers(
149             ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
150
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");
160
161         client.sendRequest(input, svcLogicContext);
162
163         // request message sent and delivered for one time
164         assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
165         assertEquals(Collections.singletonList("test-action"), messagesDelivered);
166
167         // let server complete.
168         responseObserverRef.get().onCompleted();
169     }
170
171 }