generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into PUB-2618-Dependency-Updates
- Loading branch information
Showing
25 changed files
with
359 additions
and
142 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
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
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
43 changes: 43 additions & 0 deletions
43
src/functionalTest/java/uk/gov/hmcts/pip/publication/services/NotificationEmailTests.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,43 @@ | ||
package uk.gov.hmcts.pip.publication.services; | ||
|
||
import io.restassured.response.Response; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import uk.gov.hmcts.pip.publication.services.utils.FunctionalTestBase; | ||
import uk.gov.hmcts.pip.publication.services.utils.OAuthClient; | ||
import uk.gov.hmcts.reform.pip.publication.services.Application; | ||
import uk.gov.hmcts.reform.pip.publication.services.models.request.WelcomeEmail; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.springframework.http.HttpHeaders.AUTHORIZATION; | ||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(classes = {Application.class, OAuthClient.class}, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ActiveProfiles(profiles = "functional") | ||
class NotificationEmailTests extends FunctionalTestBase { | ||
private static final String BEARER = "Bearer "; | ||
|
||
private static final String NOTIFY_URL = "/notify"; | ||
private static final String MEDIA_WELCOME_EMAIL_URL = NOTIFY_URL + "/welcome-email"; | ||
|
||
private static final String TEST_EMAIL = "test_user@justice.gov.uk"; | ||
private static final String TEST_FULL_NAME = "test user"; | ||
|
||
@Test | ||
void shouldSendMediaWelcomeEmailForNewUser() { | ||
WelcomeEmail requestBody = new WelcomeEmail(TEST_EMAIL, false, TEST_FULL_NAME); | ||
|
||
final Response response = doPostRequest(MEDIA_WELCOME_EMAIL_URL, | ||
Map.of(AUTHORIZATION, BEARER + accessToken), | ||
requestBody); | ||
|
||
assertThat(response.getStatusCode()).isEqualTo(OK.value()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/functionalTest/java/uk/gov/hmcts/pip/publication/services/utils/AuthException.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,9 @@ | ||
package uk.gov.hmcts.pip.publication.services.utils; | ||
|
||
public class AuthException extends RuntimeException { | ||
private static final long serialVersionUID = -326686171637352006L; | ||
|
||
public AuthException(String error) { | ||
super(error); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/functionalTest/java/uk/gov/hmcts/pip/publication/services/utils/FunctionalTestBase.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,56 @@ | ||
package uk.gov.hmcts.pip.publication.services.utils; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.response.Response; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.util.CollectionUtils; | ||
import uk.gov.hmcts.reform.pip.publication.services.Application; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.springframework.http.HttpHeaders.CONTENT_TYPE; | ||
|
||
@SpringBootTest(classes = {Application.class, OAuthClient.class}, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class FunctionalTestBase extends RedisConfigurationTestBase { | ||
|
||
protected static final String CONTENT_TYPE_VALUE = "application/json"; | ||
|
||
@Autowired | ||
private OAuthClient authClient; | ||
|
||
protected String accessToken; | ||
|
||
@Value("${test-url}") | ||
private String testUrl; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
RestAssured.baseURI = testUrl; | ||
accessToken = authClient.generateAccessToken(); | ||
} | ||
|
||
protected Response doPostRequest(final String path, final Map<String, String> additionalHeaders, | ||
final Object body) { | ||
return given() | ||
.relaxedHTTPSValidation() | ||
.headers(getRequestHeaders(additionalHeaders)) | ||
.body(body) | ||
.when() | ||
.post(path) | ||
.thenReturn(); | ||
} | ||
|
||
private static Map<String, String> getRequestHeaders(final Map<String, String> additionalHeaders) { | ||
final Map<String, String> headers = new ConcurrentHashMap<>(Map.of(CONTENT_TYPE, CONTENT_TYPE_VALUE)); | ||
if (!CollectionUtils.isEmpty(additionalHeaders)) { | ||
headers.putAll(additionalHeaders); | ||
} | ||
return headers; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/functionalTest/java/uk/gov/hmcts/pip/publication/services/utils/OAuthClient.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,43 @@ | ||
package uk.gov.hmcts.pip.publication.services.utils; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
@Component | ||
public class OAuthClient { | ||
|
||
@Value("${CLIENT_ID_FT}") | ||
private String clientId; | ||
|
||
@Value("${CLIENT_SECRET_FT}") | ||
private String clientSecret; | ||
|
||
@Value("${TENANT_ID}") | ||
private String tenantId; | ||
|
||
@Value("${APP_URI}") | ||
private String scope; | ||
|
||
public String generateAccessToken() { | ||
String token = given() | ||
.relaxedHTTPSValidation() | ||
.header("content-type", "application/x-www-form-urlencoded") | ||
.formParam("client_id", clientId) | ||
.formParam("scope", scope + "/.default") | ||
.formParam("client_secret", clientSecret) | ||
.formParam("grant_type", "client_credentials") | ||
.baseUri("https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token") | ||
.post() | ||
.body() | ||
.jsonPath() | ||
.get("access_token"); | ||
|
||
if (token == null) { | ||
throw new AuthException("Unable to generate access token for the API"); | ||
} | ||
return token; | ||
} | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
...onalTest/java/uk/gov/hmcts/pip/publication/services/utils/RedisConfigurationTestBase.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,25 @@ | ||
package uk.gov.hmcts.pip.publication.services.utils; | ||
|
||
import com.redis.testcontainers.RedisContainer; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@Testcontainers | ||
@SuppressWarnings("checkstyle:HideUtilityClassConstructorCheck") | ||
public class RedisConfigurationTestBase { | ||
private static final String REDIS_IMAGE_NAME = "redis:latest"; | ||
private static final int REDIS_PORT = 6379; | ||
|
||
@Container | ||
private static RedisContainer redisContainer = new RedisContainer(DockerImageName.parse(REDIS_IMAGE_NAME)) | ||
.withExposedPorts(REDIS_PORT); | ||
|
||
@DynamicPropertySource | ||
static void registerRedisProperties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.data.redis.host", redisContainer::getHost); | ||
registry.add("spring.data.redis.port", () -> redisContainer.getMappedPort(REDIS_PORT).toString()); | ||
} | ||
} |
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,7 @@ | ||
rate-limit: | ||
email: | ||
capacity: | ||
standard: 20 | ||
high: 50 | ||
interval-in-minutes: 1 | ||
test-url: ${TEST_URL:http://localhost:8081} |
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
Oops, something went wrong.