|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2024 The Dapr Authors | 
|  | 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | + * you may not use this file except in compliance with the License. | 
|  | 5 | + * You may obtain a copy of the License at | 
|  | 6 | + *     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 7 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 8 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 10 | + * See the License for the specific language governing permissions and | 
|  | 11 | +limitations under the License. | 
|  | 12 | +*/ | 
|  | 13 | + | 
|  | 14 | +package io.dapr.spring.boot.autoconfigure.client; | 
|  | 15 | + | 
|  | 16 | +import io.dapr.client.DaprClient; | 
|  | 17 | +import io.dapr.client.DaprClientBuilder; | 
|  | 18 | +import io.dapr.config.Properties; | 
|  | 19 | +import org.junit.jupiter.api.BeforeEach; | 
|  | 20 | +import org.junit.jupiter.api.DisplayName; | 
|  | 21 | +import org.junit.jupiter.api.Test; | 
|  | 22 | +import org.junit.jupiter.api.extension.ExtendWith; | 
|  | 23 | +import org.mockito.Mock; | 
|  | 24 | +import org.mockito.junit.jupiter.MockitoExtension; | 
|  | 25 | +import org.springframework.boot.autoconfigure.AutoConfigurations; | 
|  | 26 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; | 
|  | 27 | + | 
|  | 28 | +import java.util.Map; | 
|  | 29 | + | 
|  | 30 | +import static org.assertj.core.api.Assertions.assertThat; | 
|  | 31 | +import static org.mockito.Mockito.verify; | 
|  | 32 | +import static org.mockito.Mockito.when; | 
|  | 33 | + | 
|  | 34 | +/** | 
|  | 35 | + * Unit tests for {@link DaprClientAutoConfiguration}. | 
|  | 36 | + */ | 
|  | 37 | +@ExtendWith(MockitoExtension.class) | 
|  | 38 | +class DaprClientAutoConfigurationTest { | 
|  | 39 | + | 
|  | 40 | +  private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | 
|  | 41 | +      .withConfiguration(AutoConfigurations.of(DaprClientAutoConfiguration.class)); | 
|  | 42 | + | 
|  | 43 | +  @Mock | 
|  | 44 | +  private DaprConnectionDetails connectionDetails; | 
|  | 45 | + | 
|  | 46 | +  @Mock | 
|  | 47 | +  private DaprClientBuilder builder; | 
|  | 48 | + | 
|  | 49 | +  private DaprClientAutoConfiguration configuration; | 
|  | 50 | + | 
|  | 51 | +  @Test | 
|  | 52 | +  void daprClientBuilder() { | 
|  | 53 | +    contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClientBuilder.class)); | 
|  | 54 | +  } | 
|  | 55 | + | 
|  | 56 | +  @Test | 
|  | 57 | +  void daprClient() { | 
|  | 58 | +    contextRunner.run(context -> assertThat(context).hasSingleBean(DaprClient.class)); | 
|  | 59 | +  } | 
|  | 60 | + | 
|  | 61 | +  @BeforeEach | 
|  | 62 | +  void setUp() { | 
|  | 63 | +    configuration = new TestDaprClientAutoConfiguration(builder); | 
|  | 64 | +  } | 
|  | 65 | + | 
|  | 66 | +  @Test | 
|  | 67 | +  @DisplayName("Should override HTTP endpoint if it exists") | 
|  | 68 | +  void shouldOverrideHttpEndpointIfExists() { | 
|  | 69 | +    String httpEndpoint = "http://localhost:3500"; | 
|  | 70 | + | 
|  | 71 | +    when(connectionDetails.getHttpEndpoint()).thenReturn(httpEndpoint); | 
|  | 72 | + | 
|  | 73 | +    configuration.daprClientBuilder(connectionDetails); | 
|  | 74 | + | 
|  | 75 | +    verify(builder).withPropertyOverride(Properties.HTTP_ENDPOINT, httpEndpoint); | 
|  | 76 | +  } | 
|  | 77 | + | 
|  | 78 | +  @Test | 
|  | 79 | +  @DisplayName("Should override GRPC endpoint if it exists") | 
|  | 80 | +  void shouldOverrideGrpcEndpointIfExists() { | 
|  | 81 | +    String grpcEndpoint = "grpc://localhost:5001"; | 
|  | 82 | + | 
|  | 83 | +    when(connectionDetails.getGrpcEndpoint()).thenReturn(grpcEndpoint); | 
|  | 84 | + | 
|  | 85 | +    configuration.daprClientBuilder(connectionDetails); | 
|  | 86 | + | 
|  | 87 | +    verify(builder).withPropertyOverride(Properties.GRPC_ENDPOINT, grpcEndpoint); | 
|  | 88 | +  } | 
|  | 89 | + | 
|  | 90 | +  @Test | 
|  | 91 | +  @DisplayName("Should override HTTP port if it exists") | 
|  | 92 | +  void shouldOverrideHttpPortIfExists() { | 
|  | 93 | +    Integer httpPort = 3600; | 
|  | 94 | + | 
|  | 95 | +    when(connectionDetails.getHttpPort()).thenReturn(httpPort); | 
|  | 96 | + | 
|  | 97 | +    configuration.daprClientBuilder(connectionDetails); | 
|  | 98 | + | 
|  | 99 | +    verify(builder).withPropertyOverride(Properties.HTTP_PORT, String.valueOf(httpPort)); | 
|  | 100 | +  } | 
|  | 101 | + | 
|  | 102 | +  @Test | 
|  | 103 | +  @DisplayName("Should override GRPC port if it exists") | 
|  | 104 | +  void shouldOverrideGrpcPortIfExists() { | 
|  | 105 | +    Integer grpcPort = 6001; | 
|  | 106 | + | 
|  | 107 | +    when(connectionDetails.getGrpcPort()).thenReturn(grpcPort); | 
|  | 108 | + | 
|  | 109 | +    configuration.daprClientBuilder(connectionDetails); | 
|  | 110 | + | 
|  | 111 | +    verify(builder).withPropertyOverride(Properties.GRPC_PORT, String.valueOf(grpcPort)); | 
|  | 112 | +  } | 
|  | 113 | + | 
|  | 114 | +  @Test | 
|  | 115 | +  @DisplayName("Should override HTTP endpoint in properties if it exists") | 
|  | 116 | +  void shouldOverrideHttpEndpointInPropertiesIfExists() { | 
|  | 117 | +    String httpEndpoint = "http://localhost:3500"; | 
|  | 118 | + | 
|  | 119 | +    when(connectionDetails.getHttpEndpoint()).thenReturn(httpEndpoint); | 
|  | 120 | + | 
|  | 121 | +    Properties reuslt = configuration.createPropertiesFromConnectionDetails(connectionDetails); | 
|  | 122 | + | 
|  | 123 | +    assertThat(reuslt.getValue(Properties.HTTP_ENDPOINT)).isEqualTo(httpEndpoint); | 
|  | 124 | +  } | 
|  | 125 | + | 
|  | 126 | +  @Test | 
|  | 127 | +  @DisplayName("Should override GRPC endpoint in properties if it exists") | 
|  | 128 | +  void shouldOverrideGrpcEndpointPropertiesIfExists() { | 
|  | 129 | +    String grpcEndpoint = "grpc://localhost:3500"; | 
|  | 130 | + | 
|  | 131 | +    when(connectionDetails.getGrpcEndpoint()).thenReturn(grpcEndpoint); | 
|  | 132 | + | 
|  | 133 | +    Properties result = configuration.createPropertiesFromConnectionDetails(connectionDetails); | 
|  | 134 | + | 
|  | 135 | +    assertThat(result.getValue(Properties.GRPC_ENDPOINT)).isEqualTo(grpcEndpoint); | 
|  | 136 | +  } | 
|  | 137 | + | 
|  | 138 | +  @Test | 
|  | 139 | +  @DisplayName("Should override HTTP port in properties if it exists") | 
|  | 140 | +  void shouldOverrideHttpPortPropertiesIfExists() { | 
|  | 141 | +    Integer httpPort = 3600; | 
|  | 142 | + | 
|  | 143 | +    when(connectionDetails.getHttpPort()).thenReturn(httpPort); | 
|  | 144 | + | 
|  | 145 | +    Properties result = configuration.createPropertiesFromConnectionDetails(connectionDetails); | 
|  | 146 | + | 
|  | 147 | +    assertThat(result.getValue(Properties.HTTP_PORT)).isEqualTo(httpPort); | 
|  | 148 | +  } | 
|  | 149 | + | 
|  | 150 | +  @Test | 
|  | 151 | +  @DisplayName("Should override GRPC port in properties if it exists") | 
|  | 152 | +  void shouldOverrideGrpcPortPropertiesIfExists() { | 
|  | 153 | +    Integer grpcPort = 6001; | 
|  | 154 | + | 
|  | 155 | +    when(connectionDetails.getGrpcPort()).thenReturn(grpcPort); | 
|  | 156 | + | 
|  | 157 | +    Properties result = configuration.createPropertiesFromConnectionDetails(connectionDetails); | 
|  | 158 | + | 
|  | 159 | +    assertThat(result.getValue(Properties.GRPC_PORT)).isEqualTo(grpcPort); | 
|  | 160 | +  } | 
|  | 161 | + | 
|  | 162 | +  private static class TestDaprClientAutoConfiguration extends DaprClientAutoConfiguration { | 
|  | 163 | + | 
|  | 164 | +    private final DaprClientBuilder daprClientBuilder; | 
|  | 165 | + | 
|  | 166 | +    public TestDaprClientAutoConfiguration(DaprClientBuilder daprClientBuilder) { | 
|  | 167 | +      this.daprClientBuilder = daprClientBuilder; | 
|  | 168 | +    } | 
|  | 169 | + | 
|  | 170 | +    @Override | 
|  | 171 | +    protected DaprClientBuilder createDaprClientBuilder() { | 
|  | 172 | +      return daprClientBuilder; | 
|  | 173 | +    } | 
|  | 174 | +  } | 
|  | 175 | + | 
|  | 176 | +} | 
0 commit comments