|
24 | 24 | import com.ctrip.framework.apollo.biz.service.ItemService; |
25 | 25 | import com.ctrip.framework.apollo.common.dto.*; |
26 | 26 |
|
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.Base64; |
27 | 29 | import java.util.List; |
28 | 30 | import java.util.Objects; |
29 | 31 | import org.junit.Assert; |
@@ -175,4 +177,74 @@ public void testSearch() { |
175 | 177 | assertThat(itemInfoDTOS.getContent().toString()) |
176 | 178 | .isEqualTo(response.getBody().getContent().toString()); |
177 | 179 | } |
| 180 | + |
| 181 | + @Test |
| 182 | + @Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) |
| 183 | + @Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) |
| 184 | + public void testGetByEncodedKey() { |
| 185 | + this.testCreate(); |
| 186 | + |
| 187 | + String appId = "someAppId"; |
| 188 | + AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); |
| 189 | + assert app != null; |
| 190 | + ClusterDTO cluster = |
| 191 | + restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); |
| 192 | + assert cluster != null; |
| 193 | + NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), NamespaceDTO.class, |
| 194 | + app.getAppId(), cluster.getName(), "application"); |
| 195 | + |
| 196 | + String itemKey = "test-key"; |
| 197 | + |
| 198 | + // Test with URL-safe Base64 encoding without padding |
| 199 | + String encodedKey = Base64.getUrlEncoder().withoutPadding() |
| 200 | + .encodeToString(itemKey.getBytes(StandardCharsets.UTF_8)); |
| 201 | + |
| 202 | + String getByEncodedKeyUrl = url( |
| 203 | + "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/encodedItems/{key}"); |
| 204 | + assert namespace != null; |
| 205 | + ResponseEntity<ItemDTO> response = restTemplate.getForEntity(getByEncodedKeyUrl, ItemDTO.class, |
| 206 | + app.getAppId(), cluster.getName(), namespace.getNamespaceName(), encodedKey); |
| 207 | + |
| 208 | + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 209 | + Assert.assertEquals(itemKey, Objects.requireNonNull(response.getBody()).getKey()); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + @Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) |
| 214 | + @Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) |
| 215 | + public void testGetByEncodedKeyWithComplexKey() { |
| 216 | + String appId = "someAppId"; |
| 217 | + AppDTO app = restTemplate.getForObject(appBaseUrl(), AppDTO.class, appId); |
| 218 | + assert app != null; |
| 219 | + ClusterDTO cluster = |
| 220 | + restTemplate.getForObject(clusterBaseUrl(), ClusterDTO.class, app.getAppId(), "default"); |
| 221 | + assert cluster != null; |
| 222 | + NamespaceDTO namespace = restTemplate.getForObject(namespaceBaseUrl(), NamespaceDTO.class, |
| 223 | + app.getAppId(), cluster.getName(), "application"); |
| 224 | + |
| 225 | + // Create an item with a complex key containing special characters |
| 226 | + String complexKey = "wonfu.soa.circuit-breaker.enable.gitea-svc@/api/v1/fetchWorkflows"; |
| 227 | + String itemValue = "test-value"; |
| 228 | + ItemDTO item = new ItemDTO(complexKey, itemValue, "", 1); |
| 229 | + assert namespace != null; |
| 230 | + item.setNamespaceId(namespace.getId()); |
| 231 | + item.setDataChangeLastModifiedBy("apollo"); |
| 232 | + |
| 233 | + ResponseEntity<ItemDTO> createResponse = restTemplate.postForEntity(itemBaseUrl(), item, |
| 234 | + ItemDTO.class, app.getAppId(), cluster.getName(), namespace.getNamespaceName()); |
| 235 | + Assert.assertEquals(HttpStatus.OK, createResponse.getStatusCode()); |
| 236 | + |
| 237 | + // Now retrieve it using the encoded key |
| 238 | + String encodedKey = Base64.getUrlEncoder().withoutPadding() |
| 239 | + .encodeToString(complexKey.getBytes(StandardCharsets.UTF_8)); |
| 240 | + |
| 241 | + String getByEncodedKeyUrl = url( |
| 242 | + "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/encodedItems/{key}"); |
| 243 | + ResponseEntity<ItemDTO> response = restTemplate.getForEntity(getByEncodedKeyUrl, ItemDTO.class, |
| 244 | + app.getAppId(), cluster.getName(), namespace.getNamespaceName(), encodedKey); |
| 245 | + |
| 246 | + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); |
| 247 | + Assert.assertEquals(complexKey, Objects.requireNonNull(response.getBody()).getKey()); |
| 248 | + Assert.assertEquals(itemValue, response.getBody().getValue()); |
| 249 | + } |
178 | 250 | } |
0 commit comments