Skip to content

Commit

Permalink
models added
Browse files Browse the repository at this point in the history
  • Loading branch information
Muryginds committed Feb 2, 2024
1 parent a08d259 commit 365d88e
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/main/java/ru/ylab/model/AuditionEventModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.ylab.model;

import lombok.Builder;

import java.time.LocalDateTime;

@Builder
public record AuditionEventModel (
Long id,
Long userId,
Long typeId,
String message,
LocalDateTime date
) {
public static class SubmissionModelBuilder {
private final LocalDateTime date = LocalDateTime.now();
}
}
11 changes: 11 additions & 0 deletions src/main/java/ru/ylab/model/MeterModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.ylab.model;

import lombok.Builder;

@Builder
public record MeterModel (
Long id,
String factoryNumber,
Long userId,
Long typeId) {
}
14 changes: 14 additions & 0 deletions src/main/java/ru/ylab/model/MeterReadingModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.ylab.model;

import lombok.Builder;

@Builder
public record MeterReadingModel(
Long id,
Long submissionId,
Long meterId,
Long value) {
public static class MeterReadingModelBuilder {
private final Long value = 0L;
}
}
10 changes: 10 additions & 0 deletions src/main/java/ru/ylab/model/MeterTypeModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.ylab.model;

import lombok.Builder;

@Builder
public record MeterTypeModel(
Long id,
String typeName
) {
}
15 changes: 15 additions & 0 deletions src/main/java/ru/ylab/model/SubmissionModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.ylab.model;

import lombok.Builder;

import java.time.LocalDate;

@Builder
public record SubmissionModel(
Long id,
Long userId,
LocalDate date) {
public static class SubmissionModelBuilder {
private final LocalDate date = LocalDate.now();
}
}
16 changes: 16 additions & 0 deletions src/main/java/ru/ylab/model/UserModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.ylab.model;

import lombok.Builder;
import ru.ylab.enumerated.UserRole;

@Builder
public record UserModel(
Long id,
String name,
String password,
UserRole role) {

public static class UserModelBuilder {
private final UserRole role = UserRole.USER;
}
}
6 changes: 3 additions & 3 deletions src/test/java/ru/ylab/service/MeterServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void testGetMeterById_whenCorrect_thenReturnMeter() {
long meterId = 1L;
MeterType typeElectricity = MeterType.builder().typeName("Electricity").build();
Meter meter = Meter.builder().id(meterId).factoryNumber("123456789012").type(typeElectricity).build();
Mockito.when(meterRepository.findById(meterId)).thenReturn(Optional.of(meter));
//Mockito.when(meterRepository.findById(meterId)).thenReturn(Optional.of(meter));

Meter result = meterService.getById(meterId);

Expand All @@ -78,7 +78,7 @@ void testGetMeterDTOsByUserId_whenCorrect_thenReturnCollectionOfMeterDTOs() {
Meter meter1 = Meter.builder().factoryNumber("123456789012").type(typeElectricity).build();
Meter meter2 = Meter.builder().factoryNumber("987654321098").type(typeGas).build();
Set<Meter> meters = Set.of(meter1, meter2);
Mockito.when(meterRepository.getByUserId(userId)).thenReturn(meters);
//Mockito.when(meterRepository.getByUserId(userId)).thenReturn(meters);

Collection<MeterDTO> result = meterService.getMeterDTOsByUserId(userId);

Expand All @@ -94,7 +94,7 @@ void testGetMetersByUserId_whenCorrect_thenReturnCollectionOfMeters() {
Meter meter1 = Meter.builder().factoryNumber("123456789012").type(typeElectricity).build();
Meter meter2 = Meter.builder().factoryNumber("987654321098").type(typeGas).build();
Set<Meter> meters = Set.of(meter1, meter2);
Mockito.when(meterRepository.getByUserId(userId)).thenReturn(meters);
//Mockito.when(meterRepository.getByUserId(userId)).thenReturn(meters);

Collection<Meter> result = meterService.getMetersByUserId(userId);

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/ru/ylab/service/MeterTypeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void testSaveMeterType_whenExistingType_thenThrowMeterTypeExistException() {
void testGetById_whenExistingType_thenReturnMeterTypeDTO() {
long meterTypeId = 1L;
MeterType meterType = MeterType.builder().id(meterTypeId).typeName("Electricity").build();
Mockito.when(meterTypeRepository.findById(meterTypeId)).thenReturn(java.util.Optional.of(meterType));
//Mockito.when(meterTypeRepository.findById(meterTypeId)).thenReturn(java.util.Optional.of(meterType));

MeterTypeDTO result = meterTypeService.getById(meterTypeId);

Expand All @@ -80,7 +80,7 @@ void testGetAll_whenTypesExist_thenReturnCollectionOfMeterTypes() {
MeterType type1 = MeterType.builder().typeName("Electricity").build();
MeterType type2 = MeterType.builder().typeName("Water").build();
Collection<MeterType> types = java.util.List.of(type1, type2);
Mockito.when(meterTypeRepository.getAll()).thenReturn(types);
//Mockito.when(meterTypeRepository.getAll()).thenReturn(types);

Collection<MeterType> result = meterTypeService.getAll();

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/ru/ylab/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void testRegisterUser_whenExistingUserName_throwUserAlreadyExistException() {
void testAuthorize_whenExistingUser_thenReturnUserDTO() {
UserAuthorizationRequestDTO requestDTO = new UserAuthorizationRequestDTO("testUser", "password");
User user = User.builder().name("testUser").password("encodedPassword").build();
Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
//Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
Mockito.when(passwordEncoder.verify(requestDTO.password(), user.getPassword())).thenReturn(true);

UserDTO result = userService.authorize(requestDTO);
Expand All @@ -93,7 +93,7 @@ void testAuthorize_whenNonExistingUser_throwUserAuthenticationException() {
void testAuthorize_whenWrongPassword_throwUserAuthenticationException() {
UserAuthorizationRequestDTO requestDTO = new UserAuthorizationRequestDTO("testUser", "wrongPassword");
User user = User.builder().name("testUser").password("encodedPassword").build();
Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
//Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
Mockito.when(passwordEncoder.verify(requestDTO.password(), user.getPassword())).thenReturn(false);

Assertions.assertThrows(UserAuthenticationException.class, () -> userService.authorize(requestDTO));
Expand All @@ -104,7 +104,7 @@ void testAuthorize_whenWrongPassword_throwUserAuthenticationException() {
void testLogout() {
UserAuthorizationRequestDTO requestDTO = new UserAuthorizationRequestDTO("testUser", "password");
User user = User.builder().name("testUser").password("password").build();
Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
//Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
Mockito.when(passwordEncoder.verify(requestDTO.password(), user.getPassword())).thenReturn(true);

userService.authorize(requestDTO);
Expand Down Expand Up @@ -141,7 +141,7 @@ void testCheckUserExistsById() {
void testGetCurrentUserDTO() {
UserAuthorizationRequestDTO requestDTO = new UserAuthorizationRequestDTO("testUser", "password");
User user = User.builder().name("testUser").password("password").build();
Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
//Mockito.when(userRepository.findUserByName(requestDTO.name())).thenReturn(Optional.of(user));
Mockito.when(passwordEncoder.verify(requestDTO.password(), user.getPassword())).thenReturn(true);

userService.authorize(requestDTO);
Expand All @@ -155,7 +155,7 @@ void testGetCurrentUserDTO() {
void testGetUserById() {
long userId = 1L;
User user = User.builder().name("testUser").password("password").build();
Mockito.when(userRepository.findUserById(userId)).thenReturn(Optional.of(user));
//Mockito.when(userRepository.findUserById(userId)).thenReturn(Optional.of(user));

User result = userService.getUserById(userId);

Expand Down

0 comments on commit 365d88e

Please sign in to comment.