|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package com.azure.spring.cloud.autoconfigure.kafka; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.LinkedHashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import org.springframework.cloud.stream.config.BinderProperties; |
| 12 | +import org.springframework.cloud.stream.config.BindingServiceProperties; |
| 13 | +import org.springframework.util.StringUtils; |
| 14 | + |
| 15 | +import static com.azure.spring.cloud.autoconfigure.kafka.AzureKafkaSpringCloudStreamConfiguration.AZURE_KAFKA_SPRING_CLOUD_STREAM_CONFIGURATION_CLASS; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 18 | + |
| 19 | +@SuppressWarnings("unchecked") |
| 20 | +class BindingServicePropertiesBeanPostProcessorTest { |
| 21 | + |
| 22 | + private final BindingServicePropertiesBeanPostProcessor bpp = new BindingServicePropertiesBeanPostProcessor(); |
| 23 | + |
| 24 | + @Test |
| 25 | + void testReadSpringMainPropertiesMapWithoutOriginalValues() { |
| 26 | + Map<String, Object> env = new LinkedHashMap<>(); |
| 27 | + Map<String, Object> mainPropertiesMap = buildSpringMainPropertiesMap(env, null, null, null); |
| 28 | + assertSame(mainPropertiesMap, ((Map<String, Object>) env.get("spring")).get("main")); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testReadSpringMainPropertiesMapWithSpringProp() { |
| 33 | + Map<String, Object> env = new LinkedHashMap<>(); |
| 34 | + Map<String, Object> mainPropertiesMap = buildSpringMainPropertiesMap(env, "profiles", "active", "dev"); |
| 35 | + |
| 36 | + assertEquals("dev", ((Map<String, Map<String, Object>>) env.get("spring")).get("profiles").get("active")); |
| 37 | + assertSame(mainPropertiesMap, ((Map<String, Object>) env.get("spring")).get("main")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testReadSpringMainPropertiesMapWithMainProp() { |
| 42 | + Map<String, Object> env = new LinkedHashMap<>(); |
| 43 | + Map<String, Object> mainPropertiesMap = buildSpringMainPropertiesMap(env, "main", "banner-mode", "test"); |
| 44 | + |
| 45 | + assertEquals("test", ((Map<String, Map<String, Object>>) env.get("spring")).get("main").get("banner-mode")); |
| 46 | + assertSame(mainPropertiesMap, ((Map<String, Object>) env.get("spring")).get("main")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testReadSpringMainPropertiesMapWithSourcesProp() { |
| 51 | + Map<String, Object> env = new LinkedHashMap<>(); |
| 52 | + Map<String, Object> mainPropertiesMap = buildSpringMainPropertiesMap(env, "main", "sources", "test"); |
| 53 | + |
| 54 | + assertEquals("test", ((Map<String, Map<String, Object>>) env.get("spring")).get("main").get("sources")); |
| 55 | + assertSame(mainPropertiesMap, ((Map<String, Object>) env.get("spring")).get("main")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testConfigureBinderSources() { |
| 60 | + Map<String, Object> env = new LinkedHashMap<>(); |
| 61 | + Map<String, Object> mainPropertiesMap = buildSpringMainPropertiesMap(env, "main", "sources", "test"); |
| 62 | + bpp.configureBinderSources(mainPropertiesMap); |
| 63 | + assertEquals(AZURE_KAFKA_SPRING_CLOUD_STREAM_CONFIGURATION_CLASS + ",test", ((Map<String, Map<String, Object>>) env.get("spring")).get("main").get("sources")); |
| 64 | + |
| 65 | + env.clear(); |
| 66 | + mainPropertiesMap = buildSpringMainPropertiesMap(env, "main", "profiles", "active"); |
| 67 | + bpp.configureBinderSources(mainPropertiesMap); |
| 68 | + assertEquals(AZURE_KAFKA_SPRING_CLOUD_STREAM_CONFIGURATION_CLASS, ((Map<String, Map<String, Object>>) env.get("spring")).get("main").get("sources")); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testBindKafkaByDefault() { |
| 73 | + BindingServiceProperties bindingServiceProperties = new BindingServiceProperties(); |
| 74 | + bpp.postProcessBeforeInitialization(bindingServiceProperties, null); |
| 75 | + Map<String, Object> env = bindingServiceProperties.getBinders().get("kafka") |
| 76 | + .getEnvironment(); |
| 77 | + assertEquals(AZURE_KAFKA_SPRING_CLOUD_STREAM_CONFIGURATION_CLASS, ((Map<String, Map<String, Object>>) env.get("spring")).get("main").get("sources")); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testBindKafkaByName() { |
| 83 | + BinderProperties binderProperties = new BinderProperties(); |
| 84 | + Map<String, BinderProperties> binders = new HashMap<>(); |
| 85 | + binders.put("kafka", binderProperties); |
| 86 | + BindingServiceProperties bindingServiceProperties = new BindingServiceProperties(); |
| 87 | + bindingServiceProperties.setBinders(binders); |
| 88 | + |
| 89 | + bpp.postProcessBeforeInitialization(bindingServiceProperties, null); |
| 90 | + Map<String, Object> env = bindingServiceProperties.getBinders().get("kafka") |
| 91 | + .getEnvironment(); |
| 92 | + assertEquals(AZURE_KAFKA_SPRING_CLOUD_STREAM_CONFIGURATION_CLASS, ((Map<String, Map<String, Object>>) env.get("spring")).get("main").get("sources")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testBindKafkaByType() { |
| 97 | + BinderProperties binderProperties = new BinderProperties(); |
| 98 | + Map<String, BinderProperties> binders = new HashMap<>(); |
| 99 | + binders.put("test", binderProperties); |
| 100 | + binderProperties.setType("kafka"); |
| 101 | + BindingServiceProperties bindingServiceProperties = new BindingServiceProperties(); |
| 102 | + bindingServiceProperties.setBinders(binders); |
| 103 | + |
| 104 | + bpp.postProcessBeforeInitialization(bindingServiceProperties, null); |
| 105 | + Map<String, Object> env = bindingServiceProperties.getBinders().get("test") |
| 106 | + .getEnvironment(); |
| 107 | + assertEquals(AZURE_KAFKA_SPRING_CLOUD_STREAM_CONFIGURATION_CLASS, ((Map<String, Map<String, Object>>) env.get("spring")).get("main").get("sources")); |
| 108 | + } |
| 109 | + |
| 110 | + private Map<String, Object> buildSpringMainPropertiesMap(Map<String, Object> env, String secondProperty, String thirdProperty, String value) { |
| 111 | + if (StringUtils.hasText(secondProperty)) { |
| 112 | + Map<String, Object> second = new LinkedHashMap<>(); |
| 113 | + second.put(thirdProperty, value); |
| 114 | + Map<String, Object> first = new LinkedHashMap<>(); |
| 115 | + first.put(secondProperty, second); |
| 116 | + env.put("spring", first); |
| 117 | + } |
| 118 | + return bpp.readSpringMainPropertiesMap(env); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments