Added project

This commit is contained in:
2023-06-11 00:23:46 +03:00
parent 7a8ca16c1f
commit 366ce44813
36 changed files with 2274 additions and 23 deletions

View File

@@ -0,0 +1,133 @@
package ru.resprojects.restsrv.service;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.junit4.SpringRunner;
import ru.resprojects.restsrv.RestsrvApplication;
import ru.resprojects.restsrv.exception.BadResourceException;
import ru.resprojects.restsrv.exception.ResourceAlreadyExistsException;
import ru.resprojects.restsrv.exception.ResourceNotFoundException;
import ru.resprojects.restsrv.model.Profile;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RestsrvApplication.class)
@ActiveProfiles(profiles = {"test"})
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
scripts = {"classpath:schema-h2.sql"},
config = @SqlConfig(encoding = "UTF-8"))
public class ProfileServiceTest {
@Autowired
private ProfileService profileService;
private Profile exampleProfile;
@Before
public void init() {
Profile profile = new Profile();
profile.setName("h2user1");
profile.setEmail("h2user1@example.com");
profile.setAge(20);
exampleProfile = profileService.save(profile);
}
@Test
public void whenSaveNewProfileThenReturnedProfileWithId() {
Profile exampleProfile = new Profile();
exampleProfile.setName("Alex");
exampleProfile.setEmail("alex@example.com");
exampleProfile.setAge(20);
Profile newProfile = profileService.save(exampleProfile);
assertThat(newProfile).isNotNull();
assertThat(newProfile.getId()).isNotNull();
}
@Test
public void whenFindByIdThenReturnedProfile() {
Profile profile = profileService.findById(5000);
assertThat(profile).isNotNull();
}
@Test
public void whenFindByEmailThenReturnedProfile() {
Profile profile = profileService.findByEmail("h2user1@example.com");
assertThat(profile).isNotNull();
}
@Test
public void whenFindByEmailCaseInsensitiveThenReturnedProfile() {
Profile profile = profileService.findByEmail("H2uSer1@eXAmple.com");
assertThat(profile).isNotNull();
}
@Test
public void whenGetLastCreatedProfileThenReturnedLastSavedProfile() {
Profile profile = profileService.getLastCreatedProfile();
assertThat(profile).isNotNull();
assertThat(profile).isEqualTo(exampleProfile);
}
@Test
public void whenFindAllThenReturnedAllProfiles() {
List<Profile> profiles = profileService.findAll();
assertThat(profiles).isNotEmpty();
}
@Test(expected = ResourceNotFoundException.class)
public void whenFindByNonexistentIdWhenException() {
profileService.findById(5010);
}
@Test(expected = ResourceNotFoundException.class)
public void whenFindByNonexistentEmailThenException() {
profileService.findByEmail("test@test.com");
}
@Test(expected = BadResourceException.class)
public void whenSaveProfileWithIncorrectEmailThenException() {
Profile exampleProfile = new Profile();
exampleProfile.setName("Alex");
exampleProfile.setEmail(".@example.com");
exampleProfile.setAge(20);
profileService.save(exampleProfile);
}
@Test(expected = ResourceAlreadyExistsException.class)
public void whenSaveProfileWithExistEmailThenException() {
Profile exampleProfile = new Profile();
exampleProfile.setName("Alex");
exampleProfile.setEmail("h2user1@example.com");
exampleProfile.setAge(20);
profileService.save(exampleProfile);
}
@Test(expected = ResourceAlreadyExistsException.class)
public void whenSaveProfileWithExistEmailCaseInsensitiveThenException() {
Profile exampleProfile = new Profile();
exampleProfile.setName("Alex");
exampleProfile.setEmail("H2user1@example.com");
exampleProfile.setAge(20);
profileService.save(exampleProfile);
}
}