/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package software.amazon.awssdk.services.kms;

import junit.framework.Assert;
import org.junit.Test;
import software.amazon.awssdk.services.kms.model.CreateAliasRequest;
import software.amazon.awssdk.services.kms.model.CreateKeyRequest;
import software.amazon.awssdk.services.kms.model.CreateKeyResponse;
import software.amazon.awssdk.services.kms.model.DescribeKeyRequest;
import software.amazon.awssdk.services.kms.model.DescribeKeyResponse;
import software.amazon.awssdk.services.kms.model.DisableKeyRequest;
import software.amazon.awssdk.services.kms.model.EnableKeyRequest;
import software.amazon.awssdk.services.kms.model.GetKeyPolicyRequest;
import software.amazon.awssdk.services.kms.model.GetKeyPolicyResponse;
import software.amazon.awssdk.services.kms.model.KeyMetadata;
import software.amazon.awssdk.services.kms.model.KeyUsageType;
import software.amazon.awssdk.services.kms.model.ListKeysRequest;
import software.amazon.awssdk.services.kms.model.ListKeysResponse;

public class ServiceIntegrationTest extends IntegrationTestBase {

    private static void checkValid_KeyMetadata(KeyMetadata kmd) {
        Assert.assertNotNull(kmd);

        Assert.assertNotNull(kmd.arn());
        Assert.assertNotNull(kmd.awsAccountId());
        Assert.assertNotNull(kmd.description());
        Assert.assertNotNull(kmd.keyId());
        Assert.assertNotNull(kmd.keyUsage());
        Assert.assertNotNull(kmd.creationDate());
        Assert.assertNotNull(kmd.enabled());
    }

    @Test
    public void testKeyOperations() {

        // CreateKey
        CreateKeyResponse createKeyResult = kms.createKey(CreateKeyRequest.builder()
                                                                        .description("My KMS Key")
                                                                        .keyUsage(KeyUsageType.ENCRYPT_DECRYPT)
                                                                        .build());
        checkValid_KeyMetadata(createKeyResult.keyMetadata());

        final String keyId = createKeyResult.keyMetadata().keyId();

        // DescribeKey
        DescribeKeyResponse describeKeyResult = kms.describeKey(DescribeKeyRequest.builder().keyId(keyId).build());
        checkValid_KeyMetadata(describeKeyResult.keyMetadata());

        // Enable/DisableKey
        kms.enableKey(EnableKeyRequest.builder().keyId(keyId).build());
        kms.disableKey(DisableKeyRequest.builder().keyId(keyId).build());

        // ListKeys
        ListKeysResponse listKeysResult = kms.listKeys(ListKeysRequest.builder().build());
        Assert.assertFalse(listKeysResult.keys().isEmpty());

        // CreateAlias
        kms.createAlias(CreateAliasRequest.builder()
                                          .aliasName("alias/my_key" + System.currentTimeMillis())
                                          .targetKeyId(keyId)
                                          .build());

        GetKeyPolicyResponse getKeyPolicyResult = kms.getKeyPolicy(GetKeyPolicyRequest.builder()
                                                                                    .keyId(keyId)
                                                                                    .policyName("default")
                                                                                    .build());
        Assert.assertNotNull(getKeyPolicyResult.policy());

    }
}

@@ -2488,32 +2488,50 @@
      .build());%0A
+        try %7B%0A    
         checkVal
@@ -2586,16 +2586,20 @@
         
+ 
+   
 final St
@@ -2659,16 +2659,20 @@
         
+    
 // Descr
@@ -2686,16 +2686,20 @@
         
+  
+  
 Describe
@@ -2798,32 +2798,36 @@
 ild());%0A        
+    
 checkValid_KeyMe
@@ -2876,16 +2876,20 @@
         
+   
+ 
 // Enabl
@@ -2897,24 +2897,28 @@
 /DisableKey%0A
+    
         kms.
@@ -2969,32 +2969,36 @@
 eyId).build());%0A
+    
         kms.disa
@@ -3064,16 +3064,20 @@
         
+    
 // ListK
@@ -3080,16 +3080,20 @@
 istKeys%0A
+    
         
@@ -3175,32 +3175,36 @@
 ild());%0A        
+ 
+   
 Assert.assertFal
@@ -3241,32 +3241,36 @@
 ty());%0A%0A        
+    
 // CreateAlias%0A 
@@ -3272,24 +3272,28 @@
 ias%0A        
+  
+  
 kms.createAl
@@ -3317,32 +3317,36 @@
 quest.builder()%0A
+    
                 
@@ -3469,16 +3469,20 @@
         
+   
+ 
 .targetK
@@ -3527,32 +3527,36 @@
                 
+    
 .build());%0A%0A    
@@ -3543,32 +3543,36 @@
     .build());%0A%0A
+    
         GetKeyPo
@@ -3732,24 +3732,30 @@
             
+      
 .keyId(keyId
@@ -3840,16 +3840,22 @@
         
+   
+   
 .policyN
@@ -3945,32 +3945,38 @@
                 
+      
 .build());%0A     
@@ -3962,32 +3962,36 @@
      .build());%0A
+    
         Assert.a
@@ -4028,22 +4028,208 @@
 t.policy());
+%0A        %7D finally %7B%0A            kms.scheduleKeyDeletion(r -%3E r.keyId(createKeyResult.keyMetadata().keyId())%0A                                          .pendingWindowInDays(7));%0A        %7D
 %0A%0A    %7D%0A%7D%0A

Clean up KMS integration test resources when test completes. (#3305)