Created
August 30, 2021 11:31
-
-
Save JuliaKrivonos/cb0a0b19f0e7ca186360a5f4f9f2609c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.jazzteam.fitnesclub.service.impl; | |
| import com.google.common.collect.Lists; | |
| import lombok.extern.slf4j.Slf4j; | |
| import org.apache.ibatis.io.Resources; | |
| import org.apache.ibatis.jdbc.ScriptRunner; | |
| import org.jazzteam.fitnesclub.dto.InventoryDto; | |
| import org.jazzteam.fitnesclub.dto.InventoryTypeDto; | |
| import org.jazzteam.fitnesclub.mappers.InventoriesMapper; | |
| import org.jazzteam.fitnesclub.mappers.InventoryTypeMapper; | |
| import org.jazzteam.fitnesclub.model.Inventory; | |
| import org.jazzteam.fitnesclub.model.InventoryType; | |
| import org.jazzteam.fitnesclub.repository.addictional.InventoryRepository; | |
| import org.jazzteam.fitnesclub.repository.addictional.InventoryTypeRepository; | |
| import org.jazzteam.fitnesclub.repository.addictional.impl.InventoryTypeRepositoryImpl; | |
| import org.jazzteam.fitnesclub.repository.jdbc.ConnectionUtil; | |
| import org.jazzteam.fitnesclub.repository.jdbc.JdbcInventoryRepository; | |
| import org.jazzteam.fitnesclub.service.InventoryService; | |
| import org.junit.jupiter.api.AfterEach; | |
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.Test; | |
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileReader; | |
| import java.nio.charset.Charset; | |
| import java.nio.charset.StandardCharsets; | |
| import java.sql.Connection; | |
| import java.sql.PreparedStatement; | |
| import java.sql.SQLException; | |
| import java.util.List; | |
| import static org.hamcrest.MatcherAssert.assertThat; | |
| import static org.hamcrest.Matchers.*; | |
| @Slf4j | |
| class InventoryServiceTest { | |
| private InventoryTypeRepository inventoryTypeRepository; | |
| private InventoryRepository inventoryRepository; | |
| private InventoryService inventoryService; | |
| private InventoryType inventoryTypeForSave; | |
| private InventoryType inventoryTypeForUpdate; | |
| private Connection connection; | |
| @BeforeEach | |
| void setUp() throws FileNotFoundException { | |
| try(Connection connection = ConnectionUtil.getConnection()) { | |
| ScriptRunner scriptRunner = new ScriptRunner(connection); | |
| Resources.setCharset(StandardCharsets.UTF_8); | |
| scriptRunner.setLogWriter(null); | |
| scriptRunner.runScript(new FileReader(new File("src/test/resources/create_inventories.sql"))); | |
| scriptRunner.closeConnection(); | |
| } catch (SQLException e) { | |
| log.error("{}",e); | |
| } | |
| inventoryTypeRepository = new InventoryTypeRepositoryImpl(); | |
| inventoryRepository = new JdbcInventoryRepository(); | |
| inventoryService = new InventoryServiceImpl(); | |
| inventoryTypeForSave = inventoryTypeRepository | |
| .save(new InventoryType(null, | |
| "RESISTANCE", | |
| Lists.newArrayList(), | |
| Lists.newArrayList())); | |
| inventoryTypeForUpdate = inventoryTypeRepository | |
| .save(new InventoryType(null, | |
| "ELLIPTICAL", | |
| Lists.newArrayList(), | |
| Lists.newArrayList())); | |
| } | |
| @AfterEach | |
| void tearDown() { | |
| try(Connection connection = ConnectionUtil.getConnection()) { | |
| ScriptRunner scriptRunner = new ScriptRunner(connection); | |
| Resources.setCharset(StandardCharsets.UTF_8); | |
| scriptRunner.setLogWriter(null); | |
| scriptRunner.runScript(new FileReader(new File("src/test/resources/drop_tables.sql"))); | |
| scriptRunner.closeConnection(); | |
| } catch (SQLException | FileNotFoundException e) { | |
| log.error("{}",e); | |
| } | |
| } | |
| @Test | |
| void saveInventory() { | |
| InventoryType saved = inventoryTypeRepository.save(inventoryTypeForSave); | |
| Integer amount = 20; | |
| InventoryDto inventoryDto = new InventoryDto(null, InventoryTypeMapper.mapToDto(saved), amount); | |
| InventoryDto savedInventory = inventoryService.saveInventory(inventoryDto); | |
| Inventory inventoryFromDto = new Inventory( | |
| savedInventory.getId(), | |
| inventoryTypeRepository.findById(inventoryDto.getInventoryType().getId()), | |
| inventoryDto.getAmount(), | |
| Lists.newArrayList() | |
| ); | |
| assertThat(inventoryRepository.findAll().toArray(), hasItemInArray(inventoryFromDto)); | |
| } | |
| @Test | |
| void deleteInventory() { | |
| InventoryType saved = inventoryTypeRepository.save(inventoryTypeForSave); | |
| Integer amount = 20; | |
| InventoryDto inventoryDto = new InventoryDto(null, InventoryTypeMapper.mapToDto(saved), amount); | |
| InventoryDto savedInventory = inventoryService.saveInventory(inventoryDto); | |
| Inventory inventoryFromDto = new Inventory( | |
| savedInventory.getId(), | |
| inventoryTypeRepository.findById(inventoryDto.getInventoryType().getId()), | |
| inventoryDto.getAmount(), | |
| Lists.newArrayList() | |
| ); | |
| assertThat(inventoryRepository.findAll().toArray(), hasItemInArray(inventoryFromDto)); | |
| inventoryService.deleteInventory(inventoryFromDto.getId()); | |
| assertThat(inventoryRepository.findAll().toArray(), not(hasItemInArray(inventoryFromDto))); | |
| } | |
| @Test | |
| void saveInventoryType() { | |
| InventoryTypeDto inventoryTypeDto = InventoryTypeMapper.mapToDto(inventoryTypeForSave); | |
| InventoryTypeDto inventoryTypeSaved = inventoryService.saveInventoryType(inventoryTypeDto); | |
| InventoryType inventoryTypeFromDto = new InventoryType( | |
| inventoryTypeSaved.getId(), | |
| inventoryTypeDto.getType(), | |
| Lists.newArrayList(), | |
| Lists.newArrayList()); | |
| assertThat(inventoryTypeRepository.findAll().toArray(), hasItemInArray(inventoryTypeFromDto)); | |
| } | |
| @Test | |
| void deleteInventoryType() { | |
| InventoryTypeDto inventoryTypeDto = InventoryTypeMapper.mapToDto(inventoryTypeForSave); | |
| InventoryTypeDto inventoryTypeSaved = inventoryService.saveInventoryType(inventoryTypeDto); | |
| InventoryType inventoryTypeFromDto = new InventoryType( | |
| inventoryTypeSaved.getId(), | |
| inventoryTypeDto.getType(), | |
| Lists.newArrayList(), | |
| Lists.newArrayList()); | |
| assertThat(inventoryTypeRepository.findAll().toArray(), hasItemInArray(inventoryTypeFromDto)); | |
| inventoryService.deleteInventoryType(inventoryTypeFromDto.getId()); | |
| assertThat(inventoryTypeRepository.findAll().toArray(), not(hasItemInArray(inventoryTypeFromDto))); | |
| } | |
| @Test | |
| void updateInventory() { | |
| InventoryType saved = inventoryTypeRepository.save(inventoryTypeForSave); | |
| InventoryType updated = inventoryTypeRepository.save(inventoryTypeForUpdate); | |
| Integer amount = 20; | |
| Integer newAmount = 10; | |
| Inventory inventoryForUpdate = new Inventory(null, saved, amount, Lists.newArrayList()); | |
| Inventory savedInventory = inventoryRepository.save(inventoryForUpdate); | |
| inventoryForUpdate.setId(savedInventory.getId()); | |
| assertThat(inventoryRepository.findAll().toArray(), hasItemInArray(savedInventory)); | |
| InventoryDto inventoryDto = new InventoryDto( | |
| savedInventory.getId(), | |
| InventoryTypeMapper.mapToDto(updated), | |
| newAmount | |
| ); | |
| inventoryForUpdate.setAmount(inventoryDto.getAmount()); | |
| inventoryForUpdate.setInventoryType(inventoryTypeRepository.findById(inventoryDto.getInventoryType().getId())); | |
| inventoryService.updateInventory(inventoryDto); | |
| assertThat(inventoryRepository.findAll().toArray(), hasItemInArray(inventoryForUpdate)); | |
| } | |
| @Test | |
| void updateInventoryType() { | |
| InventoryTypeDto inventoryTypeDto = InventoryTypeMapper.mapToDto(inventoryTypeForSave); | |
| InventoryType inventoryTypeForUpdate = inventoryTypeRepository.save(inventoryTypeForSave); | |
| inventoryTypeDto.setId(inventoryTypeForUpdate.getId()); | |
| InventoryType inventoryTypeFromDto = new InventoryType( | |
| inventoryTypeDto.getId(), | |
| inventoryTypeDto.getType(), | |
| Lists.newArrayList(), | |
| Lists.newArrayList()); | |
| assertThat(inventoryTypeRepository.findAll().toArray(), hasItemInArray(inventoryTypeFromDto)); | |
| inventoryTypeForUpdate.setType("Test_Update"); | |
| inventoryService.updateInventoryType(inventoryTypeDto); | |
| assertThat(inventoryTypeRepository.findAll().toArray(), hasItemInArray(inventoryTypeForUpdate)); | |
| } | |
| @Test | |
| void findAllInventory() { | |
| InventoryType savedType1 = inventoryTypeRepository.save(inventoryTypeForSave); | |
| InventoryType savedType2 = inventoryTypeRepository.save(inventoryTypeForUpdate); | |
| Inventory inventory1 = new Inventory(null, savedType1, 4, Lists.newArrayList()); | |
| Inventory inventory2 = new Inventory(null, savedType2, 20, Lists.newArrayList()); | |
| Inventory savedInventory1 = inventoryRepository.save(inventory1); | |
| Inventory savedInventory2 = inventoryRepository.save(inventory2); | |
| inventory1.setId(savedInventory1.getId()); | |
| inventory2.setId(savedInventory2.getId()); | |
| List<InventoryDto> allInventory = inventoryService.findAllInventory(); | |
| assertThat(allInventory, containsInAnyOrder(InventoriesMapper.mapToDto(inventory1), InventoriesMapper.mapToDto(inventory2))); | |
| } | |
| @Test | |
| void testFindAllInventory() { | |
| InventoryType savedType1 = inventoryTypeRepository.save(inventoryTypeForSave); | |
| InventoryType savedType2 = inventoryTypeRepository.save(inventoryTypeForUpdate); | |
| Inventory inventory1 = new Inventory(null, savedType1, 4, Lists.newArrayList()); | |
| Inventory inventory2 = new Inventory(null, savedType2, 20, Lists.newArrayList()); | |
| InventoryDto savedInventory1 = InventoriesMapper.mapToDto(inventoryRepository.save(inventory1)); | |
| InventoryDto savedInventory2 = InventoriesMapper.mapToDto(inventoryRepository.save(inventory2)); | |
| List<InventoryDto> allInventory = inventoryService.findAllInventory(inventory -> inventory.getAmount() > 4); | |
| assertThat(allInventory, hasItems(savedInventory2)); | |
| assertThat(allInventory, not(hasItems(savedInventory1))); | |
| } | |
| @Test | |
| void findAllInventoryListType() { | |
| InventoryType inventory1 = new InventoryType(null, "savedType1", Lists.newArrayList(), Lists.newArrayList()); | |
| InventoryType inventory2 = new InventoryType(null, "savedType2", Lists.newArrayList(), Lists.newArrayList()); | |
| InventoryTypeDto savedType1 = InventoryTypeMapper.mapToDto(inventoryTypeRepository.save(inventory1)); | |
| InventoryTypeDto savedType2 = InventoryTypeMapper.mapToDto(inventoryTypeRepository.save(inventory2)); | |
| inventory1.setId(savedType1.getId()); | |
| inventory1.setId(savedType2.getId()); | |
| List<InventoryTypeDto> allInventoryTypes = inventoryService.findAllInventoryListType(); | |
| assertThat(allInventoryTypes, hasItems(savedType1,savedType2)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment