test followers

This commit is contained in:
Stepan Usatiuk
2023-12-15 12:56:45 +01:00
parent 60a6068512
commit c99bfca6df
7 changed files with 65 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.DS_Store

0
server/gradlew vendored Normal file → Executable file
View File

View File

@@ -12,7 +12,9 @@ import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.security.Principal;
import java.util.Optional;
import java.util.stream.Stream;
@RestController
@RequestMapping(value = "/person", produces = MediaType.APPLICATION_JSON_VALUE)
@@ -44,4 +46,14 @@ public class PersonController {
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);
}
}

View File

@@ -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.service.exceptions.UserAlreadyExistsException;
import com.usatiuk.tjv.y.server.service.exceptions.UserNotFoundException;
import java.util.Collection;
import java.util.Optional;
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> readByUsername(String username);
Collection<Person> getFollowers(String uuid) throws UserNotFoundException;
Collection<Person> getFollowing(String uuid) throws UserNotFoundException;
}

View File

@@ -8,6 +8,7 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.Optional;
@Service
@@ -48,4 +49,14 @@ public class PersonServiceImpl extends CrudServiceImpl<Person, String> implement
public Optional<Person> readByUsername(String 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();
}
}

View File

@@ -20,6 +20,7 @@ import org.springframework.test.context.transaction.TestTransaction;
import org.springframework.test.jdbc.JdbcTestUtils;
import java.util.Collections;
import java.util.List;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class DemoDataDbTest {
@@ -77,7 +78,8 @@ public abstract class DemoDataDbTest {
new Person()
.setUsername("person3")
.setFullName("Person 3")
.setPassword(passwordEncoder.encode(person3Password)));
.setPassword(passwordEncoder.encode(person3Password))
.setFollowing(List.of(person2)));
person3Auth = new TokenResponse(tokenService.generateToken(person3.getUuid()));
post1 = postRepository.save(new Post().setAuthor(person1).setText("post 1"));
@@ -87,7 +89,7 @@ public abstract class DemoDataDbTest {
@AfterEach
void erase() {
assert !TestTransaction.isActive();
JdbcTestUtils.deleteFromTables(jdbcTemplate, "post", "person");
JdbcTestUtils.deleteFromTables(jdbcTemplate, "user_follows", "post", "person");
}
}

View File

@@ -46,4 +46,35 @@ public class PersonControllerTest extends DemoDataDbTest {
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());
}
}