|
| 1 | +import com.example.cognito.*; |
| 2 | +import org.junit.jupiter.api.*; |
| 3 | +import software.amazon.awssdk.regions.Region; |
| 4 | +import software.amazon.awssdk.services.cognitoidentity.CognitoIdentityClient; |
| 5 | +import software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClient; |
| 6 | +import java.io.*; |
| 7 | +import java.util.*; |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | + |
| 10 | +@TestInstance(TestInstance.Lifecycle.PER_METHOD) |
| 11 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 12 | +public class AmazonCognitoTest { |
| 13 | + |
| 14 | + private static CognitoIdentityProviderClient cognitoclient; |
| 15 | + private static CognitoIdentityProviderClient cognitoIdentityProviderClient ; |
| 16 | + private static CognitoIdentityClient cognitoIdclient ; |
| 17 | + private static String userPoolName=""; |
| 18 | + private static String identityId=""; |
| 19 | + private static String userPoolId="" ; //set in test 2 |
| 20 | + private static String identityPoolId =""; //set in test 5 |
| 21 | + private static String username=""; |
| 22 | + private static String email=""; |
| 23 | + private static String clientName=""; |
| 24 | + private static String identityPoolName=""; |
| 25 | + private static String appId=""; |
| 26 | + private static String existingUserPoolId=""; |
| 27 | + private static String existingIdentityPoolId = ""; |
| 28 | + private static String providerName=""; |
| 29 | + private static String existingPoolName=""; |
| 30 | + |
| 31 | + @BeforeAll |
| 32 | + public static void setUp() throws IOException { |
| 33 | + |
| 34 | + // Run tests on Real AWS Resources |
| 35 | + Region region = Region.US_EAST_1; |
| 36 | + cognitoclient = CognitoIdentityProviderClient.builder() |
| 37 | + .region(Region.US_EAST_1) |
| 38 | + .build(); |
| 39 | + |
| 40 | + cognitoIdclient = CognitoIdentityClient.builder() |
| 41 | + .region(Region.US_EAST_1) |
| 42 | + .build(); |
| 43 | + |
| 44 | + cognitoIdentityProviderClient = CognitoIdentityProviderClient.builder() |
| 45 | + .region(Region.US_EAST_1) |
| 46 | + .build(); |
| 47 | + |
| 48 | + |
| 49 | + try (InputStream input = AmazonCognitoTest.class.getClassLoader().getResourceAsStream("config.properties")) { |
| 50 | + |
| 51 | + Properties prop = new Properties(); |
| 52 | + |
| 53 | + if (input == null) { |
| 54 | + System.out.println("Sorry, unable to find config.properties"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + //load a properties file from class path, inside static method |
| 59 | + prop.load(input); |
| 60 | + |
| 61 | + // Populate the data members required for all tests |
| 62 | + userPoolName = prop.getProperty("userPoolName"); |
| 63 | + username= prop.getProperty("username"); |
| 64 | + email= prop.getProperty("email"); |
| 65 | + clientName = prop.getProperty("clientName"); |
| 66 | + identityPoolName = prop.getProperty("identityPoolName"); |
| 67 | + identityId = prop.getProperty("identityId"); // used in the GetIdentityCredentials test |
| 68 | + appId = prop.getProperty("appId"); |
| 69 | + existingUserPoolId = prop.getProperty("existingUserPoolId"); |
| 70 | + existingIdentityPoolId = prop.getProperty("existingIdentityPoolId"); |
| 71 | + providerName = prop.getProperty("providerName"); |
| 72 | + existingPoolName = prop.getProperty("existingPoolName"); |
| 73 | + |
| 74 | + } catch (IOException ex) { |
| 75 | + ex.printStackTrace(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @Order(1) |
| 81 | + public void whenInitializingAWSS3Service_thenNotNull() { |
| 82 | + assertNotNull(cognitoclient); |
| 83 | + assertNotNull(cognitoIdclient); |
| 84 | + assertNotNull(cognitoIdentityProviderClient); |
| 85 | + System.out.println("Test 1 passed"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + @Order(2) |
| 90 | + public void CreateUserPool() { |
| 91 | + userPoolId = CreateUserPool.createPool(cognitoclient, userPoolName); |
| 92 | + assertTrue(!userPoolId.isEmpty()); |
| 93 | + System.out.println("Test 2 passed"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @Order(3) |
| 98 | + public void CreateAdminUser() { |
| 99 | + CreateAdminUser.createAdmin(cognitoclient,userPoolId ,username, email); |
| 100 | + System.out.println("Test 2 passed"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @Order(4) |
| 105 | + public void CreateUserPoolClient() { |
| 106 | + CreateUserPoolClient.createPoolClient(cognitoclient,clientName, userPoolId); |
| 107 | + System.out.println("Test 4 passed"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @Order(5) |
| 112 | + public void CreateIdentityPool() { |
| 113 | + identityPoolId = CreateIdentityPool.createIdPool(cognitoIdclient, identityPoolName); |
| 114 | + assertTrue(!identityPoolId.isEmpty()); |
| 115 | + System.out.println("Test 5 passed"); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + @Order(6) |
| 120 | + public void ListUserPools() { |
| 121 | + ListUserPools.listAllUserPools(cognitoclient); |
| 122 | + System.out.println("Test 6 passed"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @Order(7) |
| 127 | + public void ListIdentityPools() { |
| 128 | + ListIdentityPools.listIdPools(cognitoIdclient); |
| 129 | + System.out.println("Test 7 passed"); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + @Order(8) |
| 134 | + public void ListUserPoolClients() { |
| 135 | + |
| 136 | + ListUserPoolClients.listAllUserPoolClients(cognitoIdentityProviderClient, existingUserPoolId); |
| 137 | + System.out.println("Test 8 passed"); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + @Order(9) |
| 142 | + public void ListUsers() { |
| 143 | + ListUsers.listAllUsers(cognitoclient, existingUserPoolId); |
| 144 | + System.out.println("Test 9 passed"); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + @Order(10) |
| 149 | + public void ListIdentities() { |
| 150 | + ListIdentities.listPoolIdentities(cognitoIdclient, existingIdentityPoolId); |
| 151 | + System.out.println("Test 9 passed"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + @Order(11) |
| 156 | + public void AddLoginProvider() { |
| 157 | + AddLoginProvider.setLoginProvider(cognitoIdclient, appId, existingPoolName, existingIdentityPoolId, providerName); |
| 158 | + System.out.println("Test 11 passed"); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + @Order(12) |
| 163 | + public void GetIdentityCredentials() { |
| 164 | + GetIdentityCredentials.getCredsForIdentity(cognitoIdclient, identityId); |
| 165 | + System.out.println("Test 12 passed"); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + @Order(13) |
| 170 | + public void GetId() { |
| 171 | + GetId.getClientID(cognitoIdclient, existingIdentityPoolId); |
| 172 | + System.out.println("Test 13 passed"); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + @Order(14) |
| 177 | + public void DeleteUserPool() { |
| 178 | + DeleteUserPool.deletePool(cognitoclient, userPoolId); |
| 179 | + System.out.println("Test 14 passed"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + @Order(15) |
| 184 | + public void DeleteIdentityPool() { |
| 185 | + |
| 186 | + DeleteIdentityPool.deleteIdPool(cognitoIdclient, identityPoolId); |
| 187 | + System.out.println("Test 15 passed"); |
| 188 | + } |
| 189 | +} |
0 commit comments