Лабораторная 2 (Backend)
This commit is contained in:
parent
8897d11490
commit
47316f7761
@ -1,6 +1,6 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.2.2'
|
||||
id 'org.springframework.boot' version '3.2.4'
|
||||
id 'io.spring.dependency-management' version '1.1.4'
|
||||
}
|
||||
|
||||
@ -18,6 +18,7 @@ repositories {
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
|
||||
implementation 'org.modelmapper:modelmapper:3.2.0'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
0
Lab2_1/demo/gradlew → Lab2_2/gradlew
vendored
0
Lab2_1/demo/gradlew → Lab2_2/gradlew
vendored
77
Lab2_2/src/main/java/com/example/demo/DemoApplication.java
Normal file
77
Lab2_2/src/main/java/com/example/demo/DemoApplication.java
Normal file
@ -0,0 +1,77 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.house.model.HouseEntity;
|
||||
import com.example.demo.house.service.HouseService;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class DemoApplication implements CommandLineRunner {
|
||||
private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
|
||||
|
||||
private final TypeService typeService;
|
||||
private final HouseService houseService;
|
||||
private final UsersService usersService;
|
||||
|
||||
public DemoApplication(TypeService typeService, HouseService houseService, UsersService usersService) {
|
||||
this.typeService = typeService;
|
||||
this.houseService = houseService;
|
||||
this.usersService = usersService;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
log.info("Create default types values");
|
||||
final var type1 = typeService.create(new TypeEntity(null, "Апартаменты"));
|
||||
final var type2 = typeService.create(new TypeEntity(null, "Таунхауз"));
|
||||
final var type3 = typeService.create(new TypeEntity(null, "Пентхауз"));
|
||||
final var type4 = typeService.create(new TypeEntity(null, "Квартира"));
|
||||
|
||||
final var user1 = usersService.create(new UserEntity(null, "Иван",
|
||||
"ivan@mail.ru", "+79048756587", "1"));
|
||||
final var user2 = usersService.create(new UserEntity(null, "Пётр",
|
||||
"ivan@mail.ru", "+79048756587", "1232"));
|
||||
|
||||
log.info("Create default items values");
|
||||
|
||||
houseService.create(user1.getId(), new HouseEntity(null, type4, user1, "Квартира1",
|
||||
"Удобная квартира в центре города", 49999.00));
|
||||
houseService.create(user1.getId(), new HouseEntity(null, type1, user1, "Квартира2",
|
||||
"Удобная квартира на севере", 129999.00));
|
||||
houseService.create(user1.getId(), new HouseEntity(null, type4, user1, "Квартира3",
|
||||
"Квартира с большой парковкой", 15450.50));
|
||||
houseService.create(user2.getId(), new HouseEntity(null, type2, user2, "Апартаменты",
|
||||
"Апартаменты на 24 этаже", 69900.50));
|
||||
houseService.create(user2.getId(), new HouseEntity(null, type2, user2, "Пентауз",
|
||||
"Дорогой пентхауз", 150000.00));
|
||||
houseService.create(user1.getId(), new HouseEntity(null, type3, user1, "Таунхауз",
|
||||
"Большой бассейн", 75000.00));
|
||||
houseService.create(user2.getId(), new HouseEntity(null, type3, user2, "Пентауз",
|
||||
"Для каждого!", 67800.00));
|
||||
|
||||
usersService.create(new UserEntity(null, "Иван",
|
||||
"ivan@mail.ru", "+79048756587", "1"));
|
||||
usersService.create(new UserEntity(null, "Пётр",
|
||||
"petr@mail.ru", "89057655257", "12345"));
|
||||
usersService.create(new UserEntity(null, "Сергей",
|
||||
"serg79@mail.ru", "95-85-72", "1234"));
|
||||
usersService.create(new UserEntity(null, "Джеймс",
|
||||
"jaims21@mail.ru", "89047856987", "jaen12"));
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.demo.core.configuration;
|
||||
|
||||
public class Constants {
|
||||
public static final String API_URL = "/api/1.0";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.demo.core.configuration;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MapperConfiguration {
|
||||
@Bean
|
||||
ModelMapper modelMapper() {
|
||||
return new ModelMapper();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.example.demo;
|
||||
package com.example.demo.core.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.lang.NonNull;
|
||||
@ -6,7 +6,7 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
public class WebConfiguration implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addCorsMappings(@NonNull CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
@ -0,0 +1,7 @@
|
||||
package com.example.demo.core.error;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(Long id) {
|
||||
super(String.format("Entity with id [%s] is not found or not exists", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.example.demo.core.model;
|
||||
|
||||
public abstract class BaseEntity {
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity() {
|
||||
}
|
||||
|
||||
protected BaseEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.example.demo.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CommonRepository<E, T> {
|
||||
List<E> getAll();
|
||||
|
||||
E get(T id);
|
||||
|
||||
E create(E entity);
|
||||
|
||||
E update(E entity);
|
||||
|
||||
E delete(E entity);
|
||||
|
||||
void deleteAll();
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.example.demo.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Long> {
|
||||
private final Map<Long, E> entities = new TreeMap<>();
|
||||
private Long lastId = 0L;
|
||||
|
||||
protected MapRepository() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> getAll() {
|
||||
return entities.values().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(Long id) {
|
||||
return entities.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E create(E entity) {
|
||||
lastId++;
|
||||
entity.setId(lastId);
|
||||
entities.put(lastId, entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E update(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.put(entity.getId(), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E delete(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.remove(entity.getId());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
lastId = 0L;
|
||||
entities.clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package com.example.demo.house.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.house.model.HouseEntity;
|
||||
import com.example.demo.house.service.HouseService;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/user/{userId}/house")
|
||||
public class HouseController {
|
||||
private final HouseService houseService;
|
||||
private final TypeService typeService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public HouseController(HouseService houseService, TypeService typeService,
|
||||
ModelMapper modelMapper) {
|
||||
this.houseService = houseService;
|
||||
this.modelMapper = modelMapper;
|
||||
this.typeService = typeService;
|
||||
}
|
||||
|
||||
private HouseDto toDto(HouseEntity entity) {
|
||||
return modelMapper.map(entity, HouseDto.class);
|
||||
// HouseDto dto = new HouseDto();
|
||||
// dto.setId(entity.getId());
|
||||
// dto.setUserId(entity.getUser().getId());
|
||||
// dto.setTitle(entity.getTitle());
|
||||
// dto.setDescription(entity.getDescription());
|
||||
// dto.setPrice(entity.getPrice());
|
||||
// return dto;
|
||||
}
|
||||
|
||||
private HouseEntity toEntity(HouseDto dto) {
|
||||
final HouseEntity entity = modelMapper.map(dto, HouseEntity.class);
|
||||
entity.setType(typeService.get(dto.getTypeId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
public List<HouseDto> getAllHouses() {
|
||||
return houseService.getAllHouses().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<HouseDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId,
|
||||
@PathVariable(name = "userId") Long userId) {
|
||||
return houseService.getAll(typeId, userId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public HouseDto get(@PathVariable(name = "id") Long id,
|
||||
@PathVariable(name = "userId") Long userId) {
|
||||
return toDto(houseService.get(id, userId));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public HouseDto create(@PathVariable(name = "userId") Long userId,
|
||||
@RequestBody @Valid HouseDto dto) {
|
||||
return toDto(houseService.create(userId, toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public HouseDto update(@PathVariable(name = "id") Long id,
|
||||
@PathVariable(name = "userId") Long userId, @RequestBody HouseDto dto) {
|
||||
return toDto(houseService.update(id, userId, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public HouseDto delete(@PathVariable(name = "id") Long id, @PathVariable(name = "userId") Long userId) {
|
||||
return toDto(houseService.delete(id, userId));
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package com.example.demo.house.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class HouseDto {
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long typeId;
|
||||
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long userId;
|
||||
|
||||
@NotBlank
|
||||
private String title;
|
||||
|
||||
@NotBlank
|
||||
private String description;
|
||||
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Double price;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getTypeId() {
|
||||
return typeId;
|
||||
}
|
||||
|
||||
public void setTypeId(Long typeId) {
|
||||
this.typeId = typeId;
|
||||
}
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package com.example.demo.house.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
public class HouseEntity extends BaseEntity {
|
||||
private TypeEntity type;
|
||||
private UserEntity user;
|
||||
private String title;
|
||||
private String description;
|
||||
private Double price;
|
||||
|
||||
public HouseEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public HouseEntity(Long id, TypeEntity type, UserEntity user, String title,
|
||||
String description, Double price) {
|
||||
super(id);
|
||||
this.type = type;
|
||||
this.user = user;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public TypeEntity getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(TypeEntity type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public UserEntity getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(UserEntity user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, user, title, description, price);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final HouseEntity other = (HouseEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getType(), type)
|
||||
&& Objects.equals(other.getUser(), user)
|
||||
&& Objects.equals(other.getTitle(), title)
|
||||
&& Objects.equals(other.getDescription(), description)
|
||||
&& Objects.equals(other.getPrice(), price);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.demo.house.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.house.model.HouseEntity;
|
||||
|
||||
@Repository
|
||||
public class HouseRepository extends MapRepository<HouseEntity> {
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.example.demo.house.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.house.model.HouseEntity;
|
||||
import com.example.demo.house.repository.HouseRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
@Service
|
||||
public class HouseService {
|
||||
private final HouseRepository repository;
|
||||
private final UsersService userService;
|
||||
|
||||
public HouseService(HouseRepository repository, UsersService usersService) {
|
||||
this.repository = repository;
|
||||
this.userService = usersService;
|
||||
}
|
||||
|
||||
public List<HouseEntity> getAllHouses() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public List<HouseEntity> getAll(Long typeId, Long userId) {
|
||||
final UserEntity user = userService.get(userId);
|
||||
if (typeId > 0) {
|
||||
return repository.getAll().stream()
|
||||
.filter(house -> house.getType().getId().equals(typeId))
|
||||
.filter(house -> house.getUser().equals(user))
|
||||
.toList();
|
||||
}
|
||||
|
||||
return repository.getAll().stream()
|
||||
.filter(house -> house.getUser().equals(user))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public HouseEntity get(Long id, Long userId) {
|
||||
final UserEntity user = userService.get(userId);
|
||||
final HouseEntity house = Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
||||
if (!house.getUser().equals(user)) {
|
||||
throw new NotFoundException(id);
|
||||
}
|
||||
return house;
|
||||
}
|
||||
|
||||
public HouseEntity create(Long userId, HouseEntity entity) {
|
||||
final UserEntity user = userService.get(userId);
|
||||
entity.setUser(user);
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public HouseEntity update(Long id, Long userId, HouseEntity entity) {
|
||||
userService.get(userId);
|
||||
final HouseEntity existEntity = get(id, userId);
|
||||
existEntity.setTitle(entity.getTitle());
|
||||
existEntity.setDescription(entity.getDescription());
|
||||
existEntity.setPrice(entity.getPrice());
|
||||
existEntity.setType(entity.getType());
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
public HouseEntity delete(Long id, Long userId) {
|
||||
userService.get(userId);
|
||||
final HouseEntity existEntity = get(id, userId);
|
||||
return repository.delete(existEntity);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.example.demo.speaker.api;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.speaker.service.SpeakerService;
|
||||
|
||||
@RestController
|
||||
public class SpeakerController {
|
||||
private final SpeakerService speakerService;
|
||||
|
||||
public SpeakerController(SpeakerService speakerService) {
|
||||
this.speakerService = speakerService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String hello(
|
||||
@RequestParam(value = "name", defaultValue = "Мир") String name,
|
||||
@RequestParam(value = "lang", defaultValue = "ru") String lang) {
|
||||
return speakerService.say(name, lang);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.example.demo.speaker.configuration;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.example.demo.speaker.domain.Speaker;
|
||||
import com.example.demo.speaker.domain.SpeakerEng;
|
||||
import com.example.demo.speaker.domain.SpeakerRus;
|
||||
|
||||
@Configuration
|
||||
public class SpeakerConfiguration {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerConfiguration.class);
|
||||
|
||||
@Bean(value = "ru", initMethod = "init", destroyMethod = "destroy")
|
||||
public SpeakerRus createRusSpeaker() {
|
||||
log.info("Call createRusSpeaker()");
|
||||
return new SpeakerRus();
|
||||
}
|
||||
|
||||
@Bean(value = "en")
|
||||
public Speaker createEngSpeaker() {
|
||||
log.info("Call createEngSpeaker()");
|
||||
return new SpeakerEng();
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
public interface Speaker {
|
||||
String say();
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
|
||||
@Component(value = "de")
|
||||
public class SpeakerDeu implements Speaker {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerDeu.class);
|
||||
|
||||
@Override
|
||||
public String say() {
|
||||
return "Hallo";
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("SpeakerDeu.init()");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
log.info("SpeakerDeu.destroy()");
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
public class SpeakerEng implements Speaker, InitializingBean, DisposableBean {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerEng.class);
|
||||
|
||||
@Override
|
||||
public String say() {
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
log.info("SpeakerEng.afterPropertiesSet()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
log.info("SpeakerEng.destroy()");
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SpeakerRus implements Speaker {
|
||||
private final Logger log = LoggerFactory.getLogger(SpeakerRus.class);
|
||||
|
||||
@Override
|
||||
public String say() {
|
||||
return "Привет";
|
||||
}
|
||||
|
||||
public void init() {
|
||||
log.info("SpeakerRus.init()");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
log.info("SpeakerRus.destroy()");
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.example.demo.speaker.domain.Speaker;
|
||||
|
||||
@Service
|
||||
public class SpeakerService {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public SpeakerService(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public String say(String name, String lang) {
|
||||
@SuppressWarnings("null")
|
||||
final Speaker speaker = (Speaker) applicationContext.getBean(lang);
|
||||
return String.format("%s, %s!", speaker.say(), name);
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.example.demo.types.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/type")
|
||||
public class TypeController {
|
||||
private final TypeService typeService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public TypeController(TypeService typeService, ModelMapper modelMapper) {
|
||||
this.typeService = typeService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private TypeDto toDto(TypeEntity entity) {
|
||||
return modelMapper.map(entity, TypeDto.class);
|
||||
}
|
||||
|
||||
private TypeEntity toEntity(TypeDto dto) {
|
||||
return modelMapper.map(dto, TypeEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<TypeDto> getAll() {
|
||||
return typeService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public TypeDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(typeService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public TypeDto create(@RequestBody @Valid TypeDto dto) {
|
||||
return toDto(typeService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody TypeDto dto) {
|
||||
return toDto(typeService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public TypeDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(typeService.delete(id));
|
||||
}
|
||||
}
|
28
Lab2_2/src/main/java/com/example/demo/types/api/TypeDto.java
Normal file
28
Lab2_2/src/main/java/com/example/demo/types/api/TypeDto.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.example.demo.types.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class TypeDto {
|
||||
private Long id;
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.demo.types.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
public class TypeEntity extends BaseEntity {
|
||||
private String name;
|
||||
|
||||
public TypeEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TypeEntity(Long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final TypeEntity other = (TypeEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.types.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
|
||||
@Repository
|
||||
public class TypeRepository extends MapRepository<TypeEntity> {
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.demo.types.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.types.repository.TypeRepository;
|
||||
|
||||
@Service
|
||||
public class TypeService {
|
||||
private final TypeRepository repository;
|
||||
|
||||
public TypeService(TypeRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<TypeEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public TypeEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public TypeEntity create(TypeEntity entity) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public TypeEntity update(Long id, TypeEntity entity) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
||||
public TypeEntity delete(Long id) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/user")
|
||||
public class UserController {
|
||||
private final UsersService usersService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public UserController(UsersService usersService, ModelMapper modelMapper) {
|
||||
this.usersService = usersService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private UserDto toDto(UserEntity entity) {
|
||||
return modelMapper.map(entity, UserDto.class);
|
||||
}
|
||||
|
||||
private UserEntity toEntity(UserDto dto) {
|
||||
final UserEntity entity = modelMapper.map(dto, UserEntity.class);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<UserDto> getAll() {
|
||||
return usersService.getAll().stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public UserDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(usersService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public UserDto create(@RequestBody @Valid UserDto dto) {
|
||||
return toDto(usersService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) {
|
||||
return toDto(usersService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public UserDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(usersService.delete(id));
|
||||
}
|
||||
}
|
67
Lab2_2/src/main/java/com/example/demo/users/api/UserDto.java
Normal file
67
Lab2_2/src/main/java/com/example/demo/users/api/UserDto.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public class UserDto {
|
||||
private Long id;
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
private String email;
|
||||
|
||||
@NotBlank
|
||||
private String numberphone;
|
||||
|
||||
@NotBlank
|
||||
private String password;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getNumberphone() {
|
||||
return numberphone;
|
||||
}
|
||||
|
||||
public void setNumberphone(String numberphone) {
|
||||
this.numberphone = numberphone;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getNumberPhone() {
|
||||
return numberphone;
|
||||
}
|
||||
|
||||
public void setNumberPhone(String numberphone) {
|
||||
this.numberphone = numberphone;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package com.example.demo.users.model;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
public class UserEntity extends BaseEntity {
|
||||
private String name;
|
||||
private String email;
|
||||
private String numberphone;
|
||||
private String password;
|
||||
|
||||
public UserEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String name, String email, String numberphone, String password) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.numberphone = numberphone;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getNumberPhone() {
|
||||
return numberphone;
|
||||
}
|
||||
|
||||
public void setNumberPhone(String numberphone) {
|
||||
this.numberphone = numberphone;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, email, numberphone, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null || getClass() != obj.getClass())
|
||||
return false;
|
||||
final UserEntity other = (UserEntity) obj;
|
||||
return Objects.equals(other.getId(), id)
|
||||
&& Objects.equals(other.getName(), name)
|
||||
&& Objects.equals(other.getEmail(), email)
|
||||
&& Objects.equals(other.getNumberPhone(), numberphone)
|
||||
&& Objects.equals(other.getPassword(), password);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.users.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
@Repository
|
||||
public class UsersRepository extends MapRepository<UserEntity> {
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.example.demo.users.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.repository.UsersRepository;
|
||||
|
||||
@Service
|
||||
public class UsersService {
|
||||
private final UsersRepository repository;
|
||||
|
||||
public UsersService(UsersRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<UserEntity> getAll() {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public UserEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public UserEntity create(UserEntity entity) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public UserEntity update(Long id, UserEntity entity) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setEmail(entity.getEmail());
|
||||
existsEntity.setNumberPhone(entity.getNumberPhone());
|
||||
existsEntity.setPassword(entity.getPassword());
|
||||
return repository.update(existsEntity);
|
||||
}
|
||||
|
||||
public UserEntity delete(Long id) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
}
|
||||
}
|
52
Lab2_2/src/test/java/com/example/demo/HouseServiceTests.java
Normal file
52
Lab2_2/src/test/java/com/example/demo/HouseServiceTests.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.house.model.HouseEntity;
|
||||
import com.example.demo.house.service.HouseService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class HouseServiceTests {
|
||||
@Autowired
|
||||
private HouseService houseService;
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> houseService.get(0L, 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
houseService.create(1L, new HouseEntity(null, null, null, "fewfe", "cool",210.0));
|
||||
houseService.create(2L, new HouseEntity(null, null, null, "weffew", "cool efwfew", 210.0));
|
||||
houseService.create(3L, new HouseEntity(null, null, null, "efwfew", "very cool wffwe", 2100.0));
|
||||
|
||||
Assertions.assertEquals(5, houseService.getAll(0L, 1L).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final HouseEntity newHouse = new HouseEntity(null, null, null, "House", "cool house", 1500.0);
|
||||
final HouseEntity upHouse = houseService.update(2L, 1L, newHouse);
|
||||
Assertions.assertEquals(newHouse.getTitle(), upHouse.getTitle());
|
||||
Assertions.assertEquals(newHouse.getDescription(), upHouse.getDescription());
|
||||
Assertions.assertEquals(newHouse.getPrice(), upHouse.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
houseService.delete(1L, 1L);
|
||||
Assertions.assertEquals(4, houseService.getAll(0L, 1L).size());
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.speaker.service.SpeakerService;
|
||||
|
||||
@SpringBootTest
|
||||
class SpeakerSrviceTests {
|
||||
@Autowired
|
||||
SpeakerService speakerService;
|
||||
|
||||
@Test
|
||||
void testSpeakerRus() {
|
||||
final String res = speakerService.say("Мир", "ru");
|
||||
Assertions.assertEquals("Привет, Мир!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSpeakerEng() {
|
||||
final String res = speakerService.say("World", "en");
|
||||
Assertions.assertEquals("Hello, World!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSpeakerDeu() {
|
||||
final String res = speakerService.say("Welt", "de");
|
||||
Assertions.assertEquals("Hallo, Welt!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSpeakerErrorWired() {
|
||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> speakerService.say("Мир", "rus"));
|
||||
}
|
||||
}
|
59
Lab2_2/src/test/java/com/example/demo/TypeServiceTests.java
Normal file
59
Lab2_2/src/test/java/com/example/demo/TypeServiceTests.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
import com.example.demo.types.service.TypeService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class TypeServiceTests {
|
||||
@Autowired
|
||||
private TypeService typeService;
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
typeService.create(new TypeEntity(null, "Ноутбук"));
|
||||
typeService.create(new TypeEntity(null, "Телефон"));
|
||||
Assertions.assertEquals(6, typeService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final String test = "TEST";
|
||||
final TypeEntity entity = typeService.get(3L);
|
||||
final String oldName = entity.getName();
|
||||
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
|
||||
Assertions.assertEquals(6, typeService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, typeService.get(3L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
typeService.delete(3L);
|
||||
Assertions.assertEquals(5, typeService.getAll().size());
|
||||
final TypeEntity last = typeService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игровая приставка"));
|
||||
Assertions.assertEquals(6, typeService.getAll().size());
|
||||
Assertions.assertEquals(7, newEntity.getId());
|
||||
}
|
||||
}
|
59
Lab2_2/src/test/java/com/example/demo/UsersServiceTests.java
Normal file
59
Lab2_2/src/test/java/com/example/demo/UsersServiceTests.java
Normal file
@ -0,0 +1,59 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UsersService;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
class UsersServiceTests {
|
||||
@Autowired
|
||||
private UsersService userService;
|
||||
|
||||
@Test
|
||||
void getTest() {
|
||||
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
userService.create(new UserEntity(null, "Иван", "", "", ""));
|
||||
userService.create(new UserEntity(null, "Иван", "", "", ""));
|
||||
Assertions.assertEquals(8, userService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final String test = "TEST";
|
||||
final UserEntity entity = userService.get(3L);
|
||||
final String oldName = entity.getName();
|
||||
final UserEntity newEntity = userService.update(3L, new UserEntity(1L, test, "test", "test", "test"));
|
||||
Assertions.assertEquals(8, userService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, userService.get(3L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void deleteTest() {
|
||||
userService.delete(3L);
|
||||
Assertions.assertEquals(7, userService.getAll().size());
|
||||
final UserEntity last = userService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final UserEntity newEntity = userService.create(new UserEntity(null, "Игровая приставка", "", "", ""));
|
||||
Assertions.assertEquals(8, userService.getAll().size());
|
||||
Assertions.assertEquals(9, newEntity.getId());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user