Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / test / java / org / onap / so / client / cds / CDSProcessingClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.cds;
22
23
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.*;
26 import io.grpc.inprocess.InProcessChannelBuilder;
27 import io.grpc.inprocess.InProcessServerBuilder;
28 import io.grpc.stub.StreamObserver;
29 import io.grpc.testing.GrpcCleanupRule;
30 import io.grpc.util.MutableHandlerRegistry;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 import java.util.concurrent.atomic.AtomicReference;
37 import org.junit.After;
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.Mock;
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
49 @RunWith(JUnit4.class)
50 public class CDSProcessingClientTest {
51
52     @Rule
53     public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
54
55     @Mock
56     private CDSProcessingListener listener = spy(new TestCDSProcessingListener());
57
58     private CDSProcessingHandler handler;
59     private CDSProcessingClient client;
60
61
62     private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry();
63     private final List<String> messagesDelivered = new ArrayList<>();
64     private final CountDownLatch allRequestsDelivered = new CountDownLatch(1);
65     private final AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef = new AtomicReference<>();
66
67     @Before
68     public void setUp() throws Exception {
69         String serverName = InProcessServerBuilder.generateName();
70         grpcCleanup.register(InProcessServerBuilder.forName(serverName).fallbackHandlerRegistry(serviceRegistry)
71                 .directExecutor().build().start());
72
73         handler = new CDSProcessingHandler(listener);
74
75         client = new CDSProcessingClient(InProcessChannelBuilder.forName(serverName).directExecutor().build(), handler);
76
77         final BluePrintProcessingServiceImplBase routeChatImpl = new BluePrintProcessingServiceImplBase() {
78             @Override
79             public StreamObserver<ExecutionServiceInput> process(
80                     StreamObserver<ExecutionServiceOutput> responseObserver) {
81
82                 responseObserverRef.set(responseObserver);
83
84                 StreamObserver<ExecutionServiceInput> requestObserver = new StreamObserver<ExecutionServiceInput>() {
85                     @Override
86                     public void onNext(ExecutionServiceInput message) {
87                         messagesDelivered.add(message.getActionIdentifiers().getActionName());
88                     }
89
90                     @Override
91                     public void onError(Throwable t) {
92
93                     }
94
95                     @Override
96                     public void onCompleted() {
97                         allRequestsDelivered.countDown();
98                     }
99                 };
100
101                 return requestObserver;
102             }
103         };
104
105         serviceRegistry.addService(routeChatImpl);
106     }
107
108     @After
109     public void tearDown() {
110         client.close();
111     }
112
113     @Test
114     public void testClientCst() {
115         new CDSProcessingClient(listener);
116     }
117
118
119     @Test
120     public void testSendMessageFail() throws Exception {
121
122         ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder()
123                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
124
125         CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
126
127         responseObserverRef.get().onError(new Throwable("fail test"));
128         verify(listener).onError(any(Throwable.class));
129
130         assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
131     }
132
133     @Test
134     public void testSendMessage() throws Exception {
135
136         ExecutionServiceInput fakeRequest1 = ExecutionServiceInput.newBuilder()
137                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("request1").build()).build();
138
139         ExecutionServiceOutput fakeResponse1 = ExecutionServiceOutput.newBuilder()
140                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("response1").build()).build();
141
142         ExecutionServiceOutput fakeResponse2 = ExecutionServiceOutput.newBuilder()
143                 .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("response2").build()).build();
144
145         CountDownLatch finishLatch = client.sendRequest(fakeRequest1);
146
147         // request message sent and delivered for one time
148         assertTrue(allRequestsDelivered.await(1, TimeUnit.SECONDS));
149         assertEquals(Collections.singletonList("request1"), messagesDelivered);
150
151         // Let the server send out two simple response messages
152         // and verify that the client receives them.
153         responseObserverRef.get().onNext(fakeResponse1);
154         verify(listener).onMessage(fakeResponse1);
155         responseObserverRef.get().onNext(fakeResponse2);
156         verify(listener).onMessage(fakeResponse2);
157
158         // let server complete.
159         responseObserverRef.get().onCompleted();
160
161         assertTrue(finishLatch.await(1, TimeUnit.SECONDS));
162     }
163
164 }