|
1 | | -package cloud.localstack; |
| 1 | +package cloud.localstack.awssdkv1; |
2 | 2 |
|
3 | 3 | import com.amazonaws.auth.AWSCredentials; |
4 | 4 | import com.amazonaws.auth.AWSCredentialsProvider; |
|
33 | 33 | import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder; |
34 | 34 | import com.amazonaws.services.sqs.AmazonSQS; |
35 | 35 | import com.amazonaws.services.sqs.AmazonSQSClientBuilder; |
36 | | -import java.io.IOException; |
37 | | -import java.lang.reflect.Field; |
38 | | -import java.nio.channels.FileChannel; |
39 | | -import java.nio.file.CopyOption; |
40 | | -import java.nio.file.Files; |
41 | | -import java.nio.file.Path; |
42 | | -import java.nio.file.StandardCopyOption; |
43 | | -import java.util.Arrays; |
44 | | -import java.util.Collections; |
45 | | -import java.util.HashMap; |
46 | | -import java.util.Map; |
47 | | -import java.util.stream.Stream; |
| 36 | + |
| 37 | +import cloud.localstack.Constants; |
| 38 | +import cloud.localstack.Localstack; |
| 39 | +import cloud.localstack.CommonUtils; |
48 | 40 |
|
49 | 41 | @SuppressWarnings("all") |
50 | 42 | public class TestUtils { |
51 | 43 |
|
52 | | - public static final String DEFAULT_REGION = "us-east-1"; |
53 | | - public static final String TEST_ACCESS_KEY = "test"; |
54 | | - public static final String TEST_SECRET_KEY = "test"; |
55 | | - public static final AWSCredentials TEST_CREDENTIALS = new BasicAWSCredentials(TEST_ACCESS_KEY, TEST_SECRET_KEY); |
56 | | - |
57 | | - private static final String[] EXCLUDED_DIRECTORIES = { |
58 | | - ".github", ".git", ".idea", ".venv", "target", "node_modules" |
59 | | - }; |
60 | | - |
61 | | - public static void setEnv(String key, String value) { |
62 | | - Map<String, String> newEnv = new HashMap<String, String>(System.getenv()); |
63 | | - newEnv.put(key, value); |
64 | | - setEnv(newEnv); |
65 | | - } |
| 44 | + public static final AWSCredentials TEST_CREDENTIALS = new BasicAWSCredentials( |
| 45 | + Constants.TEST_ACCESS_KEY, Constants.TEST_SECRET_KEY); |
66 | 46 |
|
67 | 47 | public static AmazonSQS getClientSQS() { |
68 | 48 | return getClientSQS(null); |
@@ -227,87 +207,16 @@ protected static AwsClientBuilder.EndpointConfiguration getEndpointConfiguration |
227 | 207 | return getEndpointConfiguration(Localstack.INSTANCE.getEndpointStepFunctions()); |
228 | 208 | } |
229 | 209 |
|
230 | | - protected static void setEnv(Map<String, String> newEnv) { |
231 | | - try { |
232 | | - Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment"); |
233 | | - Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment"); |
234 | | - theEnvironmentField.setAccessible(true); |
235 | | - Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null); |
236 | | - env.putAll(newEnv); |
237 | | - Field theCaseInsensitiveEnvironmentField = processEnvironmentClass |
238 | | - .getDeclaredField("theCaseInsensitiveEnvironment"); |
239 | | - theCaseInsensitiveEnvironmentField.setAccessible(true); |
240 | | - Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null); |
241 | | - cienv.putAll(newEnv); |
242 | | - } catch (NoSuchFieldException e) { |
243 | | - try { |
244 | | - Class[] classes = Collections.class.getDeclaredClasses(); |
245 | | - Map<String, String> env = System.getenv(); |
246 | | - for (Class cl : classes) { |
247 | | - if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) { |
248 | | - Field field = cl.getDeclaredField("m"); |
249 | | - field.setAccessible(true); |
250 | | - Object obj = field.get(env); |
251 | | - Map<String, String> map = (Map<String, String>) obj; |
252 | | - map.clear(); |
253 | | - map.putAll(newEnv); |
254 | | - } |
255 | | - } |
256 | | - } catch (Exception e2) { |
257 | | - e2.printStackTrace(); |
258 | | - } |
259 | | - } catch (Exception e1) { |
260 | | - e1.printStackTrace(); |
261 | | - } |
262 | | - } |
263 | | - |
264 | | - public static void disableSslCertChecking() { |
265 | | - System.setProperty("com.amazonaws.sdk.disableCertChecking", "true"); |
266 | | - } |
267 | | - |
268 | | - public static void copyFolder(Path src, Path dest) throws IOException { |
269 | | - try(Stream<Path> stream = Files.walk(src)) { |
270 | | - stream.forEach(source -> { |
271 | | - boolean isExcluded = Arrays.stream(EXCLUDED_DIRECTORIES) |
272 | | - .anyMatch( excluded -> source.toAbsolutePath().toString().contains(excluded)); |
273 | | - if (!isExcluded) { |
274 | | - copy(source, dest.resolve(src.relativize(source))); |
275 | | - } |
276 | | - }); |
277 | | - } |
278 | | - } |
279 | | - |
280 | | - public static void copy(Path source, Path dest) { |
281 | | - try { |
282 | | - CopyOption[] options = new CopyOption[] {StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING}; |
283 | | - if(Files.isDirectory(dest)) { |
284 | | - // continue without copying |
285 | | - return; |
286 | | - } |
287 | | - if (Files.exists(dest)) { |
288 | | - try(FileChannel sourceFile = FileChannel.open(source)) { |
289 | | - try (FileChannel destFile = FileChannel.open(dest)) { |
290 | | - if (!Files.getLastModifiedTime(source).equals(Files.getLastModifiedTime(dest)) |
291 | | - || sourceFile.size() != destFile.size() |
292 | | - ) { |
293 | | - Files.copy(source, dest, options); |
294 | | - } |
295 | | - } |
296 | | - } |
297 | | - } else { |
298 | | - Files.copy(source, dest, options); |
299 | | - } |
300 | | - } catch (Exception e) { |
301 | | - throw new RuntimeException(e.getMessage(), e); |
302 | | - } |
303 | | - } |
| 210 | + /** |
| 211 | + * UTIL METHODS |
| 212 | + */ |
304 | 213 |
|
305 | 214 | public static AWSCredentialsProvider getCredentialsProvider() { |
306 | 215 | return new AWSStaticCredentialsProvider(TEST_CREDENTIALS); |
307 | 216 | } |
308 | 217 |
|
309 | 218 | protected static AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(String endpointURL) { |
310 | | - return new AwsClientBuilder.EndpointConfiguration(endpointURL, DEFAULT_REGION); |
| 219 | + return new AwsClientBuilder.EndpointConfiguration(endpointURL, Constants.DEFAULT_REGION); |
311 | 220 | } |
312 | 221 |
|
313 | 222 | } |
0 commit comments