|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 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 | + * http://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 com.google.firebase.dataconnect |
| 18 | + |
| 19 | +import app.cash.turbine.test |
| 20 | +import com.google.firebase.dataconnect.testutil.DataConnectIntegrationTestBase |
| 21 | +import com.google.firebase.dataconnect.testutil.createGrpcManagedChannel |
| 22 | +import com.google.firebase.dataconnect.testutil.schemas.RealtimeConnector |
| 23 | +import com.google.firebase.dataconnect.testutil.schemas.RealtimeConnector.GetStringByKeyQuery |
| 24 | +import com.google.firebase.dataconnect.testutil.schemas.RealtimeConnector.InsertStringMutation |
| 25 | +import com.google.firebase.dataconnect.util.ProtoUtil.decodeFromStruct |
| 26 | +import com.google.firebase.dataconnect.util.ProtoUtil.encodeToStruct |
| 27 | +import google.firebase.dataconnect.proto.ConnectorStreamServiceGrpcKt.ConnectorStreamServiceCoroutineStub |
| 28 | +import google.firebase.dataconnect.proto.ExecuteRequest |
| 29 | +import google.firebase.dataconnect.proto.StreamRequest |
| 30 | +import google.firebase.dataconnect.proto.StreamResponse |
| 31 | +import io.kotest.assertions.assertSoftly |
| 32 | +import io.kotest.assertions.withClue |
| 33 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 34 | +import io.kotest.matchers.result.shouldBeSuccess |
| 35 | +import io.kotest.matchers.shouldBe |
| 36 | +import io.kotest.property.Arb |
| 37 | +import io.kotest.property.arbitrary.Codepoint |
| 38 | +import io.kotest.property.arbitrary.az |
| 39 | +import io.kotest.property.arbitrary.map |
| 40 | +import io.kotest.property.arbitrary.string |
| 41 | +import kotlinx.coroutines.channels.Channel |
| 42 | +import kotlinx.coroutines.channels.SendChannel |
| 43 | +import kotlinx.coroutines.flow.Flow |
| 44 | +import kotlinx.coroutines.flow.consumeAsFlow |
| 45 | +import kotlinx.coroutines.test.runTest |
| 46 | +import org.junit.Test |
| 47 | + |
| 48 | +class ConnectRPCIntegrationTest : DataConnectIntegrationTestBase() { |
| 49 | + |
| 50 | + @Test |
| 51 | + fun responseIsReceivedForExecuteQueryRequest() = runTest { |
| 52 | + val requestId = requestIdArb().sample() |
| 53 | + val name = nameArb().sample() |
| 54 | + val (connector, outgoingRequests, incomingResponses) = connect() |
| 55 | + val key = connector.insertString(name) |
| 56 | + outgoingRequests.sendInitRequest(requestId = "init", connector.resourceName) |
| 57 | + |
| 58 | + incomingResponses.test { |
| 59 | + outgoingRequests.sendNonBlockingOrThrow( |
| 60 | + StreamRequest.newBuilder().let { streamRequest -> |
| 61 | + streamRequest.setRequestId(requestId) |
| 62 | + streamRequest.setExecute( |
| 63 | + ExecuteRequest.newBuilder().let { executeRequest -> |
| 64 | + executeRequest.setOperationName(GetStringByKeyQuery.OPERATION_NAME) |
| 65 | + executeRequest.setVariables(encodeToStruct(GetStringByKeyQuery.Variables(key))) |
| 66 | + executeRequest.build() |
| 67 | + } |
| 68 | + ) |
| 69 | + streamRequest.build() |
| 70 | + } |
| 71 | + ) |
| 72 | + |
| 73 | + val streamResponse = awaitItem() |
| 74 | + |
| 75 | + streamResponse.shouldBeGetStringByKeyDataWithName(requestId = requestId, name = name) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun responseIsReceivedForExecuteMutationRequest() = runTest { |
| 81 | + val requestId = requestIdArb().sample() |
| 82 | + val name = nameArb().sample() |
| 83 | + val (connector, outgoingRequests, incomingResponses) = connect() |
| 84 | + outgoingRequests.sendInitRequest(requestId = "init", connector.resourceName) |
| 85 | + |
| 86 | + incomingResponses.test { |
| 87 | + outgoingRequests.sendNonBlockingOrThrow( |
| 88 | + StreamRequest.newBuilder().let { streamRequest -> |
| 89 | + streamRequest.setRequestId(requestId) |
| 90 | + streamRequest.setExecute( |
| 91 | + ExecuteRequest.newBuilder().let { executeRequest -> |
| 92 | + executeRequest.setOperationName(InsertStringMutation.OPERATION_NAME) |
| 93 | + executeRequest.setVariables(encodeToStruct(InsertStringMutation.Variables(name))) |
| 94 | + executeRequest.build() |
| 95 | + } |
| 96 | + ) |
| 97 | + streamRequest.build() |
| 98 | + } |
| 99 | + ) |
| 100 | + |
| 101 | + val streamResponse = awaitItem() |
| 102 | + |
| 103 | + val key = streamResponse.shouldBeInsertStringData(requestId = requestId) |
| 104 | + connector.getString(key).shouldNotBeNull().name shouldBe name |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Establishes a connection to the Data Connect `Connect` RPC and returns the streams for |
| 110 | + * communication. |
| 111 | + */ |
| 112 | + private fun connect(): ConnectionStreams { |
| 113 | + val connector = RealtimeConnector.getInstance(dataConnectFactory) |
| 114 | + val grpcManagedChannel = createGrpcManagedChannel(connector.dataConnect) |
| 115 | + val outgoingRequests = Channel<StreamRequest>(Channel.UNLIMITED) |
| 116 | + val stub = ConnectorStreamServiceCoroutineStub(grpcManagedChannel) |
| 117 | + val incomingResponses = stub.connect(outgoingRequests.consumeAsFlow()) |
| 118 | + return ConnectionStreams(connector, outgoingRequests, incomingResponses) |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Convenience extension function on [Arb] that gets a non-edge-case value using the [rs] property |
| 123 | + * of the [DataConnectIntegrationTestBase] superclass for the randomness source. |
| 124 | + */ |
| 125 | + private fun <T> Arb<T>.sample(): T = sample(rs).value |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Creates and returns an [Arb] that generates strings that are suitable for, and recognizable as, |
| 130 | + * "request IDs" in [StreamRequest] messages. |
| 131 | + */ |
| 132 | +private fun requestIdArb(): Arb<String> = Arb.string(size = 4, Codepoint.az()).map { "rid$it" } |
| 133 | + |
| 134 | +/** |
| 135 | + * Creates and returns an [Arb] that generates strings that are suitable for, and recognizable as, |
| 136 | + * "name" values for [RealtimeConnector.InsertStringMutation.Variables.name] and |
| 137 | + * [RealtimeConnector.UpdateStringMutation.Variables.name]. |
| 138 | + */ |
| 139 | +private fun nameArb(): Arb<String> = Arb.string(size = 4, Codepoint.az()).map { "nam$it" } |
| 140 | + |
| 141 | +/** Exception thrown by [sendNonBlockingOrThrow] if sending fails. */ |
| 142 | +private class SendChannelTrySendUnsuccessfulException(message: String, cause: Throwable?) : |
| 143 | + Exception(message, cause) |
| 144 | + |
| 145 | +/** |
| 146 | + * Sends an element to receiver [SendChannel] in a non-blocking way, throwing an exception if |
| 147 | + * sending was unsuccessful. |
| 148 | + * |
| 149 | + * @throws SendChannelTrySendUnsuccessfulException if [SendChannel.trySend] returns a failed result. |
| 150 | + */ |
| 151 | +private fun <T> SendChannel<T>.sendNonBlockingOrThrow(element: T) { |
| 152 | + val result = trySend(element) |
| 153 | + if (!result.isSuccess) { |
| 154 | + throw SendChannelTrySendUnsuccessfulException( |
| 155 | + "trySend() failed with isClosed=${result.isClosed}", |
| 156 | + result.exceptionOrNull() |
| 157 | + ) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +private data class ConnectionStreams( |
| 162 | + val connector: RealtimeConnector, |
| 163 | + val outgoingRequests: SendChannel<StreamRequest>, |
| 164 | + val incomingResponses: Flow<StreamResponse>, |
| 165 | +) |
| 166 | + |
| 167 | +/** |
| 168 | + * Sends an initialization request to the receiver [ConnectionStreams.outgoingRequests]. |
| 169 | + * |
| 170 | + * @param requestId The "request ID" to send in the outgoing message. |
| 171 | + * @param connectorResourceName The "connector resource name" to send in the outgoing message. |
| 172 | + */ |
| 173 | +private fun SendChannel<StreamRequest>.sendInitRequest( |
| 174 | + requestId: String, |
| 175 | + connectorResourceName: String |
| 176 | +) { |
| 177 | + val streamRequest = |
| 178 | + StreamRequest.newBuilder().let { |
| 179 | + it.setRequestId(requestId) |
| 180 | + it.setName(connectorResourceName) |
| 181 | + it.build() |
| 182 | + } |
| 183 | + sendNonBlockingOrThrow(streamRequest) |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Assertion to verify that a [StreamResponse] matches the expected "RealtimeString_GetByKey" data. |
| 188 | + * |
| 189 | + * @param requestId The expected request ID. |
| 190 | + * @param name The expected name in the data. |
| 191 | + */ |
| 192 | +private fun StreamResponse.shouldBeGetStringByKeyDataWithName(requestId: String, name: String) { |
| 193 | + withClue("StreamResponse.shouldBeGetStringByKeyDataWithName") { |
| 194 | + assertSoftly { |
| 195 | + val data = shouldBeSuccess<GetStringByKeyQuery.Data>(requestId = requestId) |
| 196 | + withClue("data") { data.item.shouldNotBeNull().name shouldBe name } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Assertion to verify that a [StreamResponse] matches the expected "RealtimeString_Insert" data. |
| 203 | + * |
| 204 | + * @param requestId The expected request ID. |
| 205 | + */ |
| 206 | +private fun StreamResponse.shouldBeInsertStringData(requestId: String): RealtimeConnector.Key = |
| 207 | + withClue("StreamResponse.shouldBeInsertStringData") { |
| 208 | + assertSoftly { shouldBeSuccess<InsertStringMutation.Data>(requestId = requestId).key } |
| 209 | + } |
| 210 | + |
| 211 | +/** |
| 212 | + * Assertion to verify that a [StreamResponse] matches the expected "RealtimeString_Insert" data. |
| 213 | + * |
| 214 | + * @param requestId The expected request ID. |
| 215 | + * @return the [Data] decoded from the receiver. |
| 216 | + */ |
| 217 | +private inline fun <reified Data> StreamResponse.shouldBeSuccess(requestId: String): Data { |
| 218 | + withClue("requestId") { this.requestId shouldBe requestId } |
| 219 | + withClue("errorsCount") { this.errorsCount shouldBe 0 } |
| 220 | + |
| 221 | + val dataDecodeResult = runCatching { decodeFromStruct<Data>(this.data) } |
| 222 | + val data = withClue("decodeFromStruct(data)") { dataDecodeResult.shouldBeSuccess() } |
| 223 | + |
| 224 | + return data |
| 225 | +} |
0 commit comments