|
| 1 | +/* |
| 2 | + * Copyright 2021 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.it.testcontainers; |
| 15 | + |
| 16 | +import io.dapr.client.DaprPreviewClient; |
| 17 | +import io.dapr.client.domain.ConversationInput; |
| 18 | +import io.dapr.client.domain.ConversationRequest; |
| 19 | +import io.dapr.client.domain.ConversationResponse; |
| 20 | +import io.dapr.testcontainers.Component; |
| 21 | +import io.dapr.testcontainers.DaprContainer; |
| 22 | +import io.dapr.testcontainers.DaprLogLevel; |
| 23 | +import org.junit.jupiter.api.Assertions; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Tag; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest; |
| 29 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 30 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 31 | +import org.springframework.test.context.DynamicPropertySource; |
| 32 | +import org.testcontainers.containers.Network; |
| 33 | +import org.testcontainers.junit.jupiter.Container; |
| 34 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Random; |
| 40 | + |
| 41 | +import static io.dapr.it.testcontainers.ContainerConstants.DAPR_RUNTIME_IMAGE_TAG; |
| 42 | + |
| 43 | +@SpringBootTest( |
| 44 | + webEnvironment = WebEnvironment.RANDOM_PORT, |
| 45 | + classes = { |
| 46 | + DaprPreviewClientConfiguration.class, |
| 47 | + TestConversationApplication.class |
| 48 | + } |
| 49 | +) |
| 50 | +@Testcontainers |
| 51 | +@Tag("testcontainers") |
| 52 | +public class DaprConversationIT { |
| 53 | + |
| 54 | + private static final Network DAPR_NETWORK = Network.newNetwork(); |
| 55 | + private static final Random RANDOM = new Random(); |
| 56 | + private static final int PORT = RANDOM.nextInt(1000) + 8000; |
| 57 | + |
| 58 | + @Container |
| 59 | + private static final DaprContainer DAPR_CONTAINER = new DaprContainer(DAPR_RUNTIME_IMAGE_TAG) |
| 60 | + .withAppName("conversation-dapr-app") |
| 61 | + .withComponent(new Component("echo", "conversation.echo", "v1", new HashMap<>())) |
| 62 | + .withNetwork(DAPR_NETWORK) |
| 63 | + .withDaprLogLevel(DaprLogLevel.DEBUG) |
| 64 | + .withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String())) |
| 65 | + .withAppChannelAddress("host.testcontainers.internal") |
| 66 | + .withAppPort(PORT); |
| 67 | + |
| 68 | + /** |
| 69 | + * Expose the Dapr port to the host. |
| 70 | + * |
| 71 | + * @param registry the dynamic property registry |
| 72 | + */ |
| 73 | + @DynamicPropertySource |
| 74 | + static void daprProperties(DynamicPropertyRegistry registry) { |
| 75 | + registry.add("dapr.http.endpoint", DAPR_CONTAINER::getHttpEndpoint); |
| 76 | + registry.add("dapr.grpc.endpoint", DAPR_CONTAINER::getGrpcEndpoint); |
| 77 | + registry.add("server.port", () -> PORT); |
| 78 | + } |
| 79 | + |
| 80 | + @Autowired |
| 81 | + private DaprPreviewClient daprPreviewClient; |
| 82 | + |
| 83 | + @BeforeEach |
| 84 | + public void setUp(){ |
| 85 | + org.testcontainers.Testcontainers.exposeHostPorts(PORT); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testConversationSDKShouldHaveSameOutputAndInput() { |
| 90 | + ConversationInput conversationInput = new ConversationInput("input this"); |
| 91 | + List<ConversationInput> conversationInputList = new ArrayList<>(); |
| 92 | + conversationInputList.add(conversationInput); |
| 93 | + |
| 94 | + ConversationResponse response = |
| 95 | + this.daprPreviewClient.converse(new ConversationRequest("echo", conversationInputList)).block(); |
| 96 | + |
| 97 | + Assertions.assertEquals("", response.getContextId()); |
| 98 | + Assertions.assertEquals("input this", response.getConversationOutputs().get(0).getResult()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testConversationSDKShouldScrubPIIWhenScrubPIIIsSetInRequestBody() { |
| 103 | + List<ConversationInput> conversationInputList = new ArrayList<>(); |
| 104 | + conversationInputList. add( new ConversationInput( "input this [email protected]")); |
| 105 | + conversationInputList.add(new ConversationInput("input this +12341567890")); |
| 106 | + |
| 107 | + ConversationResponse response = |
| 108 | + this.daprPreviewClient.converse(new ConversationRequest("echo", conversationInputList) |
| 109 | + .setScrubPii(true)).block(); |
| 110 | + |
| 111 | + Assertions.assertEquals("", response.getContextId()); |
| 112 | + Assertions.assertEquals("input this <EMAIL_ADDRESS>", |
| 113 | + response.getConversationOutputs().get(0).getResult()); |
| 114 | + Assertions.assertEquals("input this <PHONE_NUMBER>", |
| 115 | + response.getConversationOutputs().get(1).getResult()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testConversationSDKShouldScrubPIIOnlyForTheInputWhereScrubPIIIsSet() { |
| 120 | + List<ConversationInput> conversationInputList = new ArrayList<>(); |
| 121 | + conversationInputList. add( new ConversationInput( "input this [email protected]")); |
| 122 | + conversationInputList.add(new ConversationInput("input this +12341567890").setScrubPii(true)); |
| 123 | + |
| 124 | + ConversationResponse response = |
| 125 | + this.daprPreviewClient.converse(new ConversationRequest("echo", conversationInputList)).block(); |
| 126 | + |
| 127 | + Assertions.assertEquals("", response.getContextId()); |
| 128 | + Assertions. assertEquals( "input this [email protected]", |
| 129 | + response.getConversationOutputs().get(0).getResult()); |
| 130 | + Assertions.assertEquals("input this <PHONE_NUMBER>", |
| 131 | + response.getConversationOutputs().get(1).getResult()); |
| 132 | + } |
| 133 | +} |
0 commit comments