mirror of
https://github.com/usatiuk/y.git
synced 2025-10-28 10:37:47 +01:00
test followers
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.DS_Store
|
||||||
0
server/gradlew
vendored
Normal file → Executable file
0
server/gradlew
vendored
Normal file → Executable file
@@ -12,7 +12,9 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.server.ResponseStatusException;
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value = "/person", produces = MediaType.APPLICATION_JSON_VALUE)
|
@RequestMapping(value = "/person", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
@@ -44,4 +46,14 @@ public class PersonController {
|
|||||||
return PersonMapper.makeDto(found.get());
|
return PersonMapper.makeDto(found.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping(path = "/followers")
|
||||||
|
public Stream<PersonTo> getFollowers(Principal principal) throws UserNotFoundException {
|
||||||
|
return personService.getFollowers(principal.getName()).stream().map(PersonMapper::makeDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(path = "/following")
|
||||||
|
public Stream<PersonTo> getFollowing(Principal principal) throws UserNotFoundException {
|
||||||
|
return personService.getFollowing(principal.getName()).stream().map(PersonMapper::makeDto);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package com.usatiuk.tjv.y.server.service;
|
|||||||
|
|
||||||
import com.usatiuk.tjv.y.server.entity.Person;
|
import com.usatiuk.tjv.y.server.entity.Person;
|
||||||
import com.usatiuk.tjv.y.server.service.exceptions.UserAlreadyExistsException;
|
import com.usatiuk.tjv.y.server.service.exceptions.UserAlreadyExistsException;
|
||||||
|
import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface PersonService extends CrudService<Person, String> {
|
public interface PersonService extends CrudService<Person, String> {
|
||||||
@@ -11,4 +13,8 @@ public interface PersonService extends CrudService<Person, String> {
|
|||||||
Optional<Person> login(String username, String password);
|
Optional<Person> login(String username, String password);
|
||||||
|
|
||||||
Optional<Person> readByUsername(String username);
|
Optional<Person> readByUsername(String username);
|
||||||
|
|
||||||
|
Collection<Person> getFollowers(String uuid) throws UserNotFoundException;
|
||||||
|
|
||||||
|
Collection<Person> getFollowing(String uuid) throws UserNotFoundException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.springframework.data.repository.CrudRepository;
|
|||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -48,4 +49,14 @@ public class PersonServiceImpl extends CrudServiceImpl<Person, String> implement
|
|||||||
public Optional<Person> readByUsername(String username) {
|
public Optional<Person> readByUsername(String username) {
|
||||||
return personRepository.findByUsername(username);
|
return personRepository.findByUsername(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Person> getFollowers(String uuid) throws UserNotFoundException {
|
||||||
|
return personRepository.findById(uuid).orElseThrow(UserNotFoundException::new).getFollowers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Person> getFollowing(String uuid) throws UserNotFoundException {
|
||||||
|
return personRepository.findById(uuid).orElseThrow(UserNotFoundException::new).getFollowing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ 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;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
public abstract class DemoDataDbTest {
|
public abstract class DemoDataDbTest {
|
||||||
@@ -77,7 +78,8 @@ public abstract class DemoDataDbTest {
|
|||||||
new Person()
|
new Person()
|
||||||
.setUsername("person3")
|
.setUsername("person3")
|
||||||
.setFullName("Person 3")
|
.setFullName("Person 3")
|
||||||
.setPassword(passwordEncoder.encode(person3Password)));
|
.setPassword(passwordEncoder.encode(person3Password))
|
||||||
|
.setFollowing(List.of(person2)));
|
||||||
person3Auth = new TokenResponse(tokenService.generateToken(person3.getUuid()));
|
person3Auth = new TokenResponse(tokenService.generateToken(person3.getUuid()));
|
||||||
|
|
||||||
post1 = postRepository.save(new Post().setAuthor(person1).setText("post 1"));
|
post1 = postRepository.save(new Post().setAuthor(person1).setText("post 1"));
|
||||||
@@ -87,7 +89,7 @@ public abstract class DemoDataDbTest {
|
|||||||
@AfterEach
|
@AfterEach
|
||||||
void erase() {
|
void erase() {
|
||||||
assert !TestTransaction.isActive();
|
assert !TestTransaction.isActive();
|
||||||
JdbcTestUtils.deleteFromTables(jdbcTemplate, "post", "person");
|
JdbcTestUtils.deleteFromTables(jdbcTemplate, "user_follows", "post", "person");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,4 +46,35 @@ public class PersonControllerTest extends DemoDataDbTest {
|
|||||||
Assertions.assertEquals(personToResponse.fullName(), person1.getFullName());
|
Assertions.assertEquals(personToResponse.fullName(), person1.getFullName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldGetFollowers() {
|
||||||
|
var response = restTemplate.exchange(addr + "/person/followers",
|
||||||
|
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person2Auth)), PersonTo[].class);
|
||||||
|
|
||||||
|
Assertions.assertNotNull(response);
|
||||||
|
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
|
||||||
|
PersonTo[] personToResponse = response.getBody();
|
||||||
|
Assertions.assertNotNull(personToResponse);
|
||||||
|
|
||||||
|
Assertions.assertEquals(1, personToResponse.length);
|
||||||
|
Assertions.assertEquals(personToResponse[0].fullName(), person3.getFullName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldGetFollowees() {
|
||||||
|
var response = restTemplate.exchange(addr + "/person/following",
|
||||||
|
HttpMethod.GET, new HttpEntity<>(createAuthHeaders(person3Auth)), PersonTo[].class);
|
||||||
|
|
||||||
|
Assertions.assertNotNull(response);
|
||||||
|
Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
|
||||||
|
PersonTo[] personToResponse = response.getBody();
|
||||||
|
Assertions.assertNotNull(personToResponse);
|
||||||
|
|
||||||
|
Assertions.assertEquals(1, personToResponse.length);
|
||||||
|
Assertions.assertEquals(personToResponse[0].fullName(), person2.getFullName());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user