mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 18:37:47 +01:00
more tests
This commit is contained in:
@@ -7,6 +7,7 @@ import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
@Entity
|
||||
@@ -27,7 +28,7 @@ public class Person implements EntityWithId<String> {
|
||||
private String password;
|
||||
|
||||
@OneToMany(mappedBy = "author")
|
||||
private Collection<Post> posts;
|
||||
private Collection<Post> posts = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
|
||||
@@ -34,6 +34,7 @@ public class WebSecurityConfig {
|
||||
.csrf(AbstractHttpConfigurer::disable)
|
||||
.authorizeHttpRequests((authorize) -> authorize
|
||||
.requestMatchers(mvc.pattern(HttpMethod.GET, "/post/*")).permitAll()
|
||||
.requestMatchers(mvc.pattern(HttpMethod.GET, "/post*")).permitAll()
|
||||
.requestMatchers(mvc.pattern(HttpMethod.POST, "/person")).permitAll()
|
||||
.requestMatchers(mvc.pattern(HttpMethod.GET, "/person/*")).permitAll()
|
||||
.requestMatchers(mvc.pattern(HttpMethod.POST, "/token")).permitAll()
|
||||
|
||||
@@ -8,8 +8,4 @@ public class UserAlreadyExistsException extends Exception {
|
||||
public UserAlreadyExistsException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserAlreadyExistsException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.test.context.transaction.TestTransaction;
|
||||
import org.springframework.test.jdbc.JdbcTestUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
@@ -56,6 +57,7 @@ public abstract class DemoDataDbTest {
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
assert !TestTransaction.isActive();
|
||||
person1 = personRepository.save(
|
||||
new Person()
|
||||
.setUsername("person1")
|
||||
@@ -75,6 +77,7 @@ public abstract class DemoDataDbTest {
|
||||
|
||||
@AfterEach
|
||||
void erase() {
|
||||
assert !TestTransaction.isActive();
|
||||
JdbcTestUtils.deleteFromTables(jdbcTemplate, "post", "person");
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.usatiuk.tjv.y.server.controller;
|
||||
|
||||
import com.usatiuk.tjv.y.server.dto.PostCreate;
|
||||
import com.usatiuk.tjv.y.server.dto.PostTo;
|
||||
import com.usatiuk.tjv.y.server.dto.converters.PostMapper;
|
||||
import com.usatiuk.tjv.y.server.repository.PostRepository;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -10,6 +11,8 @@ import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PostControllerTest extends DemoDataDbTest {
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
@@ -17,10 +20,11 @@ public class PostControllerTest extends DemoDataDbTest {
|
||||
@Test
|
||||
void shouldNotCreatePostWithoutAuth() {
|
||||
Long postsBefore = postRepository.count();
|
||||
var response = restTemplate.postForObject(addr + "/post",
|
||||
new PostCreate("test text"), PostTo.class);
|
||||
var response = restTemplate.exchange(addr + "/post", HttpMethod.POST,
|
||||
new HttpEntity<>(new PostCreate("test text")), PostTo.class);
|
||||
|
||||
Assertions.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
|
||||
Assertions.assertNull(response);
|
||||
Assertions.assertEquals(postRepository.count(), postsBefore);
|
||||
}
|
||||
|
||||
@@ -42,11 +46,30 @@ public class PostControllerTest extends DemoDataDbTest {
|
||||
|
||||
@Test
|
||||
void shouldGetPost() {
|
||||
PostTo response = restTemplate.getForObject(addr + "/post/" + post1.getId(), PostTo.class);
|
||||
var response = restTemplate.exchange(addr + "/post/" + post1.getId(), HttpMethod.GET,
|
||||
HttpEntity.EMPTY, PostTo.class);
|
||||
|
||||
Assertions.assertNotNull(response);
|
||||
Assertions.assertEquals(response.text(), post1.getText());
|
||||
Assertions.assertEquals(response.authorUuid(), person1.getUuid());
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
var parsedResponse = response.getBody();
|
||||
|
||||
Assertions.assertNotNull(parsedResponse);
|
||||
Assertions.assertEquals(parsedResponse.text(), post1.getText());
|
||||
Assertions.assertEquals(parsedResponse.authorUuid(), person1.getUuid());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGetByAuthor() {
|
||||
var response = restTemplate.exchange(addr + "/post?author=" + person1.getUuid(), HttpMethod.GET,
|
||||
HttpEntity.EMPTY, PostTo[].class);
|
||||
|
||||
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
var parsedResponse = response.getBody();
|
||||
var repoResponse = postRepository.findByAuthorUuid(person1.getUuid());
|
||||
|
||||
Assertions.assertNotNull(parsedResponse);
|
||||
Assertions.assertTrue(parsedResponse.length > 0);
|
||||
Assertions.assertEquals(parsedResponse.length, repoResponse.size());
|
||||
Assertions.assertIterableEquals(Arrays.asList(parsedResponse), repoResponse.stream().map(PostMapper::makeDto).toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user