|
| 1 | +package io.dapr.it.spring.cloudconfig; |
| 2 | + |
| 3 | +import io.dapr.testcontainers.Component; |
| 4 | +import io.dapr.testcontainers.DaprContainer; |
| 5 | +import io.dapr.testcontainers.DaprLogLevel; |
| 6 | +import org.junit.jupiter.api.Tag; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 12 | +import org.springframework.test.context.ContextConfiguration; |
| 13 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 14 | +import org.testcontainers.containers.Network; |
| 15 | +import org.testcontainers.images.builder.Transferable; |
| 16 | +import org.testcontainers.junit.jupiter.Container; |
| 17 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static io.dapr.it.testcontainers.DaprContainerConstants.IMAGE_TAG; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | + |
| 24 | +@SpringBootTest(properties = { |
| 25 | + "spring.config.import[0]=dapr:secret:" + DaprSecretStoreIT.SECRET_STORE_NAME |
| 26 | + + "/" + DaprSecretStoreIT.SECRET_MULTI_NAME + "?type=doc", |
| 27 | + "spring.config.import[1]=dapr:secret:" + DaprSecretStoreIT.SECRET_STORE_NAME |
| 28 | + + "/" + DaprSecretStoreIT.SECRET_SINGLE_NAME + "?type=value", |
| 29 | + "spring.config.import[2]=dapr:secret:" + DaprSecretStoreIT.SECRET_STORE_NAME_MULTI |
| 30 | + + "?type=value", |
| 31 | +}) |
| 32 | +@ContextConfiguration(classes = TestDaprCloudConfigConfiguration.class) |
| 33 | +@ExtendWith(SpringExtension.class) |
| 34 | +@Testcontainers |
| 35 | +@Tag("testcontainers") |
| 36 | +public class DaprSecretStoreIT { |
| 37 | + public static final String SECRET_STORE_NAME = "democonfig"; |
| 38 | + public static final String SECRET_MULTI_NAME = "multivalue-properties"; |
| 39 | + public static final String SECRET_SINGLE_NAME = "dapr.spring.demo-config-secret.singlevalue"; |
| 40 | + |
| 41 | + public static final String SECRET_STORE_NAME_MULTI = "democonfigMultivalued"; |
| 42 | + |
| 43 | + private static final Map<String, String> SINGLE_VALUE_PROPERTY = generateSingleValueProperty(); |
| 44 | + private static final Map<String, String> MULTI_VALUE_PROPERTY = generateMultiValueProperty(); |
| 45 | + |
| 46 | + private static final Network DAPR_NETWORK = Network.newNetwork(); |
| 47 | + |
| 48 | + @Container |
| 49 | + @ServiceConnection |
| 50 | + private static final DaprContainer DAPR_CONTAINER = new DaprContainer(IMAGE_TAG) |
| 51 | + .withAppName("secret-store-dapr-app") |
| 52 | + .withComponent(new Component(SECRET_STORE_NAME, "secretstores.local.file", "v1", SINGLE_VALUE_PROPERTY)) |
| 53 | + .withComponent(new Component(SECRET_STORE_NAME_MULTI, "secretstores.local.file", "v1", MULTI_VALUE_PROPERTY)) |
| 54 | + .withNetwork(DAPR_NETWORK) |
| 55 | + .withDaprLogLevel(DaprLogLevel.DEBUG) |
| 56 | + .withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String())) |
| 57 | + .withCopyToContainer(Transferable.of(DaprSecretStores.SINGLE_VALUED_SECRET), "/dapr-secrets/singlevalued.json") |
| 58 | + .withCopyToContainer(Transferable.of(DaprSecretStores.MULTI_VALUED_SECRET), "/dapr-secrets/multivalued.json"); |
| 59 | + |
| 60 | + private static Map<String, String> generateSingleValueProperty() { |
| 61 | + return Map.of("secretsFile", "/dapr-secrets/singlevalued.json", |
| 62 | + "multiValued", "false"); |
| 63 | + } |
| 64 | + |
| 65 | + private static Map<String, String> generateMultiValueProperty() { |
| 66 | + return Map.of("secretsFile", "/dapr-secrets/multivalued.json", |
| 67 | + "nestedSeparator", ".", |
| 68 | + "multiValued", "true"); |
| 69 | + } |
| 70 | + |
| 71 | + @Value("${dapr.spring.demo-config-secret.singlevalue}") |
| 72 | + String singleValue; |
| 73 | + |
| 74 | + @Value("${dapr.spring.demo-config-secret.multivalue.v1}") |
| 75 | + String multiV1; |
| 76 | + |
| 77 | + @Value("${dapr.spring.demo-config-secret.multivalue.v2}") |
| 78 | + String multiV2; |
| 79 | + |
| 80 | + @Value("${dapr.spring.demo-config-secret.multivalue.v4}") |
| 81 | + String multiV4; |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testSecretStore() { |
| 85 | + assertEquals("testvalue", singleValue); |
| 86 | + assertEquals("spring", multiV1); |
| 87 | + assertEquals("dapr", multiV2); |
| 88 | + assertEquals("config", multiV4); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments