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