1 /* 2 * Copyright 2020 The gRPC Authors 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 17 package io.grpc; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import java.util.concurrent.Executor; 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.junit.runners.JUnit4; 25 26 @RunWith(JUnit4.class) 27 public final class CompositeCallCredentialsTest { 28 private static final Metadata.Key<String> METADATA_KEY = 29 Metadata.Key.of("key", Metadata.ASCII_STRING_MARSHALLER); 30 31 private CompositeCallCredentials callCredentials; 32 private CallCredentials.RequestInfo requestInfo; 33 private Executor appExecutor; 34 private FakeMetadataApplier applier = new FakeMetadataApplier(); 35 36 @Test applyRequestMetadata_firstFails()37 public void applyRequestMetadata_firstFails() { 38 Status failure = Status.UNAVAILABLE.withDescription("expected"); 39 callCredentials = new CompositeCallCredentials( 40 new FakeCallCredentials(failure), 41 new FakeCallCredentials(createMetadata(METADATA_KEY, "value2"))); 42 callCredentials.applyRequestMetadata(requestInfo, appExecutor, applier); 43 assertThat(applier.status).isSameInstanceAs(failure); 44 } 45 46 @Test applyRequestMetadata_secondFails()47 public void applyRequestMetadata_secondFails() { 48 Status failure = Status.UNAVAILABLE.withDescription("expected"); 49 callCredentials = new CompositeCallCredentials( 50 new FakeCallCredentials(createMetadata(METADATA_KEY, "value1")), 51 new FakeCallCredentials(failure)); 52 callCredentials.applyRequestMetadata(requestInfo, appExecutor, applier); 53 assertThat(applier.status).isSameInstanceAs(failure); 54 } 55 56 @Test applyRequestMetadata_bothSucceed()57 public void applyRequestMetadata_bothSucceed() { 58 callCredentials = new CompositeCallCredentials( 59 new FakeCallCredentials(createMetadata(METADATA_KEY, "value1")), 60 new FakeCallCredentials(createMetadata(METADATA_KEY, "value2"))); 61 callCredentials.applyRequestMetadata(requestInfo, appExecutor, applier); 62 assertThat(applier.headers).isNotNull(); 63 assertThat(applier.headers.getAll(METADATA_KEY)).containsExactly("value1", "value2"); 64 } 65 createMetadata(Metadata.Key<String> key, String value)66 private static Metadata createMetadata(Metadata.Key<String> key, String value) { 67 Metadata metadata = new Metadata(); 68 metadata.put(key, value); 69 return metadata; 70 } 71 72 private static class FakeMetadataApplier extends CallCredentials.MetadataApplier { 73 private Metadata headers; 74 private Status status; 75 apply(Metadata headers)76 @Override public void apply(Metadata headers) { 77 assertThat(this.headers).isNull(); 78 assertThat(this.status).isNull(); 79 this.headers = headers; 80 } 81 fail(Status status)82 @Override public void fail(Status status) { 83 assertThat(this.headers).isNull(); 84 assertThat(this.status).isNull(); 85 this.status = status; 86 } 87 } 88 89 private static class FakeCallCredentials extends CallCredentials { 90 private final Metadata headers; 91 private final Status status; 92 FakeCallCredentials(Metadata headers)93 public FakeCallCredentials(Metadata headers) { 94 this.headers = headers; 95 this.status = null; 96 } 97 FakeCallCredentials(Status status)98 public FakeCallCredentials(Status status) { 99 this.headers = null; 100 this.status = status; 101 } 102 applyRequestMetadata( CallCredentials.RequestInfo requestInfo, Executor appExecutor, CallCredentials.MetadataApplier applier)103 @Override public void applyRequestMetadata( 104 CallCredentials.RequestInfo requestInfo, Executor appExecutor, 105 CallCredentials.MetadataApplier applier) { 106 if (headers != null) { 107 applier.apply(headers); 108 } else { 109 applier.fail(status); 110 } 111 } 112 } 113 } 114