Skip to content

Commit

Permalink
Directly injecting test rest template in test classes through constru…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
harishkannarao committed Feb 19, 2024
1 parent 80d0d58 commit 80e4977
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.harishkannarao.springsecurityrestapi.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AdditionalConfiguration {

@Bean
protected TestRestTemplate testRestTemplate(
@Value("${test.application.url}") String testApplicationUrl
) {
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder()
.rootUri(testApplicationUrl);
return new TestRestTemplate(restTemplateBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.harishkannarao.springsecurityrestapi.AbstractBaseFtIntegrationTestProfile;
import com.harishkannarao.springsecurityrestapi.domain.UserData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -12,12 +14,19 @@

public class UserDataBetaRestControllerEnabledIntegrationTest extends AbstractBaseFtIntegrationTestProfile {

private final TestRestTemplate testRestTemplate;

@Autowired
public UserDataBetaRestControllerEnabledIntegrationTest(TestRestTemplate testRestTemplate) {
this.testRestTemplate = testRestTemplate;
}

@Test
public void test_getUserData() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth("user-token");
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<UserData> result = testRestTemplate()
ResponseEntity<UserData> result = testRestTemplate
.exchange("/beta/user-data", HttpMethod.GET, requestEntity, UserData.class);

assertThat(result.getStatusCode().value()).isEqualTo(200);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.harishkannarao.springsecurityrestapi.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -20,4 +23,13 @@ public Consumer<HttpSecurity> testEndpointCustomizer() {
}
};
}

@Bean
protected TestRestTemplate testRestTemplate(
@Value("${test.application.url}") String testApplicationUrl
) {
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder()
.rootUri(testApplicationUrl);
return new TestRestTemplate(restTemplateBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.harishkannarao.springsecurityrestapi.AbstractBaseIntegrationTestProfile;
import com.harishkannarao.springsecurityrestapi.domain.UserData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -14,12 +16,19 @@

public class AdminRestControllerIntegrationTest extends AbstractBaseIntegrationTestProfile {

private final TestRestTemplate testRestTemplate;

@Autowired
public AdminRestControllerIntegrationTest(TestRestTemplate testRestTemplate) {
this.testRestTemplate = testRestTemplate;
}

@Test
public void test_getUserData_returns200_forAdminUser() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth("admin-token");
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<UserData> result = testRestTemplate()
ResponseEntity<UserData> result = testRestTemplate
.exchange("/admin/get-user-data/{username}", HttpMethod.GET, requestEntity, UserData.class, Map.of("username", "user-name-1"));

assertThat(result.getStatusCode().value()).isEqualTo(200);
Expand All @@ -34,7 +43,7 @@ public void test_getUserData_returns400_forNonExistentUsername_forAdminUser() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth("admin-token");
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<Void> result = testRestTemplate()
ResponseEntity<Void> result = testRestTemplate
.exchange("/admin/get-user-data/{username}", HttpMethod.GET, requestEntity, Void.class, Map.of("username", "non-existent-user-name"));

assertThat(result.getStatusCode().value()).isEqualTo(400);
Expand All @@ -45,7 +54,7 @@ public void test_getUserData_returns403_forNonAdminUser() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth("user-token");
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<Void> result = testRestTemplate()
ResponseEntity<Void> result = testRestTemplate
.exchange("/admin/get-user-data/{username}", HttpMethod.GET, requestEntity, Void.class, Map.of("username", "user-name-1"));

assertThat(result.getStatusCode().value()).isEqualTo(403);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.harishkannarao.springsecurityrestapi.AbstractBaseIntegrationTestProfile;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -11,9 +13,16 @@

public class FallBackRestControllerIntegrationTest extends AbstractBaseIntegrationTestProfile {

private final TestRestTemplate testRestTemplate;

@Autowired
public FallBackRestControllerIntegrationTest(TestRestTemplate testRestTemplate) {
this.testRestTemplate = testRestTemplate;
}

@Test
public void test_nonExistentEndpoint_return401_forUnAuthenticatedRequest() {
ResponseEntity<Void> result = testRestTemplate()
ResponseEntity<Void> result = testRestTemplate
.getForEntity("/non-existent-api/some-path", Void.class);
assertThat(result.getStatusCode().value()).isEqualTo(401);
}
Expand All @@ -23,7 +32,7 @@ public void test_nonExistentEndpoint_return403_forAuthenticatedRequest() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth("user-token");
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<Void> result = testRestTemplate()
ResponseEntity<Void> result = testRestTemplate
.exchange("/non-existent-api/some-path", HttpMethod.GET, requestEntity, Void.class);

assertThat(result.getStatusCode().value()).isEqualTo(403);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
import com.harishkannarao.springsecurityrestapi.AbstractBaseIntegrationTestProfile;
import com.harishkannarao.springsecurityrestapi.domain.GeneralData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

public class GeneralDataRestControllerIntegrationTest extends AbstractBaseIntegrationTestProfile {

private final TestRestTemplate testRestTemplate;

@Autowired
public GeneralDataRestControllerIntegrationTest(TestRestTemplate testRestTemplate) {
this.testRestTemplate = testRestTemplate;
}

@Test
public void test_getGeneralData() {
ResponseEntity<GeneralData> result = testRestTemplate()
ResponseEntity<GeneralData> result = testRestTemplate
.getForEntity("/general-data", GeneralData.class);
assertThat(result.getStatusCode().value()).isEqualTo(200);
GeneralData actualEntity = result.getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

import com.harishkannarao.springsecurityrestapi.AbstractBaseIntegrationTestProfile;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

public class TestEndpointRestControllerIntegrationTest extends AbstractBaseIntegrationTestProfile {

private final TestRestTemplate testRestTemplate;

@Autowired
public TestEndpointRestControllerIntegrationTest(TestRestTemplate testRestTemplate) {
this.testRestTemplate = testRestTemplate;
}

@Test
public void testEndpoints_returns204() {
ResponseEntity<Void> result = testRestTemplate()
ResponseEntity<Void> result = testRestTemplate
.getForEntity("/test-endpoint", Void.class);

assertThat(result.getStatusCode().value()).isEqualTo(204);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.harishkannarao.springsecurityrestapi.AbstractBaseIntegrationTestProfile;
import com.harishkannarao.springsecurityrestapi.domain.UserData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -12,12 +14,19 @@

public class UserDataBetaRestControllerDisabledIntegrationTest extends AbstractBaseIntegrationTestProfile {

private final TestRestTemplate testRestTemplate;

@Autowired
public UserDataBetaRestControllerDisabledIntegrationTest(TestRestTemplate testRestTemplate) {
this.testRestTemplate = testRestTemplate;
}

@Test
public void test_apiDisabled_andReturns403_forAuthenticatedRequest() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth("user-token");
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<UserData> result = testRestTemplate()
ResponseEntity<UserData> result = testRestTemplate
.exchange("/beta/user-data", HttpMethod.GET, requestEntity, UserData.class);

assertThat(result.getStatusCode().value()).isEqualTo(403);
Expand All @@ -27,7 +36,7 @@ public void test_apiDisabled_andReturns403_forAuthenticatedRequest() {
public void test_apiDisabled_andReturns401_forUnAuthenticatedRequest() {
HttpHeaders requestHeaders = new HttpHeaders();
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<UserData> result = testRestTemplate()
ResponseEntity<UserData> result = testRestTemplate
.exchange("/beta/user-data", HttpMethod.GET, requestEntity, UserData.class);

assertThat(result.getStatusCode().value()).isEqualTo(401);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.harishkannarao.springsecurityrestapi.AbstractBaseIntegrationTestProfile;
import com.harishkannarao.springsecurityrestapi.domain.UserData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -12,12 +14,19 @@

public class UserDataRestControllerIntegrationTest extends AbstractBaseIntegrationTestProfile {

private final TestRestTemplate testRestTemplate;

@Autowired
public UserDataRestControllerIntegrationTest(TestRestTemplate testRestTemplate) {
this.testRestTemplate = testRestTemplate;
}

@Test
public void test_getUserData() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setBearerAuth("user-token");
HttpEntity<Void> requestEntity = new HttpEntity<>(requestHeaders);
ResponseEntity<UserData> result = testRestTemplate()
ResponseEntity<UserData> result = testRestTemplate
.exchange("/user-data", HttpMethod.GET, requestEntity, UserData.class);

assertThat(result.getStatusCode().value()).isEqualTo(200);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.harishkannarao.springsecurityrestapi;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.util.TestSocketUtils;
Expand All @@ -18,17 +15,4 @@ static void registerTestProperties(DynamicPropertyRegistry registry) {
final int RANDOM_SERVER_PORT = TestSocketUtils.findAvailableTcpPort();
registry.add("server.port", () -> String.valueOf(RANDOM_SERVER_PORT));
}

@Value("${test.application.url}")
private String testApplicationUrl;

protected String getTestApplicationUrl() {
return testApplicationUrl;
}

protected TestRestTemplate testRestTemplate() {
RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder()
.rootUri(testApplicationUrl);
return new TestRestTemplate(restTemplateBuilder);
}
}

0 comments on commit 80e4977

Please sign in to comment.