Skip to content

Commit

Permalink
Es/enhancement/add tests (#102)
Browse files Browse the repository at this point in the history
* add abstract to address entity

* #80
fix update car

* fix and complete registration test

* fix Registration IT

* fix and complete registration test

* fix Registration IT
  • Loading branch information
Esmaeeilenani authored Jul 17, 2023
1 parent afa88cc commit 99b98d1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ResponseEntity<Void> clientRegistration(@RequestBody @Valid ClientRegistr

log.info("user registration info {}", clientRegistrationVM);

registrationService.clientRegistration(clientRegistrationVM);
registrationService.registration(clientRegistrationVM);

return ResponseEntity.status(HttpStatus.CREATED).build();
}
Expand All @@ -34,7 +34,7 @@ public ResponseEntity<Void> carProviderRegistration(@RequestBody @Valid CarProvi

log.info("car provider user registration info {}", carProviderRegistrationVM);

registrationService.carProviderRegistration(carProviderRegistrationVM);
registrationService.registration(carProviderRegistrationVM);

return ResponseEntity.status(HttpStatus.CREATED).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class RegistrationService {
private final PasswordEncoder passwordEncoder;


public void clientRegistration(ClientRegistrationVM clientRegistrationVM) {
public void registration(ClientRegistrationVM clientRegistrationVM) {
User user = createUser(clientRegistrationVM, AuthorityConstants.CLIENT, LookupCodes.UserType.client);

DriverLicense driverLicense = DriverLicenseVM.vmToEntity(clientRegistrationVM.getDriverLicense());
Expand All @@ -59,7 +59,7 @@ public void clientRegistration(ClientRegistrationVM clientRegistrationVM) {

}

public void carProviderRegistration(CarProviderRegistrationVM carProviderRegistrationVM) {
public void registration(CarProviderRegistrationVM carProviderRegistrationVM) {
User user = createUser(carProviderRegistrationVM, AuthorityConstants.CAR_PROVIDER, LookupCodes.UserType.carProvider);

CarProvider carProvider = new CarProvider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public static String obfuscateString(Object data) {
log.error("Hashing failed!");
}
}
log.error("Obfuscation of data-type {} is not implemented. Hashing failed. Returned value: data.toString().",data.getClass().toString());
return data.toString();
// log.error("Obfuscation of data-type {} is not implemented. Hashing failed. Returned value: data.toString().",data.getClass().toString());
return "";

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public User getLoggedInUser() {
}




}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.amigoscode.chohort2.carRental;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
Expand All @@ -13,7 +14,7 @@

@Testcontainers
@ActiveProfiles("test")
//@TestPropertySource(properties = "spring.datasource.url=jdbc:tc:postgresql:latest:///")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractTestContainer {

@Container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import com.amigoscode.chohort2.carRental.AbstractTestContainer;
import com.amigoscode.chohort2.carRental.driverLicense.VM.DriverLicenseVM;
import com.amigoscode.chohort2.carRental.registration.VM.ClientRegistrationVM;
import com.amigoscode.chohort2.carRental.registration.VM.UserRegistrationVM;
import com.amigoscode.chohort2.carRental.user.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;

import java.time.LocalDate;
import static org.assertj.core.api.Assertions.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class RegistrationControllerIT extends AbstractTestContainer {

@Autowired
Expand All @@ -23,6 +22,11 @@ class RegistrationControllerIT extends AbstractTestContainer {
private RegistrationService registrationService;



@Autowired
private UserRepository userRepository;


private static final String API_URL = "api/v1/registrations/";


Expand All @@ -49,7 +53,7 @@ void givenClientRegistration_whenSave_thenCreateNewUser() {
// when - action or the behaviour that we are going test
webTestClient
.post()
.uri(API_URL+"clients")
.uri(API_URL + "clients")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(Mono.just(clientRegistrationVM), ClientRegistrationVM.class)
Expand All @@ -58,6 +62,10 @@ void givenClientRegistration_whenSave_thenCreateNewUser() {


// then verify the output
assertThat(userRepository.findByUsernameWithAuthorities(clientRegistrationVM.getUsername()).isPresent())
.isTrue();



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@
import com.amigoscode.chohort2.carRental.authority.Authority;
import com.amigoscode.chohort2.carRental.authority.AuthorityConstants;
import com.amigoscode.chohort2.carRental.authority.AuthorityService;
import com.amigoscode.chohort2.carRental.carProvider.CarProvider;
import com.amigoscode.chohort2.carRental.carProvider.CarProviderService;
import com.amigoscode.chohort2.carRental.carProvider.VM.CarProviderVM;
import com.amigoscode.chohort2.carRental.carProviderUser.CarProviderUser;
import com.amigoscode.chohort2.carRental.carProviderUser.CarProviderUserService;
import com.amigoscode.chohort2.carRental.driverLicense.DriverLicense;
import com.amigoscode.chohort2.carRental.driverLicense.DriverLicenseService;
import com.amigoscode.chohort2.carRental.driverLicense.VM.DriverLicenseVM;
import com.amigoscode.chohort2.carRental.registration.VM.CarProviderRegistrationVM;
import com.amigoscode.chohort2.carRental.registration.VM.ClientRegistrationVM;
import com.amigoscode.chohort2.carRental.user.User;
import com.amigoscode.chohort2.carRental.user.UserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.password.PasswordEncoder;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;


import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class RegistrationServiceTest{
class RegistrationServiceTest {

@Mock
private UserService userService;
Expand All @@ -36,6 +46,11 @@ class RegistrationServiceTest{
@Mock
private DriverLicenseService driverLicenseService;

@Mock
private CarProviderUserService carProviderUserService;

@Mock
private CarProviderService carProviderService;


@InjectMocks
Expand Down Expand Up @@ -66,13 +81,48 @@ void givenClientRegistration_whenSave_thenCreateNewUser() {
.willReturn(new Authority().setName(AuthorityConstants.CLIENT));

given(passwordEncoder.encode(clientRegistrationVM.getPassword()))
.willReturn("$2a$10$fi6PMDJmJaxFyoursx7Pw.LdMbpcbvAf0i7g9wO6fG8HrIJ6Kn0jm");
.willReturn("encoded");


// when - action or the behaviour that we are going test
underTest.clientRegistration(clientRegistrationVM);
underTest.registration(clientRegistrationVM);

// then verify the output
verify(userService).save(any(User.class));
verify(driverLicenseService).save(any(DriverLicense.class));

}


@Test
void givenCarProviderRegistration_whenSave_thenCreateNewUser() {
// given precondition or setup
CarProviderRegistrationVM carProviderRegistrationVM = (CarProviderRegistrationVM) new CarProviderRegistrationVM()
.setUsername("esmaeeil")
.setFirstName("esmaeeil")
.setLastName("enani")
.setEmail("[email protected]")
.setNin("123456789")
.setPassword("123456789");

CarProviderVM carProviderVM = new CarProviderVM()
.setCrNumber("1234567890")
.setName("my company");
carProviderRegistrationVM.setCarProviderVM(carProviderVM);

given(authorityService.findByName(AuthorityConstants.CAR_PROVIDER))
.willReturn(new Authority().setName(AuthorityConstants.CAR_PROVIDER));

given(passwordEncoder.encode(carProviderRegistrationVM.getPassword()))
.willReturn("encoded");
// when - action or the behaviour that we are going test
underTest.registration(carProviderRegistrationVM);


// then verify the output
verify(userService).save(any(User.class));
verify(carProviderService).saveCarProvider(any(CarProvider.class));
verify(carProviderUserService).saveCarProviderUser(any(CarProviderUser.class));

}
}

0 comments on commit 99b98d1

Please sign in to comment.