StruckCroissant / Game-DB

Game-DB is a game database search engine deployed on Docker and powered by Java Spring Boot and AngularJS
GNU General Public License v3.0
2 stars 0 forks source link

- add tests for exception handling #31

Closed github-actions[bot] closed 2 years ago

github-actions[bot] commented 2 years ago

https://github.com/StruckCroissant/Game-DB/blob/b3372c8a86e09809278637e39f3f2085f5bdfff8/api/test/java/com/StruckCroissant/GameDB/core/user/UserDAOImplTest.java#L29


package com.StruckCroissant.GameDB.core.user;

import com.StruckCroissant.GameDB.core.game.Game;
import com.StruckCroissant.GameDB.TestDbConfig;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.JdbcTestUtils;

import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations = "classpath:test.properties")
@ContextConfiguration(classes = {TestDbConfig.class, UserDAOImpl.class})
class UserDAOImplTest {
  // TODO - add tests for exception handling

  @Qualifier("db-user")
  @Autowired
  private UserDAOImpl underTest;

  @Qualifier("testTemplate")
  @Autowired
  private JdbcTemplate jdbcTemplate;

  @AfterEach
  void tearDown(){
    JdbcTestUtils.deleteFromTables(jdbcTemplate, "user");
  }

  @NotNull
  private String insertDefaultUserDb() {
    User user = new User("test_username", "test_password", UserRoleEnum.USER, false, true);
    underTest.insertUser(user);
    return "test_username";
  }

  @NotNull
  private String insertUserDb(String username) {
    User user = new User(username, "test_password", UserRoleEnum.USER, false, true);
    underTest.insertUser(user);
    return username;
  }

  @NotNull
  private Integer getUidFromUserObj(User userFromDb) {
    return userFromDb.getId().orElseThrow();
  }

  @NotNull
  private Integer getTestUidFromUsernameDb(String test_username) {
    return underTest.selectUserByUsername(test_username).flatMap(User::getId)
        .orElseThrow(
            () -> new IllegalStateException("User not found in DB")
        );
  }

  @Test
  @Order(1)
  void shouldInsertAndSelectNewUser() {
    // given
    String test_username = insertDefaultUserDb();

    // when
    boolean userExists = underTest.selectUserByUsername(test_username).isPresent();
StruckCroissant commented 2 years ago

Fixed as of ace2bc3