|
| 1 | +package io.dapr.ai.client; |
| 2 | + |
| 3 | +import io.dapr.client.resiliency.ResiliencyOptions; |
| 4 | +import io.dapr.config.Properties; |
| 5 | +import io.dapr.v1.DaprGrpc; |
| 6 | +import io.dapr.v1.DaprProtos; |
| 7 | +import io.grpc.ManagedChannel; |
| 8 | +import io.grpc.stub.StreamObserver; |
| 9 | +import org.junit.Assert; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | +import org.mockito.ArgumentCaptor; |
| 13 | +import org.mockito.Mockito; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.mockito.Mockito.*; |
| 19 | + |
| 20 | +public class DaprConversationClientTest { |
| 21 | + |
| 22 | + private DaprGrpc.DaprStub daprStub; |
| 23 | + |
| 24 | + @Before |
| 25 | + public void initialize() { |
| 26 | + |
| 27 | + ManagedChannel channel = mock(ManagedChannel.class); |
| 28 | + daprStub = mock(DaprGrpc.DaprStub.class); |
| 29 | + when(daprStub.getChannel()).thenReturn(channel); |
| 30 | + when(daprStub.withInterceptors(Mockito.any(), Mockito.any())).thenReturn(daprStub); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void converseShouldThrowIllegalArgumentExceptionWhenComponentNameIsNull() throws Exception { |
| 35 | + try (DaprConversationClient daprConversationClient = new DaprConversationClient()) { |
| 36 | + List<DaprConversationInput> daprConversationInputs = new ArrayList<>(); |
| 37 | + daprConversationInputs.add(new DaprConversationInput("Hello there !")); |
| 38 | + |
| 39 | + IllegalArgumentException exception = |
| 40 | + Assert.assertThrows(IllegalArgumentException.class, () -> |
| 41 | + daprConversationClient.converse(null, daprConversationInputs).block()); |
| 42 | + Assert.assertEquals("Conversation component name cannot be null or empty.", exception.getMessage()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void converseShouldThrowIllegalArgumentExceptionWhenConversationComponentIsEmpty() throws Exception { |
| 48 | + try (DaprConversationClient daprConversationClient = new DaprConversationClient()) { |
| 49 | + List<DaprConversationInput> daprConversationInputs = new ArrayList<>(); |
| 50 | + daprConversationInputs.add(new DaprConversationInput("Hello there !")); |
| 51 | + |
| 52 | + IllegalArgumentException exception = |
| 53 | + Assert.assertThrows(IllegalArgumentException.class, () -> |
| 54 | + daprConversationClient.converse("", daprConversationInputs).block()); |
| 55 | + Assert.assertEquals("Conversation component name cannot be null or empty.", exception.getMessage()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void converseShouldThrowIllegalArgumentExceptionWhenConversationInputIsEmpty() throws Exception { |
| 61 | + try (DaprConversationClient daprConversationClient = new DaprConversationClient()) { |
| 62 | + List<DaprConversationInput> daprConversationInputs = new ArrayList<>(); |
| 63 | + |
| 64 | + IllegalArgumentException exception = |
| 65 | + Assert.assertThrows(IllegalArgumentException.class, () -> |
| 66 | + daprConversationClient.converse("openai", daprConversationInputs).block()); |
| 67 | + Assert.assertEquals("Conversation inputs cannot be null or empty.", exception.getMessage()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void converseShouldThrowIllegalArgumentExceptionWhenConversationInputIsNull() throws Exception { |
| 73 | + try (DaprConversationClient daprConversationClient = |
| 74 | + new DaprConversationClient(new Properties(), null)) { |
| 75 | + |
| 76 | + IllegalArgumentException exception = |
| 77 | + Assert.assertThrows(IllegalArgumentException.class, () -> |
| 78 | + daprConversationClient.converse("openai", null).block()); |
| 79 | + Assert.assertEquals("Conversation inputs cannot be null or empty.", exception.getMessage()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void converseShouldThrowIllegalArgumentExceptionWhenConversationInputContentIsNull() throws Exception { |
| 85 | + try (DaprConversationClient daprConversationClient = new DaprConversationClient()) { |
| 86 | + List<DaprConversationInput> daprConversationInputs = new ArrayList<>(); |
| 87 | + daprConversationInputs.add(new DaprConversationInput(null)); |
| 88 | + |
| 89 | + IllegalArgumentException exception = |
| 90 | + Assert.assertThrows(IllegalArgumentException.class, () -> |
| 91 | + daprConversationClient.converse("openai", daprConversationInputs).block()); |
| 92 | + Assert.assertEquals("Conversation input content cannot be null or empty.", exception.getMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void converseShouldThrowIllegalArgumentExceptionWhenConversationInputContentIsEmpty() throws Exception { |
| 98 | + try (DaprConversationClient daprConversationClient = new DaprConversationClient()) { |
| 99 | + List<DaprConversationInput> daprConversationInputs = new ArrayList<>(); |
| 100 | + daprConversationInputs.add(new DaprConversationInput("")); |
| 101 | + |
| 102 | + IllegalArgumentException exception = |
| 103 | + Assert.assertThrows(IllegalArgumentException.class, () -> |
| 104 | + daprConversationClient.converse("openai", daprConversationInputs).block()); |
| 105 | + Assert.assertEquals("Conversation input content cannot be null or empty.", exception.getMessage()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void converseShouldReturnConversationResponseWhenRequiredInputsAreValid() throws Exception { |
| 111 | + DaprProtos.ConversationResponse conversationResponse = DaprProtos.ConversationResponse.newBuilder() |
| 112 | + .addOutputs(DaprProtos.ConversationResult.newBuilder().setResult("Hello How are you").build()).build(); |
| 113 | + |
| 114 | + doAnswer(invocation -> { |
| 115 | + StreamObserver<DaprProtos.ConversationResponse> observer = invocation.getArgument(1); |
| 116 | + observer.onNext(conversationResponse); |
| 117 | + observer.onCompleted(); |
| 118 | + return null; |
| 119 | + }).when(daprStub).converseAlpha1(any(DaprProtos.ConversationRequest.class), any()); |
| 120 | + |
| 121 | + try (DaprConversationClient daprConversationClient = |
| 122 | + new DaprConversationClient(daprStub, new ResiliencyOptions())) { |
| 123 | + List<DaprConversationInput> daprConversationInputs = new ArrayList<>(); |
| 124 | + daprConversationInputs.add(new DaprConversationInput("Hello there")); |
| 125 | + |
| 126 | + DaprConversationResponse daprConversationResponse = |
| 127 | + daprConversationClient.converse("openai", daprConversationInputs).block(); |
| 128 | + |
| 129 | + ArgumentCaptor<DaprProtos.ConversationRequest> captor = |
| 130 | + ArgumentCaptor.forClass(DaprProtos.ConversationRequest.class); |
| 131 | + verify(daprStub, times(1)).converseAlpha1(captor.capture(), Mockito.any()); |
| 132 | + |
| 133 | + DaprProtos.ConversationRequest conversationRequest = captor.getValue(); |
| 134 | + |
| 135 | + Assert.assertEquals("openai", conversationRequest.getName()); |
| 136 | + Assert.assertEquals("Hello there", conversationRequest.getInputs(0).getContent()); |
| 137 | + Assert.assertEquals("Hello How are you", |
| 138 | + daprConversationResponse.getDaprConversationOutputs().get(0).getResult()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void converseShouldReturnConversationResponseWhenRequiredAndOptionalInputsAreValid() throws Exception { |
| 144 | + DaprProtos.ConversationResponse conversationResponse = DaprProtos.ConversationResponse.newBuilder() |
| 145 | + .setContextID("contextId") |
| 146 | + .addOutputs(DaprProtos.ConversationResult.newBuilder().setResult("Hello How are you").build()).build(); |
| 147 | + |
| 148 | + doAnswer(invocation -> { |
| 149 | + StreamObserver<DaprProtos.ConversationResponse> observer = invocation.getArgument(1); |
| 150 | + observer.onNext(conversationResponse); |
| 151 | + observer.onCompleted(); |
| 152 | + return null; |
| 153 | + }).when(daprStub).converseAlpha1(any(DaprProtos.ConversationRequest.class), any()); |
| 154 | + |
| 155 | + try (DaprConversationClient daprConversationClient = new DaprConversationClient(daprStub, null)) { |
| 156 | + DaprConversationInput daprConversationInput = new DaprConversationInput("Hello there") |
| 157 | + .setRole(DaprConversationRole.ASSISSTANT) |
| 158 | + .setScrubPii(true); |
| 159 | + |
| 160 | + List<DaprConversationInput> daprConversationInputs = new ArrayList<>(); |
| 161 | + daprConversationInputs.add(daprConversationInput); |
| 162 | + |
| 163 | + DaprConversationResponse daprConversationResponse = |
| 164 | + daprConversationClient.converse("openai", daprConversationInputs, |
| 165 | + "contextId", true, 1.1d).block(); |
| 166 | + |
| 167 | + ArgumentCaptor<DaprProtos.ConversationRequest> captor = |
| 168 | + ArgumentCaptor.forClass(DaprProtos.ConversationRequest.class); |
| 169 | + verify(daprStub, times(1)).converseAlpha1(captor.capture(), Mockito.any()); |
| 170 | + |
| 171 | + DaprProtos.ConversationRequest conversationRequest = captor.getValue(); |
| 172 | + |
| 173 | + Assert.assertEquals("openai", conversationRequest.getName()); |
| 174 | + Assert.assertEquals("contextId", conversationRequest.getContextID()); |
| 175 | + Assert.assertTrue(conversationRequest.getScrubPII()); |
| 176 | + Assert.assertEquals(1.1d, conversationRequest.getTemperature(), 0d); |
| 177 | + Assert.assertEquals("Hello there", conversationRequest.getInputs(0).getContent()); |
| 178 | + Assert.assertTrue(conversationRequest.getInputs(0).getScrubPII()); |
| 179 | + Assert.assertEquals(DaprConversationRole.ASSISSTANT.toString(), conversationRequest.getInputs(0).getRole()); |
| 180 | + Assert.assertEquals("contextId", daprConversationResponse.getContextId()); |
| 181 | + Assert.assertEquals("Hello How are you", |
| 182 | + daprConversationResponse.getDaprConversationOutputs().get(0).getResult()); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments