From c61b363bc701a24d93ba8a8f57ef21aeb173b9fa Mon Sep 17 00:00:00 2001 From: scmacdon Date: Mon, 10 Jul 2023 08:55:07 -0400 Subject: [PATCH] update tests --- .../src/test/java/AutoScaleTest.java | 236 ++++++------------ 1 file changed, 72 insertions(+), 164 deletions(-) diff --git a/javav2/example_code/autoscale/src/test/java/AutoScaleTest.java b/javav2/example_code/autoscale/src/test/java/AutoScaleTest.java index a377b0ceb6c..401f65105b3 100644 --- a/javav2/example_code/autoscale/src/test/java/AutoScaleTest.java +++ b/javav2/example_code/autoscale/src/test/java/AutoScaleTest.java @@ -3,212 +3,120 @@ SPDX-License-Identifier: Apache-2.0 */ -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertFalse; -import com.example.autoscaling.AutoScalingScenario; -import com.example.autoscaling.CreateAutoScalingGroup; -import com.example.autoscaling.DeleteAutoScalingGroup; -import com.example.autoscaling.DescribeAutoScalingInstances; -import com.example.autoscaling.DetachInstances; -import com.google.gson.Gson; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.MethodOrderer; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.TestMethodOrder; -import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; -import software.amazon.awssdk.services.autoscaling.AutoScalingClient; -import java.io.IOException; -import java.util.Random; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; -import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest; -import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse; - -/** - * To run these integration tests, you must set the required values - * in the config.properties file or AWS Secrets Manager. - */ +import com.example.appsync.*; +import org.junit.jupiter.api.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; +import software.amazon.awssdk.services.appsync.AppSyncClient; +import java.io.*; +import java.util.*; +import software.amazon.awssdk.regions.Region; + @TestInstance(TestInstance.Lifecycle.PER_METHOD) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) -public class AutoScaleTest { +public class AppSyncTest { - private static AutoScalingClient autoScalingClient; - private static String groupName=""; - private static String groupNameSc=""; - private static String instanceId=""; - private static String instanceId2=""; - private static String launchTemplateName=""; - private static String vpcZoneId=""; + private static AppSyncClient appSyncClient; + private static String apiId=""; + private static String dsName=""; + private static String dsRole=""; + private static String tableName=""; + private static String keyId = ""; // Gets dynamically set in a test. + private static String dsARN = ""; // Gets dynamically set in a test. + private static String reg = ""; @BeforeAll public static void setUp() throws IOException { - autoScalingClient = AutoScalingClient.builder() - .region(Region.US_EAST_1) - .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) - .build(); - - Random random = new Random(); - int randomNum = random.nextInt((10000 - 1) + 1) + 1; - - // Get the values to run these tests from AWS Secrets Manager. - Gson gson = new Gson(); - TestValues myValues = gson.fromJson(String.valueOf(getSecretValues()), TestValues.class); - groupName = myValues.getGroupName()+randomNum; - launchTemplateName = myValues.getLaunchTemplateName(); - vpcZoneId = myValues.getVpcZoneId(); - groupNameSc = myValues.getGroupNameSc()+randomNum; - - // Uncomment this code block if you prefer using a config.properties file to retrieve AWS values required for these tests. - /* - try (InputStream input = AutoScaleTest.class.getClassLoader().getResourceAsStream("config.properties")) { + + ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create(); + Region region = Region.US_EAST_1; + reg = region.toString(); + appSyncClient = AppSyncClient.builder() + .region(region) + .credentialsProvider(credentialsProvider) + .build(); + + try (InputStream input = AppSyncTest.class.getClassLoader().getResourceAsStream("config.properties")) { + Properties prop = new Properties(); + if (input == null) { System.out.println("Sorry, unable to find config.properties"); return; } prop.load(input); - groupName = prop.getProperty("groupName")+randomNum; - launchTemplateName = prop.getProperty("launchTemplateName"); - vpcZoneId = prop.getProperty("vpcZoneId"); - groupNameSc = prop.getProperty("groupNameSc")+randomNum; + apiId = prop.getProperty("apiId"); + dsName = prop.getProperty("dsName"); + dsRole= prop.getProperty("dsRole"); + tableName= prop.getProperty("tableName"); } catch (IOException ex) { ex.printStackTrace(); } - */ } @Test @Order(1) - public void createAutoScalingGroup() { - assertDoesNotThrow(() -> CreateAutoScalingGroup.createAutoScalingGroup(autoScalingClient, groupName, launchTemplateName, vpcZoneId)); + public void whenInitializingAWSService_thenNotNull() { + assertNotNull(appSyncClient); + assertTrue(!apiId.isEmpty()); + assertTrue(!dsName.isEmpty()); + assertTrue(!dsRole.isEmpty()); + assertTrue(!tableName.isEmpty()); System.out.println("Test 1 passed"); } @Test @Order(2) - public void describeAutoScalingInstances() throws InterruptedException { - System.out.println("Wait 1 min for the resources"); - Thread.sleep(60000); - instanceId2 = DescribeAutoScalingInstances.getAutoScaling(autoScalingClient, groupName); - assertFalse(instanceId2.isEmpty()); - System.out.println(instanceId2); + public void CreateApiKey() { + keyId = CreateApiKey.createKey(appSyncClient, apiId); + assertTrue(!keyId.isEmpty()); System.out.println("Test 2 passed"); } @Test @Order(3) - public void detachInstances()throws InterruptedException { - System.out.println("Wait 1 min for the resources, including the instance"); - Thread.sleep(60000); - assertDoesNotThrow(() -> DetachInstances.detachInstance(autoScalingClient, groupName, instanceId2)); + public void CreateDataSource() { + dsARN = CreateDataSource.createDS(appSyncClient, dsName, reg, dsRole, apiId, tableName); + assertTrue(!dsARN.isEmpty()); System.out.println("Test 3 passed"); } @Test @Order(4) - public void deleteAutoScalingGroup() { - assertDoesNotThrow(() -> DeleteAutoScalingGroup.deleteAutoScalingGroup(autoScalingClient, groupName)); + public void GetDataSource() { + GetDataSource.getDS(appSyncClient, apiId, dsName); System.out.println("Test 4 passed"); } @Test @Order(5) - public void autoScalingScenario() throws InterruptedException { - System.out.println("**** Create an Auto Scaling group named "+groupName); - AutoScalingScenario.createAutoScalingGroup(autoScalingClient, groupNameSc, launchTemplateName, vpcZoneId); - - System.out.println("Wait 1 min for the resources, including the instance. Otherwise, an empty instance Id is returned"); - Thread.sleep(60000); - - System.out.println("**** Get Auto Scale group Id value"); - String instanceId = AutoScalingScenario.getSpecificAutoScalingGroups(autoScalingClient, groupNameSc); - assertFalse(instanceId.isEmpty()); - - System.out.println("**** Describe Auto Scaling with the Id value "+instanceId); - AutoScalingScenario.describeAutoScalingInstance( autoScalingClient, instanceId); - - System.out.println("**** Enable metrics collection "+instanceId); - AutoScalingScenario.enableMetricsCollection(autoScalingClient, groupNameSc); - - System.out.println("**** Update an Auto Scaling group to update max size to 3"); - AutoScalingScenario.updateAutoScalingGroup(autoScalingClient, groupNameSc, launchTemplateName); - - System.out.println("**** Describe all Auto Scaling groups to show the current state of the groups"); - AutoScalingScenario.describeAutoScalingGroups(autoScalingClient, groupNameSc); - - System.out.println("**** Describe account details"); - AutoScalingScenario.describeAccountLimits(autoScalingClient); - - System.out.println("Wait 1 min for the resources, including the instance. Otherwise, an empty instance Id is returned"); - Thread.sleep(60000); - - System.out.println("**** Set desired capacity to 2"); - AutoScalingScenario.setDesiredCapacity(autoScalingClient, groupNameSc); - - System.out.println("**** Get the two instance Id values and state"); - AutoScalingScenario.getSpecificAutoScalingGroups(autoScalingClient, groupNameSc); - - System.out.println("**** List the scaling activities that have occurred for the group"); - AutoScalingScenario.describeScalingActivities(autoScalingClient, groupNameSc); - - System.out.println("**** Terminate an instance in the Auto Scaling group"); - AutoScalingScenario.terminateInstanceInAutoScalingGroup(autoScalingClient, instanceId); + public void ListGraphqlApis() { + ListGraphqlApis.getApis(appSyncClient); + System.out.println("Test 5 passed"); + } - System.out.println("**** Stop the metrics collection"); - AutoScalingScenario.disableMetricsCollection(autoScalingClient, groupNameSc); - - System.out.println("**** Delete the Auto Scaling group"); - AutoScalingScenario.deleteAutoScalingGroup(autoScalingClient, groupNameSc); - } - private static String getSecretValues() { - SecretsManagerClient secretClient = SecretsManagerClient.builder() - .region(Region.US_EAST_1) - .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) - .build(); - String secretName = "test/autoscale"; - - GetSecretValueRequest valueRequest = GetSecretValueRequest.builder() - .secretId(secretName) - .build(); - - GetSecretValueResponse valueResponse = secretClient.getSecretValue(valueRequest); - return valueResponse.secretString(); + @Test + @Order(6) + public void ListApiKeys() { + ListApiKeys.getKeys(appSyncClient,apiId); + System.out.println("Test 6 passed"); } - @Nested - @DisplayName("A class used to get test values from test/autoscale (an AWS Secrets Manager secret)") - class TestValues { - private String groupName; - private String groupNameSc; - - private String launchTemplateName; - - private String vpcZoneId; - - TestValues() { - } - - String getGroupName(){ - return this.groupName; - } - - String getGroupNameSc(){ - return this.groupNameSc; - } - - String getLaunchTemplateName(){ - return this.launchTemplateName; - } - - String getVpcZoneId(){ - return this.vpcZoneId; - } - } -} + @Test + @Order(7) + public void DeleteDataSource() { + DeleteDataSource.deleteDS(appSyncClient, apiId, dsName) ; + System.out.println("Test 7 passed"); + } + @Test + @Order(8) + public void DeleteApiKey() { + DeleteApiKey.deleteKey(appSyncClient, keyId, apiId) ; + System.out.println("Test 8 passed"); + } +} \ No newline at end of file