|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or 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 | + * https://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 org.springframework.boot.testcontainers.service.connection.hazelcast; |
| 18 | + |
| 19 | +import java.util.UUID; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import com.hazelcast.client.config.ClientConfig; |
| 23 | +import com.hazelcast.client.impl.clientside.HazelcastClientProxy; |
| 24 | +import com.hazelcast.core.HazelcastInstance; |
| 25 | +import com.hazelcast.map.IMap; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.testcontainers.junit.jupiter.Container; |
| 28 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 29 | + |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 32 | +import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; |
| 33 | +import org.springframework.boot.autoconfigure.hazelcast.HazelcastConnectionDetails; |
| 34 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 35 | +import org.springframework.boot.testsupport.container.HazelcastContainer; |
| 36 | +import org.springframework.boot.testsupport.container.TestImage; |
| 37 | +import org.springframework.context.annotation.Configuration; |
| 38 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests for {@link HazelcastContainerConnectionDetailsFactory} with a custom hazelcast |
| 44 | + * cluster name. |
| 45 | + * |
| 46 | + * @author Dmytro Nosan |
| 47 | + */ |
| 48 | +@SpringJUnitConfig |
| 49 | +@Testcontainers(disabledWithoutDocker = true) |
| 50 | +class CustomClusterHazelcastContainerConnectionDetailsFactoryIntegrationTests { |
| 51 | + |
| 52 | + @Container |
| 53 | + @ServiceConnection |
| 54 | + static final HazelcastContainer hazelcast = TestImage.container(HazelcastContainer.class) |
| 55 | + .withEnv("HZ_CLUSTERNAME", "spring-boot"); |
| 56 | + |
| 57 | + @Autowired(required = false) |
| 58 | + private HazelcastConnectionDetails connectionDetails; |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private HazelcastInstance hazelcastInstance; |
| 62 | + |
| 63 | + @Test |
| 64 | + void connectionCanBeMadeToHazelcastContainer() { |
| 65 | + assertThat(this.connectionDetails).isNotNull(); |
| 66 | + assertThat(this.hazelcastInstance).satisfies(clusterName("spring-boot")); |
| 67 | + IMap<String, String> map = this.hazelcastInstance.getMap(UUID.randomUUID().toString()); |
| 68 | + map.put("test", "containers"); |
| 69 | + assertThat(map.get("test")).isEqualTo("containers"); |
| 70 | + } |
| 71 | + |
| 72 | + private static Consumer<HazelcastInstance> clusterName(String name) { |
| 73 | + return (hazelcastInstance) -> { |
| 74 | + assertThat(hazelcastInstance).isInstanceOf(HazelcastClientProxy.class); |
| 75 | + HazelcastClientProxy proxy = (HazelcastClientProxy) hazelcastInstance; |
| 76 | + assertThat(proxy.getClientConfig()).extracting(ClientConfig::getClusterName).isEqualTo(name); |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + @Configuration(proxyBeanMethods = false) |
| 81 | + @ImportAutoConfiguration(HazelcastAutoConfiguration.class) |
| 82 | + static class TestConfiguration { |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments