Skip to content

Commit

Permalink
Coverage unit for repository layer (#41)
Browse files Browse the repository at this point in the history
* implement authentication and fix error security

* refactor name version of dependencies

* active prod environment

* modify relationship entities to get data

* add message status to response

* turn on jpa auditing

* add correlationId

* handle verify email

* override MethodArgumentNotValidException and handle exception

* create role api

* fix error verify email

* remove redudant class

* create a general for response api and modify response at controller

* handle upload file amazon s3

* dÆhandle dÆÆownload file from s3

* implement redis server

* implement search and advance search api

* handle get ÃÃinformation company api

* handle cache data for get all users

* handle cache data search users api

* add a field response data of CompanyProfile

* add READ.ME for config send log from application to cloudWatch

* set up monitoring log by CloudWatch

* add document to set up cloudWatch

* create update users api and invalidate cache

* add payment class

* modify relationship between CompanyProfile and CompanyProfileMapping

* coverage unit tÃest for repository layer

---------

Co-authored-by: letung999 <[email protected]>
  • Loading branch information
letung999 and letung999 authored Nov 19, 2023
1 parent db7894c commit 462ee72
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 0 deletions.
161 changes: 161 additions & 0 deletions src/test/java/com/ppn/ppn/repository/CompanyRepositoryTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.ppn.ppn.repository;


import com.ppn.ppn.entities.CompanyProfile;
import com.ppn.ppn.entities.CompanyProfileMapping;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class CompanyRepositoryTests {

@Autowired
private CompanyRepository companyRepository;

@Autowired
private CompanyProfileMappingRepository companyProfileMappingRepository;

@Test
public void givenListCompanyProfile_whenGetListCompanyProfileByDate_thenListCompanyProfile() throws ParseException {
//setup
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date inputDate = simpleDateFormat.parse("2009-10-01");
Date inputDate1 = simpleDateFormat.parse("2004-10-01");

CompanyProfile companyProfile = CompanyProfile.builder()
.companyIdName("001_shopBack")
.dateOfEstablishment(inputDate)
.build();

CompanyProfile companyProfile1 = CompanyProfile.builder()
.companyIdName("002_ManHeim")
.dateOfEstablishment(inputDate1)
.build();

CompanyProfile companyProfileSaved = companyRepository.save(companyProfile);
CompanyProfile companyProfileSaved1 = companyRepository.save(companyProfile1);


CompanyProfileMapping companyProfileMapping = CompanyProfileMapping.builder()
.companyId(companyProfileSaved.getCompanyId())
.parentCompanyId("001_shopBack")
.companyProfile(companyProfileSaved)
.build();
CompanyProfileMapping companyProfileMapping1 = CompanyProfileMapping.builder()
.companyId(companyProfileSaved1.getCompanyId())
.parentCompanyId("002_ManHeim")
.companyProfile(companyProfileSaved1)
.build();

companyProfileMappingRepository.save(companyProfileMapping);
companyProfileMappingRepository.save(companyProfileMapping1);

//action
List<CompanyProfile> resultData = companyRepository.getListCompanyProfile(inputDate1);

//output
Assertions.assertThat(resultData).isNotNull();
Assertions.assertThat(resultData.size()).isEqualTo(2);
}

@Test
public void givenListCompanyProfileId_whenGetListCompanyProfileId_thenListCompanyProfileId() throws ParseException {
//setup
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date inputDate = simpleDateFormat.parse("2009-10-01");
Date inputDate1 = simpleDateFormat.parse("2004-10-01");

CompanyProfile companyProfile = CompanyProfile.builder()
.companyIdName("001_shopBack")
.dateOfEstablishment(inputDate)
.build();

CompanyProfile companyProfile1 = CompanyProfile.builder()
.companyIdName("002_ManHeim")
.dateOfEstablishment(inputDate1)
.build();

CompanyProfile companyProfileSaved = companyRepository.save(companyProfile);
CompanyProfile companyProfileSaved1 = companyRepository.save(companyProfile1);

CompanyProfileMapping companyProfileMapping = CompanyProfileMapping.builder()
.companyId(companyProfileSaved.getCompanyId())
.parentCompanyId("001_shopBack")
.companyProfile(companyProfileSaved)
.build();
CompanyProfileMapping companyProfileMapping1 = CompanyProfileMapping.builder()
.companyId(companyProfileSaved1.getCompanyId())
.parentCompanyId("002_ManHeim")
.companyProfile(companyProfileSaved1)
.build();

companyProfileMappingRepository.save(companyProfileMapping);
companyProfileMappingRepository.save(companyProfileMapping1);

PageRequest pageRequest = PageRequest.of(0, 2);

//action
Page<CompanyProfile> companyProfilePage = companyRepository.getListCompanyProfileId(pageRequest);
List<CompanyProfile> companyProfiles = companyProfilePage.getContent();

//output
Assertions.assertThat(companyProfiles).isNotNull();
Assertions.assertThat(companyProfiles.size()).isEqualTo(2);
}

@Test
public void givenListCompanyProfileIdIsEmpty_whenGetListCompanyProfileId_givenListCompanyProfileIdIsEmpty() throws ParseException {
//setup
//setup
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date inputDate = simpleDateFormat.parse("2009-10-01");
Date inputDate1 = simpleDateFormat.parse("2004-10-01");

CompanyProfile companyProfile = CompanyProfile.builder()
.companyIdName("001_shopBack")
.dateOfEstablishment(inputDate)
.build();

CompanyProfile companyProfile1 = CompanyProfile.builder()
.companyIdName("002_ManHeim")
.dateOfEstablishment(inputDate1)
.build();

CompanyProfile companyProfileSaved = companyRepository.save(companyProfile);
CompanyProfile companyProfileSaved1 = companyRepository.save(companyProfile1);

CompanyProfileMapping companyProfileMapping = CompanyProfileMapping.builder()
.companyId(companyProfileSaved.getCompanyId())
.parentCompanyId("001_shopBack")
.companyProfile(companyProfileSaved)
.build();
CompanyProfileMapping companyProfileMapping1 = CompanyProfileMapping.builder()
.companyId(companyProfileSaved1.getCompanyId())
.parentCompanyId("002_ManHeim")
.companyProfile(companyProfileSaved1)
.build();

companyProfileMappingRepository.save(companyProfileMapping);
companyProfileMappingRepository.save(companyProfileMapping1);

PageRequest pageRequest = PageRequest.of(1, 2);

//action
Page<CompanyProfile> companyProfilePage = companyRepository.getListCompanyProfileId(pageRequest);
List<CompanyProfile> companyProfiles = companyProfilePage.getContent();

//output
Assertions.assertThat(companyProfiles).isEmpty();
}
}
137 changes: 137 additions & 0 deletions src/test/java/com/ppn/ppn/repository/UserRepositoryTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.ppn.ppn.repository;


import com.ppn.ppn.entities.Users;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.util.List;
import java.util.Optional;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class UserRepositoryTests {
@Autowired
private UsersRepository usersRepository;


@Test
public void givenUserObject_whenSave_thenReturnSavedUser() {
//set up
Users users = Users.builder()
.email("[email protected]")
.firstName("tung")
.phoneNumber("0338257409")
.password("123456")
.status("PENDING")
.gender("Male")
.build();

//action
Users dataSaved = usersRepository.save(users);

//output
Assertions.assertThat(dataSaved).isNotNull();
Assertions.assertThat(dataSaved.getUserId()).isGreaterThan(0);
}

@Test
public void givenUsersList_whenFindAll_thenUsersList() {
//setup
Users users = Users.builder()
.email("[email protected]")
.firstName("tung")
.phoneNumber("0338257409")
.password("123456")
.status("PENDING")
.gender("Male")
.build();

Users users1 = Users.builder()
.email("[email protected]")
.firstName("tung")
.phoneNumber("0338257409")
.password("123456")
.status("PENDING")
.gender("Male")
.build();

usersRepository.save(users);
usersRepository.save(users1);

//action
List<Users> usersList = usersRepository.findAll();

//output
Assertions.assertThat(usersList).isNotNull();
Assertions.assertThat(usersList.size()).isEqualTo(3);
}

@Test
public void givenUsersObject_whenFindByUsersById_thenUsersObject() {
//setup
Users users = Users.builder()
.email("[email protected]")
.firstName("tung")
.phoneNumber("0338257409")
.password("123456")
.status("PENDING")
.gender("Male")
.build();

Users dataSaved = usersRepository.save(users);

//action
Users data = usersRepository.findById(dataSaved.getUserId()).get();

//output
Assertions.assertThat(data).isNotNull();
}

@Test
public void givenUsersObject_whenFindByEmail_thenUsersObject() {
//setup
Users users = Users.builder()
.email("[email protected]")
.firstName("tung")
.phoneNumber("0338257409")
.password("123456")
.status("PENDING")
.gender("Male")
.build();

usersRepository.save(users);

//action
Optional<Users> data = usersRepository.findByEmail(users.getEmail());

//output
Assertions.assertThat(data).isNotNull();
Assertions.assertThat(data.get().getEmail()).isEqualTo(users.getEmail());
}

@Test
public void givenUsersObject_whenFindByVerifyCode_thenUserObject() {
//setup
Users users = Users.builder()
.email("[email protected]")
.firstName("tung")
.phoneNumber("0338257409")
.password("123456")
.status("PENDING")
.gender("Male")
.verifyCode("C7jgrUGYJNsmvrbGXQSf8SZXPsxlvx")
.build();

usersRepository.save(users);

//action
Optional<Users> data = usersRepository.findByVerifyCode(users.getVerifyCode());

//output
Assertions.assertThat(data).isNotNull();
}
}

0 comments on commit 462ee72

Please sign in to comment.