Добавлены тесты для подписок

This commit is contained in:
Никита Потапов 2024-04-01 17:57:28 +04:00
parent a80766ce83
commit 59262cd474
2 changed files with 152 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import com.example.nekontakte.core.errors.NotFoundException;
import com.example.nekontakte.subscribes.model.SubscribeEntity;
import com.example.nekontakte.subscribes.repository.SubscribeRepository;
import com.example.nekontakte.users.model.UserEntity;
@Service
public class SubscribeService {
@ -21,6 +22,10 @@ public class SubscribeService {
return repository.getAll();
}
public List<SubscribeEntity> getAllSubscribesByUser(UserEntity user) {
return repository.getAll().stream().filter(item -> item.getUser().equals(user)).toList();
}
public SubscribeEntity get(Integer id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}

View File

@ -0,0 +1,147 @@
package com.example.nekontakte;
import org.junit.jupiter.api.TestMethodOrder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import org.apache.coyote.BadRequestException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.nekontakte.core.errors.NotFoundException;
import com.example.nekontakte.subscribes.model.SubscribeEntity;
import com.example.nekontakte.subscribes.service.SubscribeService;
import com.example.nekontakte.users.service.UserService;
import com.example.nekontakte.users.model.UserEntity;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class SubscribeServiceTests {
@Autowired
private UserService userService;
@Autowired
private SubscribeService subscribeService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> subscribeService.get(0));
}
@Test
@Order(1)
void createTest() throws ParseException, BadRequestException {
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH);
final UserEntity firstUser = userService.create(new UserEntity(
null,
"nspotapov",
"pass123456",
true,
"Никита",
"Потапов",
dateFormatter.parse("17.02.2003"),
"Ульяновск",
null,
"Я здесь админ"));
final UserEntity secondUser = userService.create(new UserEntity(
null,
"ekallin",
"pass87654321",
false,
"Елена",
"Каллин",
dateFormatter.parse("14.06.2005"),
"Новоульяновск",
null,
null));
final UserEntity thirdUser = userService.create(new UserEntity(
null,
"olegulya",
"passOleg",
false,
"Олег",
"Тинькофф",
dateFormatter.parse("25.12.1967"),
"Полысаево",
null,
"Вчера я потерял $250 млн за день, были дни и похуже, миллиарды в день терял. Поэтому это позитивный очень день"));
subscribeService.create(new SubscribeEntity(null, firstUser, secondUser));
final SubscribeEntity secondSubscribe = subscribeService.create(
new SubscribeEntity(null, firstUser, thirdUser));
subscribeService.create(new SubscribeEntity(null, secondUser, thirdUser));
Assertions.assertEquals(secondSubscribe, subscribeService.get(2));
Assertions.assertEquals(3, subscribeService.getAll().size());
Assertions.assertEquals(2, subscribeService.getAllSubscribesByUser(firstUser).size());
}
@Test
@Order(2)
void update() throws BadRequestException, ParseException {
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH);
final UserEntity firstUser = userService.create(new UserEntity(
null,
"nspotapov",
"pass123456",
true,
"Никита",
"Потапов",
dateFormatter.parse("17.02.2003"),
"Ульяновск",
null,
"Я здесь админ"));
final UserEntity secondUser = userService.create(new UserEntity(
null,
"ekallin",
"pass87654321",
false,
"Елена",
"Каллин",
dateFormatter.parse("14.06.2005"),
"Новоульяновск",
null,
null));
final UserEntity thirdUser = userService.create(new UserEntity(
null,
"olegulya",
"passOleg",
false,
"Олег",
"Тинькофф",
dateFormatter.parse("25.12.1967"),
"Полысаево",
null,
"Вчера я потерял $250 млн за день, были дни и похуже, миллиарды в день терял. Поэтому это позитивный очень день"));
subscribeService.create(new SubscribeEntity(null, firstUser, secondUser));
Integer subscribeId = 1;
SubscribeEntity oldSubscribeEntity = subscribeService.get(subscribeId);
UserEntity oldSubscriber = oldSubscribeEntity.getSubscriber();
SubscribeEntity newSubscribeEntity = new SubscribeEntity(
oldSubscribeEntity.getId(),
oldSubscribeEntity.getUser(),
thirdUser);
subscribeService.update(subscribeId, newSubscribeEntity);
Assertions.assertNotEquals(subscribeService.get(subscribeId).getSubscriber(), oldSubscriber);
}
@Test
@Order(3)
void delete() {
Integer oldCount = subscribeService.getAll().size();
subscribeService.delete(1);
Assertions.assertEquals(oldCount - 1, subscribeService.getAll().size());
}
}