|
| 1 | +/* |
| 2 | + * Copyright 2025 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.adk.utils; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | + |
| 22 | +import com.google.adk.tools.BuiltInCodeExecutionTool; |
| 23 | +import com.google.adk.tools.GoogleSearchTool; |
| 24 | +import java.util.Optional; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.junit.runners.JUnit4; |
| 28 | + |
| 29 | +@RunWith(JUnit4.class) |
| 30 | +public final class ComponentRegistryTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testPreWiredEntries() { |
| 34 | + ComponentRegistry registry = new ComponentRegistry(); |
| 35 | + |
| 36 | + Optional<GoogleSearchTool> searchTool = registry.get("google_search", GoogleSearchTool.class); |
| 37 | + assertThat(searchTool).isPresent(); |
| 38 | + |
| 39 | + Optional<BuiltInCodeExecutionTool> codeTool = |
| 40 | + registry.get("code_execution", BuiltInCodeExecutionTool.class); |
| 41 | + assertThat(codeTool).isPresent(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testRegisterAndGet() { |
| 46 | + ComponentRegistry registry = new ComponentRegistry(); |
| 47 | + String testValue = "test value"; |
| 48 | + |
| 49 | + registry.register("test_key", testValue); |
| 50 | + |
| 51 | + Optional<String> result = registry.get("test_key", String.class); |
| 52 | + assertThat(result).hasValue(testValue); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testGetWithoutType() { |
| 57 | + ComponentRegistry registry = new ComponentRegistry(); |
| 58 | + String testValue = "test value"; |
| 59 | + |
| 60 | + registry.register("test_key", testValue); |
| 61 | + |
| 62 | + Optional<Object> result = registry.get("test_key"); |
| 63 | + assertThat(result).hasValue(testValue); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testGetNonExistentKey() { |
| 68 | + ComponentRegistry registry = new ComponentRegistry(); |
| 69 | + |
| 70 | + Optional<String> result = registry.get("non_existent", String.class); |
| 71 | + assertThat(result).isEmpty(); |
| 72 | + |
| 73 | + Optional<Object> resultNoType = registry.get("non_existent"); |
| 74 | + assertThat(resultNoType).isEmpty(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testGetWithWrongType() { |
| 79 | + ComponentRegistry registry = new ComponentRegistry(); |
| 80 | + registry.register("test_key", "string value"); |
| 81 | + |
| 82 | + Optional<Integer> result = registry.get("test_key", Integer.class); |
| 83 | + assertThat(result).isEmpty(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testOverridePreWiredEntry() { |
| 88 | + ComponentRegistry registry = new ComponentRegistry(); |
| 89 | + String customSearchTool = "custom search tool"; |
| 90 | + |
| 91 | + registry.register("google_search", customSearchTool); |
| 92 | + |
| 93 | + Optional<String> result = registry.get("google_search", String.class); |
| 94 | + assertThat(result).hasValue(customSearchTool); |
| 95 | + |
| 96 | + Optional<GoogleSearchTool> originalTool = registry.get("google_search", GoogleSearchTool.class); |
| 97 | + assertThat(originalTool).isEmpty(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testRegisterWithNullName() { |
| 102 | + ComponentRegistry registry = new ComponentRegistry(); |
| 103 | + |
| 104 | + IllegalArgumentException thrown = |
| 105 | + assertThrows(IllegalArgumentException.class, () -> registry.register(null, "value")); |
| 106 | + |
| 107 | + assertThat(thrown.getMessage()).contains("Name cannot be null or empty"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testRegisterWithEmptyName() { |
| 112 | + ComponentRegistry registry = new ComponentRegistry(); |
| 113 | + |
| 114 | + IllegalArgumentException thrown1 = |
| 115 | + assertThrows(IllegalArgumentException.class, () -> registry.register("", "value")); |
| 116 | + IllegalArgumentException thrown2 = |
| 117 | + assertThrows(IllegalArgumentException.class, () -> registry.register(" ", "value")); |
| 118 | + |
| 119 | + assertThat(thrown1.getMessage()).contains("Name cannot be null or empty"); |
| 120 | + assertThat(thrown2.getMessage()).contains("Name cannot be null or empty"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testGetWithNullName() { |
| 125 | + ComponentRegistry registry = new ComponentRegistry(); |
| 126 | + |
| 127 | + Optional<String> result = registry.get(null, String.class); |
| 128 | + assertThat(result).isEmpty(); |
| 129 | + |
| 130 | + Optional<Object> resultNoType = registry.get(null); |
| 131 | + assertThat(resultNoType).isEmpty(); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testGetWithEmptyName() { |
| 136 | + ComponentRegistry registry = new ComponentRegistry(); |
| 137 | + |
| 138 | + Optional<String> result = registry.get("", String.class); |
| 139 | + assertThat(result).isEmpty(); |
| 140 | + |
| 141 | + Optional<String> resultWhitespace = registry.get(" ", String.class); |
| 142 | + assertThat(resultWhitespace).isEmpty(); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testRegisterNullValue() { |
| 147 | + ComponentRegistry registry = new ComponentRegistry(); |
| 148 | + |
| 149 | + IllegalArgumentException thrown = |
| 150 | + assertThrows(IllegalArgumentException.class, () -> registry.register("null_test", null)); |
| 151 | + |
| 152 | + assertThat(thrown.getMessage()).contains("Value cannot be null"); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testSubclassExtension() { |
| 157 | + class CustomComponentRegistry extends ComponentRegistry { |
| 158 | + public CustomComponentRegistry() { |
| 159 | + super(); |
| 160 | + register("custom_tool", "my custom tool"); |
| 161 | + register("custom_agent", new Object()); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + CustomComponentRegistry registry = new CustomComponentRegistry(); |
| 166 | + |
| 167 | + Optional<GoogleSearchTool> prewiredTool = registry.get("google_search", GoogleSearchTool.class); |
| 168 | + assertThat(prewiredTool).isPresent(); |
| 169 | + |
| 170 | + Optional<String> customTool = registry.get("custom_tool", String.class); |
| 171 | + assertThat(customTool).hasValue("my custom tool"); |
| 172 | + |
| 173 | + Optional<Object> customAgent = registry.get("custom_agent"); |
| 174 | + assertThat(customAgent).isPresent(); |
| 175 | + } |
| 176 | +} |
0 commit comments