Skip to content

Commit

Permalink
Changed to use @AutoConfigureMockMvc
Browse files Browse the repository at this point in the history
  • Loading branch information
softwaremagico committed Oct 12, 2024
1 parent 1d3fbc7 commit 95061b3
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -57,6 +58,7 @@

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@Test(groups = "participantAccess")
public class ParticipantAccessTests extends AbstractTestNGSpringContextTests {

Expand All @@ -75,9 +77,6 @@ public class ParticipantAccessTests extends AbstractTestNGSpringContextTests {
@Autowired
private ObjectMapper objectMapper;

@Autowired
private WebApplicationContext context;

@Autowired
private AuthenticatedUserController authenticatedUserController;

Expand All @@ -87,6 +86,7 @@ public class ParticipantAccessTests extends AbstractTestNGSpringContextTests {
@Autowired
private ParticipantProvider participantProvider;

@Autowired
private MockMvc mockMvc;

private String jwtToken;
Expand All @@ -107,10 +107,6 @@ private <T> T fromJson(String payload, Class<T> clazz) throws IOException {

@BeforeClass
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();

authenticatedUserController.createUser(null, USER_NAME, USER_FIRST_NAME, USER_LAST_NAME, USER_PASSWORD, USER_ROLES);

AuthRequest request = new AuthRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@
import com.softwaremagico.kt.rest.controllers.AuthenticatedUserController;
import com.softwaremagico.kt.rest.security.dto.AuthRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand All @@ -78,6 +75,7 @@


@SpringBootTest
@AutoConfigureMockMvc
@Test(groups = {"restSimpleChampionshipTest"})
public class RestSimpleChampionshipTest extends AbstractTestNGSpringContextTests {

Expand All @@ -95,18 +93,12 @@ public class RestSimpleChampionshipTest extends AbstractTestNGSpringContextTests

private static final String CLUB_CITY = "Valencia";

@Autowired
private WebApplicationContext context;

@Autowired
private ObjectMapper objectMapper;

@Autowired
private AuthenticatedUserController authenticatedUserController;

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private GroupController groupController;

Expand Down Expand Up @@ -134,7 +126,7 @@ public class RestSimpleChampionshipTest extends AbstractTestNGSpringContextTests
@Autowired
private ClubController clubController;


@Autowired
private MockMvc mockMvc;

private String jwtToken;
Expand Down Expand Up @@ -179,13 +171,6 @@ private <T> T fromJson(String payload, Class<T> clazz) throws IOException {
}

@BeforeClass
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
}

@BeforeClass(dependsOnMethods = "setUp")
public void setAuthentication() throws Exception {
//Create the admin user
authenticatedUserController.createUser(null, USER_NAME, USER_FIRST_NAME, USER_LAST_NAME, USER_PASSWORD, USER_ROLES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -54,6 +55,7 @@

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@Test(groups = "userRegister")
public class UserRegisterTests extends AbstractTestNGSpringContextTests {

Expand All @@ -68,10 +70,8 @@ public class UserRegisterTests extends AbstractTestNGSpringContextTests {

private static final String CLUB_CITY = "Valencia";

private MockMvc mockMvc;

@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;
Expand All @@ -94,10 +94,6 @@ private <T> T fromJson(String payload, Class<T> clazz) throws IOException {

@BeforeClass
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();

authenticatedUserController.createUser(null, USER_NAME, USER_FIRST_NAME, USER_LAST_NAME, USER_PASSWORD, USER_ROLES);

AuthRequest request = new AuthRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -51,6 +52,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

@SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureMockMvc
@ExtendWith(MockitoExtension.class)
@Test(groups = "bruteForce")
public class BruteForceTests extends AbstractTestNGSpringContextTests {
Expand All @@ -59,10 +61,8 @@ public class BruteForceTests extends AbstractTestNGSpringContextTests {
private static final String USER_LAST_NAME = "User";
private static final String USER_PASSWORD = "password";

private MockMvc mockMvc;

@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;
Expand All @@ -81,13 +81,6 @@ private <T> T fromJson(String payload, Class<T> clazz) throws IOException {
return objectMapper.readValue(payload, clazz);
}

@BeforeClass
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
}

@BeforeMethod
public void clearDatabase() {
authenticatedUserController.findAll().forEach(authenticatedUser -> authenticatedUserController.delete(authenticatedUser));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* #L%
Expand All @@ -24,6 +24,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
Expand All @@ -46,20 +47,13 @@

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@Test(groups = "restAccess")
public class RestAccessTests extends AbstractTestNGSpringContextTests {

@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;

@BeforeClass
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity())
.build();
}

@Test
public void checkPublicRestService() throws Exception {
//Info services are opened in rest-server library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -56,6 +57,7 @@

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@Test(groups = "authApi")
public class TestAuthApi extends AbstractTestNGSpringContextTests {
private static final String USER_NAME = "user";
Expand All @@ -75,10 +77,8 @@ public class TestAuthApi extends AbstractTestNGSpringContextTests {
private static final String USER2_NEW_FIRST_NAME = "New Test2";
private static final String USER2_NEW_LAST_NAME = "New User2";

private MockMvc mockMvc;

@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;
Expand All @@ -101,10 +101,6 @@ private <T> T fromJson(String payload, Class<T> clazz) throws IOException {

@BeforeClass
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();

authenticatedUserController.createUser(null, USER_NAME, USER_FIRST_NAME, USER_LAST_NAME, USER_PASSWORD, USER_ROLES);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -56,6 +57,7 @@

@SpringBootTest(webEnvironment = RANDOM_PORT)
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@Test(groups = "userRegistration")
public class UserRegistrationTest extends AbstractTestNGSpringContextTests {
public static final String USER_NAME_2 = "Goku";
Expand All @@ -68,7 +70,6 @@ public class UserRegistrationTest extends AbstractTestNGSpringContextTests {
private static final String[] USER_ROLES = new String[] {"admin", "viewer"};

@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;


Expand Down Expand Up @@ -96,10 +97,6 @@ private <T> T fromJson(String payload, Class<T> clazz) throws IOException {

@BeforeClass
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();

authenticatedUserController.findAll().forEach(authenticatedUser -> authenticatedUserController.delete(authenticatedUser));
}

Expand Down

0 comments on commit 95061b3

Please sign in to comment.