|
| 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.testutil.schemas |
| 18 | + |
| 19 | +import com.google.firebase.dataconnect.ConnectorConfig |
| 20 | +import com.google.firebase.dataconnect.FirebaseDataConnect |
| 21 | +import com.google.firebase.dataconnect.serializers.UUIDSerializer |
| 22 | +import com.google.firebase.dataconnect.testutil.TestDataConnectFactory |
| 23 | +import java.util.UUID |
| 24 | +import kotlinx.serialization.Serializable |
| 25 | +import kotlinx.serialization.serializer |
| 26 | + |
| 27 | +class RealtimeConnector private constructor(val dataConnect: FirebaseDataConnect) { |
| 28 | + |
| 29 | + val getStringByKey = GetStringByKeyQuery(this) |
| 30 | + |
| 31 | + val insertString = InsertStringMutation(this) |
| 32 | + |
| 33 | + val updateString = UpdateStringMutation(this) |
| 34 | + |
| 35 | + val deleteString = DeleteStringMutation(this) |
| 36 | + |
| 37 | + suspend fun getString(key: Key) = getStringByKey.execute(key) |
| 38 | + |
| 39 | + suspend fun insertString(name: String) = insertString.execute(name) |
| 40 | + |
| 41 | + suspend fun updateString(key: Key, name: String) = updateString.execute(key, name) |
| 42 | + |
| 43 | + suspend fun deleteString(key: Key) = deleteString.execute(key) |
| 44 | + |
| 45 | + class GetStringByKeyQuery(val connector: RealtimeConnector) { |
| 46 | + |
| 47 | + suspend fun execute(key: Key) = execute(Variables(key)) |
| 48 | + |
| 49 | + suspend fun execute(variables: Variables) = queryRef(variables).execute().data.item |
| 50 | + |
| 51 | + fun queryRef(variables: Variables) = |
| 52 | + connector.dataConnect.query(OPERATION_NAME, variables, serializer<Data>(), serializer()) |
| 53 | + |
| 54 | + @Serializable data class Variables(val key: Key) |
| 55 | + |
| 56 | + @Serializable |
| 57 | + data class Data(val item: Item?) { |
| 58 | + @Serializable data class Item(val name: String) |
| 59 | + } |
| 60 | + |
| 61 | + companion object { |
| 62 | + const val OPERATION_NAME = "RealtimeString_GetByKey" |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + class InsertStringMutation(val connector: RealtimeConnector) { |
| 67 | + @Serializable data class Variables(val name: String) |
| 68 | + @Serializable data class Data(val key: Key) |
| 69 | + |
| 70 | + suspend fun execute(name: String) = execute(Variables(name = name)) |
| 71 | + |
| 72 | + suspend fun execute(variables: Variables) = mutationRef(variables).execute().data.key |
| 73 | + |
| 74 | + fun mutationRef(variables: Variables) = |
| 75 | + connector.dataConnect.mutation(OPERATION_NAME, variables, serializer<Data>(), serializer()) |
| 76 | + |
| 77 | + companion object { |
| 78 | + const val OPERATION_NAME = "RealtimeString_Insert" |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + class UpdateStringMutation(val connector: RealtimeConnector) { |
| 83 | + @Serializable data class Variables(val key: Key, val name: String) |
| 84 | + |
| 85 | + suspend fun execute(key: Key, name: String) = execute(Variables(key = key, name = name)) |
| 86 | + |
| 87 | + suspend fun execute(variables: Variables) { |
| 88 | + mutationRef(variables).execute() |
| 89 | + } |
| 90 | + |
| 91 | + fun mutationRef(variables: Variables) = |
| 92 | + connector.dataConnect.mutation(OPERATION_NAME, variables, serializer<Unit>(), serializer()) |
| 93 | + |
| 94 | + companion object { |
| 95 | + const val OPERATION_NAME = "RealtimeString_Update" |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + class DeleteStringMutation(val connector: RealtimeConnector) { |
| 100 | + @Serializable data class Variables(val key: Key) |
| 101 | + |
| 102 | + suspend fun execute(key: Key) = execute(Variables(key = key)) |
| 103 | + |
| 104 | + suspend fun execute(variables: Variables) { |
| 105 | + mutationRef(variables).execute() |
| 106 | + } |
| 107 | + |
| 108 | + fun mutationRef(variables: Variables) = |
| 109 | + connector.dataConnect.mutation(OPERATION_NAME, variables, serializer<Unit>(), serializer()) |
| 110 | + |
| 111 | + companion object { |
| 112 | + const val OPERATION_NAME = "RealtimeString_Delete" |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Serializable data class Key(val id: @Serializable(with = UUIDSerializer::class) UUID) |
| 117 | + |
| 118 | + companion object { |
| 119 | + val config = |
| 120 | + ConnectorConfig( |
| 121 | + connector = "realtime", |
| 122 | + location = "us-central1", |
| 123 | + serviceId = "sid2ehn9ct8te", |
| 124 | + ) |
| 125 | + |
| 126 | + fun getInstance(dataConnectFactory: TestDataConnectFactory): RealtimeConnector { |
| 127 | + val dataConnect = dataConnectFactory.newInstance(config) |
| 128 | + return RealtimeConnector(dataConnect) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments