Upgrade to cds 1.1.5-SNAPSHOT
[policy/models.git] / models-interactions / model-impl / cds / src / test / java / org / onap / policy / cds / client / BasicAuthClientHeaderInterceptorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Bell Canada.
4  * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.cds.client;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.AdditionalAnswers.delegatesTo;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26
27 import io.grpc.ClientInterceptors;
28 import io.grpc.ManagedChannel;
29 import io.grpc.Metadata;
30 import io.grpc.Metadata.Key;
31 import io.grpc.ServerCall;
32 import io.grpc.ServerCall.Listener;
33 import io.grpc.ServerCallHandler;
34 import io.grpc.ServerInterceptor;
35 import io.grpc.ServerInterceptors;
36 import io.grpc.inprocess.InProcessChannelBuilder;
37 import io.grpc.inprocess.InProcessServerBuilder;
38 import io.grpc.stub.StreamObserver;
39 import io.grpc.testing.GrpcCleanupRule;
40 import java.io.IOException;
41 import java.nio.charset.StandardCharsets;
42 import java.util.Base64;
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.mockito.ArgumentCaptor;
47 import org.mockito.ArgumentMatchers;
48 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc;
49 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase;
50 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub;
51 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput;
52 import org.onap.policy.cds.properties.CdsServerProperties;
53
54 public class BasicAuthClientHeaderInterceptorTest {
55
56     // Generate a unique in-process server name.
57     private static final String SERVER_NAME = InProcessServerBuilder.generateName();
58     private static final String CREDS = "test";
59
60     // Manages automatic graceful shutdown for the registered server and client channels at the end of test.
61     @Rule
62     public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
63
64     private final ServerInterceptor mockCdsGrpcServerInterceptor = mock(ServerInterceptor.class,
65         delegatesTo(new TestServerInterceptor()));
66
67     private final CdsServerProperties props = new CdsServerProperties();
68
69     private ManagedChannel channel;
70
71     /**
72      * Setup the test.
73      *
74      * @throws IOException on failure to register the test grpc server for graceful shutdown
75      */
76     @Before
77     public void setUp() throws IOException {
78         // Setup the CDS properties
79         props.setHost(SERVER_NAME);
80         props.setPort(2000);
81         props.setUsername(CREDS);
82         props.setPassword(CREDS);
83         props.setTimeout(60);
84
85         // Implement the test gRPC server
86         BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() {};
87
88         // Create a server, add service, start, and register for automatic graceful shutdown.
89         grpcCleanup.register(InProcessServerBuilder.forName(SERVER_NAME).directExecutor()
90             .addService(ServerInterceptors.intercept(testCdsBlueprintServerImpl, mockCdsGrpcServerInterceptor)).build()
91             .start());
92
93         // Create a client channel and register for automatic graceful shutdown
94         channel = grpcCleanup.register(InProcessChannelBuilder.forName(SERVER_NAME).directExecutor().build());
95     }
96
97     @Test
98     public void testIfBasicAuthHeaderIsDeliveredToCdsServer() {
99         BluePrintProcessingServiceStub bpProcessingSvcStub = BluePrintProcessingServiceGrpc
100             .newStub(ClientInterceptors.intercept(channel, new BasicAuthClientHeaderInterceptor(props)));
101         ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
102         bpProcessingSvcStub.process(new StreamObserver<ExecutionServiceOutput>() {
103             @Override
104             public void onNext(final ExecutionServiceOutput executionServiceOutput) {
105                 // Test purpose only
106             }
107
108             @Override
109             public void onError(final Throwable throwable) {
110                 // Test purpose only
111             }
112
113             @Override
114             public void onCompleted() {
115                 // Test purpose only
116             }
117         });
118         verify(mockCdsGrpcServerInterceptor).interceptCall(ArgumentMatchers.any(), metadataCaptor.capture(),
119             ArgumentMatchers.any());
120
121         Key<String> authHeader = Key
122             .of(BasicAuthClientHeaderInterceptor.BASIC_AUTH_HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER);
123         String expectedBaseAuth = "Basic " + Base64.getEncoder().encodeToString(String.format("%s:%s", CREDS, CREDS)
124             .getBytes(StandardCharsets.UTF_8));
125         assertEquals(expectedBaseAuth, metadataCaptor.getValue().get(authHeader));
126     }
127
128     private static class TestServerInterceptor implements ServerInterceptor {
129
130         @Override
131         public <Q, P> Listener<Q> interceptCall(final ServerCall<Q, P> serverCall,
132                         final Metadata metadata, final ServerCallHandler<Q, P> serverCallHandler) {
133             return serverCallHandler.startCall(serverCall, metadata);
134         }
135     }
136 }
137
138