|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.core.management; |
| 5 | + |
| 6 | +import com.azure.core.management.serializer.SerializerFactory; |
| 7 | +import com.azure.core.util.serializer.SerializerAdapter; |
| 8 | +import com.azure.core.util.serializer.SerializerEncoding; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import wiremock.com.fasterxml.jackson.annotation.JsonProperty; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +public class ResourceTests { |
| 16 | + |
| 17 | + private static class ProxyResourceWithSystemData extends ProxyResource { |
| 18 | + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) |
| 19 | + private SystemData systemData; |
| 20 | + |
| 21 | + /** |
| 22 | + * Get the systemData value. |
| 23 | + * |
| 24 | + * @return the metadata pertaining to creation and last modification of the resource. |
| 25 | + * */ |
| 26 | + public SystemData systemData() { |
| 27 | + return this.systemData; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static class ResourceWithSystemData extends Resource { |
| 32 | + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) |
| 33 | + private SystemData systemData; |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the systemData value. |
| 37 | + * |
| 38 | + * @return the metadata pertaining to creation and last modification of the resource. |
| 39 | + * */ |
| 40 | + public SystemData systemData() { |
| 41 | + return this.systemData; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testSerialization() throws IOException { |
| 47 | + String cosmosAccountJson = "{\"id\":\"/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.DocumentDB/databaseAccounts/c1weidxu\",\"name\":\"c1weidxu\",\"location\":\"West US\",\"type\":\"Microsoft.DocumentDB/databaseAccounts\",\"kind\":\"GlobalDocumentDB\",\"tags\":{\"defaultExperience\":\"Core (SQL)\",\"hidden-cosmos-mmspecial\":\"\",\"CosmosAccountType\":\"Non-Production\"},\"systemData\":{\"createdBy\":\"00000000-0000-0000-0000-000000000000\",\"createdByType\":\"Application\",\"createdAt\":\"2021-03-03T02:03:46.3387771Z\",\"lastModifiedBy\":\"johndoe@microsoft.com\",\"lastModifiedByType\":\"User\",\"lastModifiedAt\":\"2021-03-03T02:04:42.8252362Z\"},\"properties\":{\"provisioningState\":\"Succeeded\",\"documentEndpoint\":\"https://c1weidxu.documents.azure.com:443/\",\"publicNetworkAccess\":\"Enabled\",\"enableAutomaticFailover\":false,\"enableMultipleWriteLocations\":false,\"enablePartitionKeyMonitor\":false,\"isVirtualNetworkFilterEnabled\":false,\"virtualNetworkRules\":[],\"EnabledApiTypes\":\"Sql\",\"disableKeyBasedMetadataWriteAccess\":false,\"enableFreeTier\":false,\"enableAnalyticalStorage\":false,\"instanceId\":\"f5a124e6-988e-4936-8c9b-38e011c80ef4\",\"createMode\":\"Default\",\"databaseAccountOfferType\":\"Standard\",\"enableCassandraConnector\":false,\"connectorOffer\":\"\",\"consistencyPolicy\":{\"defaultConsistencyLevel\":\"Session\",\"maxIntervalInSeconds\":5,\"maxStalenessPrefix\":100},\"configurationOverrides\":{},\"writeLocations\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"documentEndpoint\":\"https://c1weidxu-westus.documents.azure.com:443/\",\"provisioningState\":\"Succeeded\",\"failoverPriority\":0,\"isZoneRedundant\":false}],\"readLocations\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"documentEndpoint\":\"https://c1weidxu-westus.documents.azure.com:443/\",\"provisioningState\":\"Succeeded\",\"failoverPriority\":0,\"isZoneRedundant\":false}],\"locations\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"documentEndpoint\":\"https://c1weidxu-westus.documents.azure.com:443/\",\"provisioningState\":\"Succeeded\",\"failoverPriority\":0,\"isZoneRedundant\":false}],\"failoverPolicies\":[{\"id\":\"c1weidxu-westus\",\"locationName\":\"West US\",\"failoverPriority\":0}],\"cors\":[],\"capabilities\":[],\"ipRules\":[],\"backupPolicy\":{\"type\":\"Periodic\",\"periodicModeProperties\":{\"backupIntervalInMinutes\":240,\"backupRetentionIntervalInHours\":8}}}}"; |
| 48 | + |
| 49 | + SerializerAdapter serializerAdapter = SerializerFactory.createDefaultManagementSerializerAdapter(); |
| 50 | + ResourceWithSystemData cosmosAccountResource = serializerAdapter.deserialize(cosmosAccountJson, ResourceWithSystemData.class, SerializerEncoding.JSON); |
| 51 | + Assertions.assertEquals("/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.DocumentDB/databaseAccounts/c1weidxu", cosmosAccountResource.id()); |
| 52 | + Assertions.assertEquals(Region.US_WEST, Region.fromName(cosmosAccountResource.location())); |
| 53 | + Assertions.assertEquals("Microsoft.DocumentDB/databaseAccounts", cosmosAccountResource.type()); |
| 54 | + Assertions.assertEquals(3, cosmosAccountResource.tags().size()); |
| 55 | + Assertions.assertNotNull(cosmosAccountResource.systemData()); |
| 56 | + Assertions.assertNotNull(cosmosAccountResource.systemData().createdAt()); |
| 57 | + Assertions.assertEquals("00000000-0000-0000-0000-000000000000", cosmosAccountResource.systemData().createdBy()); |
| 58 | + Assertions.assertEquals(ResourceAuthorIdentityType.APPLICATION, cosmosAccountResource.systemData().createdByType()); |
| 59 | + Assertions.assertNotNull(cosmosAccountResource.systemData().createdAt()); |
| 60 | + Assertions.assertEquals("johndoe@microsoft.com", cosmosAccountResource.systemData().lastModifiedBy()); |
| 61 | + Assertions.assertEquals(ResourceAuthorIdentityType.USER, cosmosAccountResource.systemData().lastModifiedByType()); |
| 62 | + Assertions.assertNotNull(cosmosAccountResource.systemData().lastModifiedAt()); |
| 63 | + |
| 64 | + ProxyResourceWithSystemData proxyResource = serializerAdapter.deserialize(cosmosAccountJson, ProxyResourceWithSystemData.class, SerializerEncoding.JSON); |
| 65 | + Assertions.assertNotNull(proxyResource.systemData()); |
| 66 | + Assertions.assertNotNull(proxyResource.systemData().createdAt()); |
| 67 | + Assertions.assertEquals("00000000-0000-0000-0000-000000000000", proxyResource.systemData().createdBy()); |
| 68 | + Assertions.assertEquals(ResourceAuthorIdentityType.APPLICATION, proxyResource.systemData().createdByType()); |
| 69 | + Assertions.assertNotNull(cosmosAccountResource.systemData().createdAt()); |
| 70 | + Assertions.assertEquals("johndoe@microsoft.com", cosmosAccountResource.systemData().lastModifiedBy()); |
| 71 | + Assertions.assertEquals(ResourceAuthorIdentityType.USER, cosmosAccountResource.systemData().lastModifiedByType()); |
| 72 | + Assertions.assertNotNull(cosmosAccountResource.systemData().lastModifiedAt()); |
| 73 | + |
| 74 | + String vaultJson = "{\"id\":\"/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.KeyVault/vaults/v1weidxu\",\"name\":\"v1weidxu\",\"type\":\"Microsoft.KeyVault/vaults\",\"location\":\"centralus\",\"tags\":{},\"properties\":{\"sku\":{\"family\":\"A\",\"name\":\"standard\"},\"tenantId\":\"###\",\"accessPolicies\":[],\"enabledForDeployment\":false,\"vaultUri\":\"https://v1weidxu.vault.azure.net/\",\"provisioningState\":\"Succeeded\"}}"; |
| 75 | + ResourceWithSystemData vaultResource = serializerAdapter.deserialize(vaultJson, ResourceWithSystemData.class, SerializerEncoding.JSON); |
| 76 | + Assertions.assertEquals("/subscriptions/###/resourceGroups/rg-weidxu/providers/Microsoft.KeyVault/vaults/v1weidxu", vaultResource.id()); |
| 77 | + Assertions.assertEquals(Region.US_CENTRAL, Region.fromName(vaultResource.location())); |
| 78 | + Assertions.assertEquals("Microsoft.KeyVault/vaults", vaultResource.type()); |
| 79 | + Assertions.assertEquals(0, vaultResource.tags().size()); |
| 80 | + Assertions.assertNull(vaultResource.systemData()); |
| 81 | + } |
| 82 | +} |
0 commit comments