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,44 @@
package ru.resprojects.restsrv;
import org.junit.Test;
import ru.resprojects.restsrv.util.ValidationUtil;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class ValidationUtilTest {
@Test
public void whenPassCorrectEmailToValidatorThenReturnTrue() {
List<String> emails = List.of(
"john@somewhere.com",
"john.foo@somewhere.com",
"john.foo+label@somewhere.com",
"john@192.168.1.10",
"john+label@192.168.1.10",
"john.foo@someserver",
"JOHN.FOO@somewhere.com"
);
for (String email : emails) {
assertThat(ValidationUtil.isEmailValid(email)).isTrue();
}
}
@Test
public void whenPassIncorrectEmailToValidatorThenReturnFalse() {
List<String> emails = List.of(
"@someserver",
"@someserver.com",
"john@.",
".@somewhere.com",
".@.somewhere.com"
);
for (String email : emails) {
assertThat(ValidationUtil.isEmailValid(email)).isFalse();
}
}
}

View File

@@ -0,0 +1,196 @@
package ru.resprojects.restsrv.controller;
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.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.config.TokenRequestsFilter;
import ru.resprojects.restsrv.dto.EmailDto;
import ru.resprojects.restsrv.dto.ProfileDto;
import ru.resprojects.restsrv.dto.ProfileIdDto;
import ru.resprojects.restsrv.exception.ErrorMessage;
import ru.resprojects.restsrv.exception.LastException;
import ru.resprojects.restsrv.model.Profile;
import ru.resprojects.restsrv.repository.ProfileRepository;
import ru.resprojects.restsrv.token.TokenResourceDetails;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = {"test"})
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
scripts = {"classpath:schema-h2.sql"},
config = @SqlConfig(encoding = "UTF-8"))
public class ProfileControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private ProfileRepository repository;
@Autowired
private TokenResourceDetails tokenResourceDetails;
private Profile exampleProfile;
private final HttpHeaders headers = new HttpHeaders();
@Before
public void init() {
headers.add("Authorization", TokenRequestsFilter.SECURITY_SCHEME + " " + tokenResourceDetails.getToken());
Profile profile = new Profile();
profile.setName("Alex");
profile.setEmail("alex@example.com");
profile.setAge(10);
exampleProfile = repository.save(profile);
}
@Test
public void whenFindByExistentIdThenStatus200AndReturnProfile() {
HttpEntity<String> entity = new HttpEntity<>(null, headers);
ResponseEntity<Profile> profile = restTemplate.exchange("/profiles/5000", HttpMethod.GET, entity, Profile.class);
assertThat(profile.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(profile.getBody()).isNotNull();
assertThat(profile.getBody().getName()).isEqualTo(exampleProfile.getName());
}
@Test
public void whenRequestAllProfilesThenStatus200AndReturnListOfProfiles() {
HttpEntity<String> entity = new HttpEntity<>(null, headers);
ResponseEntity<Profile[]> profiles = restTemplate.exchange("/profiles", HttpMethod.GET, entity, Profile[].class);
assertThat(profiles.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(profiles.getBody()).isNotNull();
assertThat(profiles.getBody()).isNotEmpty();
assertThat(profiles.getBody()[0].getName()).isEqualTo(exampleProfile.getName());
}
@Test
public void whenFindProfileByEmailThenStatus200AndReturnProfile() {
EmailDto emailDto = new EmailDto();
emailDto.setEmail("alex@example.com");
HttpEntity<EmailDto> entity = new HttpEntity<>(emailDto, headers);
ResponseEntity<Profile> profile = restTemplate.exchange("/profiles/get", HttpMethod.POST, entity, Profile.class);
assertThat(profile.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(profile.getBody()).isNotNull();
assertThat(profile.getBody().getName()).isEqualTo(exampleProfile.getName());
assertThat(profile.getBody().getCreated()).isNotNull();
}
@Test
public void whenCreateProfileThenStatus200AndReturnIdSavedProfile() {
ProfileDto profileDto = new ProfileDto();
profileDto.setName("John");
profileDto.setAge(10);
profileDto.setEmail("john@gmail.com");
HttpEntity<ProfileDto> entity = new HttpEntity<>(profileDto, headers);
ResponseEntity<ProfileIdDto> newProfileId = restTemplate.exchange("/profiles/set", HttpMethod.POST, entity, ProfileIdDto.class);
assertThat(newProfileId.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(newProfileId.getBody()).isNotNull();
assertThat(newProfileId.getBody().getUserId()).isEqualTo(5001);
}
@Test
public void whenRequestLastCreatedProfileThenStatus200AndReturnLastSavedProfile() {
ProfileDto profileDto = new ProfileDto();
profileDto.setName("John");
profileDto.setAge(10);
profileDto.setEmail("john@gmail.com");
HttpEntity<ProfileDto> entity = new HttpEntity<>(profileDto, headers);
ResponseEntity<ProfileIdDto> newProfileId = restTemplate.exchange("/profiles/set", HttpMethod.POST, entity, ProfileIdDto.class);
assertThat(newProfileId.getBody()).isNotNull();
HttpEntity<Void> newEntity = new HttpEntity<>(null, headers);
ResponseEntity<Profile> profile = restTemplate.exchange("/profiles/last", HttpMethod.GET, newEntity, Profile.class);
assertThat(profile.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(profile.getBody()).isNotNull();
assertThat(profile.getBody().getId()).isEqualTo(newProfileId.getBody().getUserId());
}
@Test
public void whenRequestProfileWithNonexistentIdThenStatus404AndReturnErrorMessage() {
HttpEntity<String> entity = new HttpEntity<>(null, headers);
ResponseEntity<ErrorMessage> errorMessage = restTemplate.exchange("/profiles/5010", HttpMethod.GET, entity, ErrorMessage.class);
assertThat(errorMessage.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(errorMessage.getBody()).isNotNull();
}
@Test
public void whenRequestProfileWithNonexistentEmailThenStatus404AndReturnErrorMessage() {
EmailDto emailDto = new EmailDto();
emailDto.setEmail("test@example.com");
HttpEntity<EmailDto> entity = new HttpEntity<>(emailDto, headers);
ResponseEntity<ErrorMessage> errorMessage = restTemplate.exchange("/profiles/get", HttpMethod.POST, entity, ErrorMessage.class);
assertThat(errorMessage.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(errorMessage.getBody()).isNotNull();
}
@Test
public void whenCreatProfileWithIncorrectEmailThenReturnStatus400AndErrorMessage() {
ProfileDto profileDto = new ProfileDto();
profileDto.setName("John");
profileDto.setAge(10);
profileDto.setEmail(".@gmail.com");
HttpEntity<ProfileDto> entity = new HttpEntity<>(profileDto, headers);
ResponseEntity<ErrorMessage> errorMessage = restTemplate.exchange("/profiles/set", HttpMethod.POST, entity, ErrorMessage.class);
assertThat(errorMessage.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(errorMessage.getBody()).isNotNull();
}
@Test
public void whenCreatProfileWithExistentEmailThenReturnStatus403AndErrorMessage() {
ProfileDto profileDto = new ProfileDto();
profileDto.setName("John");
profileDto.setAge(10);
profileDto.setEmail("alex@example.com");
HttpEntity<ProfileDto> entity = new HttpEntity<>(profileDto, headers);
ResponseEntity<ErrorMessage> errorMessage = restTemplate.exchange("/profiles/set", HttpMethod.POST, entity, ErrorMessage.class);
assertThat(errorMessage.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(errorMessage.getBody()).isNotNull();
}
@Test
public void whenRequestLastErrorThenStatus200AndReturnMessageWithLastError() {
HttpEntity<String> entity = new HttpEntity<>(null, headers);
ResponseEntity<ErrorMessage> errorMessage = restTemplate.exchange("/profiles/5010", HttpMethod.GET, entity, ErrorMessage.class);
assertThat(errorMessage.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(errorMessage.getBody()).isNotNull();
ResponseEntity<LastException> lastErrorMessage = restTemplate.exchange("/error/last", HttpMethod.GET, entity, LastException.class);
assertThat(lastErrorMessage.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(lastErrorMessage.getBody()).isNotNull();
assertThat(lastErrorMessage.getBody().getMessage()).isEqualTo(errorMessage.getBody().getMessage());
assertThat(lastErrorMessage.getBody().getCreated()).isNotNull();
}
}

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);
}
}