forked from TuesdayGangnam/caregiver-salon
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws sns 서비스를 추가하라 (TuesdayGangnam#52)
aws sns 서비스를 추가하라
- Loading branch information
Showing
10 changed files
with
218 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...notification/adapter-aws-sns/src/main/java/com/caregiver/config/AwsDefaultCredential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.caregiver.config; | ||
|
||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.sns.AmazonSNS; | ||
import com.amazonaws.services.sns.AmazonSNSClientBuilder; | ||
import com.amazonaws.services.sns.model.MessageAttributeValue; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
/** | ||
* AmazonSNS 객체를 생성합니다. | ||
* 기본 자격 증명을 사용 합니다. | ||
*/ | ||
@Configuration | ||
@RequiredArgsConstructor | ||
@SuppressWarnings("SpringFacetCodeInspection") | ||
public class AwsDefaultCredential implements AwsSnsClientFactory { | ||
|
||
private final AwsProperties awsProperties; | ||
|
||
@Bean | ||
@Override | ||
public AmazonSNS createSnsClient() { | ||
BasicAWSCredentials credentialsMaster = new BasicAWSCredentials( | ||
awsProperties.getAccessKey(), awsProperties.getSecretKey()); | ||
|
||
AmazonSNSClientBuilder amazonSnsClientBuilder = AmazonSNSClientBuilder | ||
.standard() | ||
.withCredentials(new AWSStaticCredentialsProvider(credentialsMaster)); | ||
amazonSnsClientBuilder.setRegion(awsProperties.getSigningRegion()); | ||
|
||
return amazonSnsClientBuilder.build(); | ||
} | ||
|
||
/** | ||
* 메세지 전송 속성 값 을 가진 빈을 리턴합니다. | ||
* <a href='https://docs.aws.amazon.com/ko_kr/sns/latest/dg/sms_publish-to-phone.html#sms_publish_sdk'></a> | ||
*/ | ||
@Bean | ||
public Map<String, MessageAttributeValue> messageAttributeValue() { | ||
return Map.of( | ||
"AWS.SNS.SMS.SenderID", | ||
new MessageAttributeValue().withStringValue("mySenderId").withDataType("String"), | ||
"AWS.SNS.SMS.MaxPrice", | ||
new MessageAttributeValue().withStringValue("0.50").withDataType("String"), | ||
"AWS.SNS.SMS.SMSType", | ||
new MessageAttributeValue().withStringValue("Promotional").withDataType("String")); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
adapters/notification/adapter-aws-sns/src/main/java/com/caregiver/config/AwsProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.caregiver.config; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* SNS 서비스를 사용하기위한 properties 클래스입니다. | ||
*/ | ||
@Getter | ||
@Component | ||
public class AwsProperties { | ||
|
||
@Value("${caregiver.aws.access-key:default-aws-access-key}") | ||
private String accessKey; | ||
|
||
@Value("${caregiver.aws.secret-key:default-aws-secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${caregiver.aws.signing-region}") | ||
private String signingRegion; | ||
|
||
@Value("${caregiver.aws.sns.arn}") | ||
private String arn; | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
.../notification/adapter-aws-sns/src/main/java/com/caregiver/config/AwsSnsClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.caregiver.config; | ||
|
||
|
||
import com.amazonaws.services.sns.AmazonSNS; | ||
|
||
/** | ||
* <p> AWS SNS 를 사용하기 위한 객체를 생성합니다.</p> | ||
* Spring profile 구분 별로 다른 자격 증명 방식을 사용하고 있습니다. | ||
* 기본 자격 증명 방식 과 IAM ROLE 자격증명 방식을 사용합니다. | ||
* | ||
* @see <a href="https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html"> | ||
* Setting Credentials</a> | ||
*/ | ||
public interface AwsSnsClientFactory { | ||
|
||
/** | ||
* SNS 서비스 클라이언트 AmazonSNS 리턴합니다. | ||
*/ | ||
AmazonSNS createSnsClient(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...s/notification/adapter-aws-sns/src/test/java/com/caregiver/AdapterSnsApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.caregiver; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* SNS Application Junit Test를 실행하기 위한 클래스. | ||
*/ | ||
@SpringBootApplication | ||
public class AdapterSnsApplicationTest { | ||
} |
20 changes: 20 additions & 0 deletions
20
...rs/notification/adapter-aws-sns/src/test/java/com/caregiver/common/BaseConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.caregiver.common; | ||
|
||
import com.caregiver.config.AwsDefaultCredential; | ||
import com.caregiver.config.AwsProperties; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
/** | ||
* 테스트를 클래스에서 공통으로 필요로하는 정보를 관리하는 클래스. | ||
*/ | ||
@ActiveProfiles("test") | ||
@ExtendWith(SpringExtension.class) | ||
@ContextConfiguration(classes = {AwsProperties.class, AwsDefaultCredential.class}, | ||
initializers = ConfigFileApplicationContextInitializer.class) | ||
public class BaseConfiguration { | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...rs/notification/adapter-aws-sns/src/test/java/com/caregiver/config/AwsPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.caregiver.config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.amazonaws.regions.Regions; | ||
import com.caregiver.common.BaseConfiguration; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
@SuppressWarnings("NonAsciiCharacters") | ||
class AwsPropertiesTest extends BaseConfiguration { | ||
|
||
@Autowired | ||
AwsProperties awsProperties; | ||
|
||
@Test | ||
@DisplayName("Aws 설정정보를 확인하라") | ||
public void Aws_설정정보를_확인하라() { | ||
|
||
assertThat(awsProperties.getSigningRegion()).isEqualTo(Regions.AP_NORTHEAST_1.getName()); | ||
assertThat(awsProperties.getArn()).isNotBlank(); | ||
assertThat(awsProperties.getAccessKey()).isNotBlank(); | ||
assertThat(awsProperties.getSecretKey()).isNotBlank(); | ||
|
||
} | ||
|
||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...ters/notification/adapter-aws-sns/src/test/java/com/caregiver/sms/SendSmsMessageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.caregiver.sms; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.amazonaws.services.sns.AmazonSNS; | ||
import com.amazonaws.services.sns.model.MessageAttributeValue; | ||
import com.amazonaws.services.sns.model.PublishRequest; | ||
import com.amazonaws.services.sns.model.PublishResult; | ||
import com.caregiver.common.BaseConfiguration; | ||
import com.caregiver.config.AwsDefaultCredential; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
/** | ||
* 문자메세지 발송 테스트. | ||
*/ | ||
@SuppressWarnings({"NonAsciiCharacters", "CheckStyle"}) | ||
public class SendSmsMessageTest extends BaseConfiguration { | ||
|
||
@Autowired | ||
AwsDefaultCredential awsDefaultCredential; | ||
|
||
/** | ||
* 테스트가 실행될 경우 SMS 문자 메세지를 발송합니다. | ||
* 과금이 발생 요소가 있기에 Disabled 처리합니다. | ||
* TODO 테스트 코드 목킹 그리고 리팩토링 | ||
*/ | ||
@Test | ||
@Disabled | ||
@DisplayName("aws Sms 메일 발송을 테스트하라") | ||
public void aws_sms_메일_발송을_테스트하라() { | ||
|
||
final var amazonSns = awsDefaultCredential.createSnsClient(); | ||
|
||
final var publishResult = sendSmsMessage(amazonSns, "안녕하세요 이지훈입니다. 문자메세지 테스트", | ||
"+8201093793259", awsDefaultCredential.messageAttributeValue()); | ||
|
||
assertThat(publishResult.getMessageId()).isNotBlank(); | ||
} | ||
|
||
private PublishResult sendSmsMessage(AmazonSNS amazonSns, String message, String phoneNumber, | ||
Map<String, MessageAttributeValue> smsAttributes) { | ||
|
||
return amazonSns.publish(new PublishRequest().withMessage(message) | ||
.withPhoneNumber(phoneNumber) | ||
.withMessageAttributes(smsAttributes)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters