From 74f230c94950907e67780a358a56185dac651a47 Mon Sep 17 00:00:00 2001 From: smirnovaae Date: Mon, 11 Mar 2024 09:19:07 -0700 Subject: [PATCH] wip --- .../gov/cms/ab2d/optout/OptOutConstants.java | 2 +- .../java/gov/cms/ab2d/optout/OptOutS3.java | 30 +++++++++++++++++++ .../cms/ab2d/optout/OptOutHandlerTest.java | 2 +- optout/src/test/resources/sqsEvent.json | 10 +++---- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java index 458057e..3e14d9b 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/OptOutConstants.java @@ -6,7 +6,7 @@ public class OptOutConstants { public static final String ENDPOINT = "https://s3.amazonaws.com"; public static final String TEST_ENDPOINT = "http://127.0.0.1:8001"; public static final String TEST_FILE_NAME = "optOutDummy.txt"; - public static final String TEST_BFD_BUCKET_NAME = "ab2d-opt-out-temp-349849222861-us-east-1"; + public static final String TEST_BFD_BUCKET_NAME = "bfd-test-eft"; public static final Region S3_REGION = Region.US_EAST_1; public static final String HEADER_RESP = "HDR_BENEDATARSP"; public static final String TRAILER_RESP = "TLR_BENEDATARSP"; diff --git a/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java b/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java index 5891f52..8e2e815 100644 --- a/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java +++ b/optout/src/main/java/gov/cms/ab2d/optout/OptOutS3.java @@ -27,11 +27,41 @@ public OptOutS3(S3Client s3Client, String fileName, String bfdBucket, LambdaLogg this.logger = logger; } + public void doesBucketExist(){ + try { + logger.log("Bucket name: " + bfdBucket); + logger.log("File name: " + fileName); + + HeadBucketRequest headBucketRequest = HeadBucketRequest.builder() + .bucket(bfdBucket).build(); + + s3Client.headBucket(headBucketRequest); + logger.log("Bucket exists"); + + } catch (SdkClientException ex) { + var errorMessage = "Unable to load credentials to connect S3 bucket"; + logger.log(errorMessage); + throw new OptOutException(errorMessage, ex); + } catch (S3Exception ex) { + if (ex.statusCode() == 404) { + + var errorMessage = "Object " + fileName + " does not exist. " + ex.getMessage(); + logger.log(errorMessage); + throw new OptOutException(errorMessage, ex); + } else { + logger.log(ex.getMessage()); + throw ex; + } + } + + } + public BufferedReader openFileS3() { try { //Checking if object exists logger.log("Buket name: " + bfdBucket); logger.log("File name: " + fileName); + HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() .bucket(bfdBucket) .key(fileName) diff --git a/optout/src/test/java/gov/cms/ab2d/optout/OptOutHandlerTest.java b/optout/src/test/java/gov/cms/ab2d/optout/OptOutHandlerTest.java index 6078725..7e61c19 100644 --- a/optout/src/test/java/gov/cms/ab2d/optout/OptOutHandlerTest.java +++ b/optout/src/test/java/gov/cms/ab2d/optout/OptOutHandlerTest.java @@ -44,7 +44,7 @@ static void beforeAll() throws URISyntaxException, IOException { } @Test - void getTest() throws IOException, ParseException { + void getBucketAndFileNamesTest() throws IOException, ParseException { JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(getPayload()); var s3EventMessage = json.get("Message"); diff --git a/optout/src/test/resources/sqsEvent.json b/optout/src/test/resources/sqsEvent.json index a723240..9d2740d 100644 --- a/optout/src/test/resources/sqsEvent.json +++ b/optout/src/test/resources/sqsEvent.json @@ -1,12 +1,12 @@ { "Type" : "Notification", "MessageId" : "5da6e965-ca05-5123-8347-b72ad7893428", - "TopicArn" : "arn:aws:sns:us-east-1:577373831711:bfd-test-eft-inbound-received-s3-dpc", + "TopicArn" : "arn:aws:sns:test", "Subject" : "Amazon S3 Notification", - "Message" : "{\"Records\":[{\"eventVersion\":\"2.1\",\"eventSource\":\"aws:s3\",\"awsRegion\":\"us-east-1\",\"eventTime\":\"2024-02-20T21:36:11.419Z\",\"eventName\":\"ObjectCreated:Put\",\"userIdentity\":{\"principalId\":\"AWS:AIDAYM3RJQIPTYMO35QPN\"},\"requestParameters\":{\"sourceIPAddress\":\"66.61.78.110\"},\"responseElements\":{\"x-amz-request-id\":\"JM2GMJJNSSEH9PKY\",\"x-amz-id-2\":\"gO4Fl4AxgEJ8LYteQ6LW4gHNtmB9+CcRDOnE20HW9ZumAQTsP+HG1DKDYLxQa67VCG6XIyIfSsZOW8cOfPuOQAD3+7HfBs/3\"},\"s3\":{\"s3SchemaVersion\":\"1.0\",\"configurationId\":\"bfd-test-eft-inbound-received-s3-dpc\",\"bucket\":{\"name\":\"bfd-test-eft\",\"ownerIdentity\":{\"principalId\":\"A5VBSMJFI0FCE\"},\"arn\":\"arn:aws:s3:::bfd-test-eft\"},\"object\":{\"key\":\"bfdeft01/ab2d/in/optOutDummy.txt\",\"size\":12,\"eTag\":\"fdad692379d13b4c029804195d0a4aa5\",\"sequencer\":\"0065D51B4B4098E68C\"}}}]}", + "Message" : "{\"Records\":[{\"eventVersion\":\"2.1\",\"eventSource\":\"aws:s3\",\"awsRegion\":\"us-east-1\",\"eventTime\":\"2024-02-20T21:36:11.419Z\",\"eventName\":\"ObjectCreated:Put\",\"s3\":{\"s3SchemaVersion\":\"1.0\",\"configurationId\":\"bfd-test-eft-inbound-received-s3-dpc\",\"bucket\":{\"name\":\"bfd-test-eft\",\"arn\":\"arn:aws:s3:::bfd-test-eft\"},\"object\":{\"key\":\"bfdeft01/ab2d/in/optOutDummy.txt\",\"size\":12}}}]}", "Timestamp" : "2024-02-20T21:36:12.405Z", "SignatureVersion" : "1", - "Signature" : "geHsbUYOSZ6HZLDo0cVnMsmbrsncnGNubwLHZfcSmAtIpm4ciCeR3tkoZeZhWySXFW8N1a9URlgo1WIy0Cf08yspRG0HCtdYJCVv0hJtxiHpS2M3qyDJY0o3hsX4Uk29PcczjUAu89/j7niPRID/zh0LQcUWkA9UPaHDrxAJR69WDV5EvPLOBl4B2gNA/zUGgIlQL7jXhYiGnvRhldNUD2ZibY+wSrlYEs/b+U/dJsid1bvzT30Dw6nwpEB6Xqpw8KH6DX32E2/XDvKOqELd5aAv1P/+uLMhohzD/DSmRd31pZUECF99/GLdT2b41/M1L1KQm31r7uF9Lvy4DrG8CQ==", - "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-60eadc530605d63b8e62a523676ef735.pem", - "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:577373831711:bfd-test-eft-inbound-received-s3-dpc:f14e07ab-36d0-48ec-bdcd-41b0be370fc8" + "Signature" : "TestSignature", + "SigningCertURL" : "TestSigningCertURL", + "UnsubscribeURL" : "TestUnsubscribeURL" } \ No newline at end of file