diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 6a3a6ba6..adc21f57 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -10,43 +10,42 @@ name: Java CI with Maven on: push: - branches: [ "main","development" ] + branches: ["main", "development"] pull_request: - branches: [ "main","development" ] + branches: ["main", "development"] jobs: build: - runs-on: ubuntu-latest - + steps: - - uses: actions/checkout@v3 - - name: Set up JDK 20.0.2 - uses: actions/setup-java@v3 - with: - java-version: '20.0.2' - distribution: 'temurin' - cache: maven - - - name: Set up Environment Variables - env: - URL: ${{ secrets.URL }} - USER_NAME: ${{ secrets.USER_NAME }} - PASSWORD: ${{ secrets.PASSWORD }} - ACCESS_KEY: ${{ secrets.ACCESS_KEY }} - SECRET_KEY: ${{ secrets.SECRET_KEY }} - URL_TEST: ${{ secrets.URL_TEST }} - USER_NAME_TEST: ${{ secrets.USER_NAME_TEST }} - PASSWORD_TEST: ${{ secrets.PASSWORD_TEST }} - run: | - echo "URL=$URL" > $GITHUB_WORKSPACE/.env - echo "USER_NAME=$USER_NAME" >> $GITHUB_WORKSPACE/.env - echo "PASSWORD=$PASSWORD" >> $GITHUB_WORKSPACE/.env - echo "ACCESS_KEY=$ACCESS_KEY" >> $GITHUB_WORKSPACE/.env - echo "SECRET_KEY=$SECRET_KEY" >> $GITHUB_WORKSPACE/.env - echo "URL_TEST=$URL_TEST" >> $GITHUB_WORKSPACE/.env - echo "USER_NAME_TEST=$USER_NAME_TEST" >> $GITHUB_WORKSPACE/.env - echo "PASSWORD_TEST=$PASSWORD_TEST" >> $GITHUB_WORKSPACE/.env - - - name: Build with Maven - run: mvn -B package --file pom.xml \ No newline at end of file + - uses: actions/checkout@v3 + - name: Set up JDK 20.0.2 + uses: actions/setup-java@v3 + with: + java-version: "20.0.2" + distribution: "temurin" + cache: maven + + - name: Set up Environment Variables + env: + URL: ${{ secrets.URL }} + USER_NAME: ${{ secrets.USER_NAME }} + PASSWORD: ${{ secrets.PASSWORD }} + ACCESS_KEY: ${{ secrets.ACCESS_KEY }} + SECRET_KEY: ${{ secrets.SECRET_KEY }} + URL_TEST: ${{ secrets.URL_TEST }} + USER_NAME_TEST: ${{ secrets.USER_NAME_TEST }} + PASSWORD_TEST: ${{ secrets.PASSWORD_TEST }} + run: | + echo "URL=$URL" > $GITHUB_WORKSPACE/.env + echo "USER_NAME=$USER_NAME" >> $GITHUB_WORKSPACE/.env + echo "PASSWORD=$PASSWORD" >> $GITHUB_WORKSPACE/.env + echo "ACCESS_KEY=$ACCESS_KEY" >> $GITHUB_WORKSPACE/.env + echo "SECRET_KEY=$SECRET_KEY" >> $GITHUB_WORKSPACE/.env + echo "URL_TEST=$URL_TEST" >> $GITHUB_WORKSPACE/.env + echo "USER_NAME_TEST=$USER_NAME_TEST" >> $GITHUB_WORKSPACE/.env + echo "PASSWORD_TEST=$PASSWORD_TEST" >> $GITHUB_WORKSPACE/.env + + - name: Build with Maven + run: mvn -B package --file pom.xml diff --git a/src/main/java/com/casaculturaqxd/sgec/jdbc/DatabasePostgres.java b/src/main/java/com/casaculturaqxd/sgec/jdbc/DatabasePostgres.java index 1763cffe..5ebf5512 100644 --- a/src/main/java/com/casaculturaqxd/sgec/jdbc/DatabasePostgres.java +++ b/src/main/java/com/casaculturaqxd/sgec/jdbc/DatabasePostgres.java @@ -86,7 +86,7 @@ public void setSenha(String passwordKey) throws IncorrectEnvironmentVariableExce } private boolean variavelEnvExists(String envKey) { - return dotenv.get(envKey) == null; + return dotenv.get(envKey) != null; } } diff --git a/src/test/java/com/casaculturaqxd/sgec/jdbc/TestDatabasePostgres.java b/src/test/java/com/casaculturaqxd/sgec/jdbc/TestDatabasePostgres.java new file mode 100644 index 00000000..ce82dfb7 --- /dev/null +++ b/src/test/java/com/casaculturaqxd/sgec/jdbc/TestDatabasePostgres.java @@ -0,0 +1,62 @@ +package com.casaculturaqxd.sgec.jdbc; + +import java.sql.Connection; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + + +public class TestDatabasePostgres { + DatabasePostgres database; + String INVALID_ENV = ""; + + public TestDatabasePostgres() { + database = DatabasePostgres.getInstance("URL_TEST", "USER_NAME_TEST", "PASSWORD_TEST"); + INVALID_ENV = ""; + } + + @BeforeEach + public void setUp() throws IncorrectEnvironmentVariableException { + database = DatabasePostgres.getInstance("URL_TEST", "USER_NAME_TEST", "PASSWORD_TEST"); + } + + + @Test + public void testUniqueInstance() { + Connection firstConnection = database.getConnection(); + Connection secondConnection = database.getConnection(); + assertEquals(firstConnection, secondConnection); + } + + + @Test + public void testDesconectarOnValidConnection() { + database.getConnection(); + assertDoesNotThrow(() -> { + database.desconectar(database.getConnection()); + }); + } + + @Test + public void testSetInvalidUrlDataBase() throws IncorrectEnvironmentVariableException { + assertThrows(IncorrectEnvironmentVariableException.class, () -> { + database.setUrlDataBase(INVALID_ENV); + }); + } + + @Test + public void testSetInvalidNomeUsuario() { + assertThrows(IncorrectEnvironmentVariableException.class, () -> { + database.setNomeUsuario(INVALID_ENV); + }); + } + + @Test + public void testSetInvalidSenha() { + assertThrows(IncorrectEnvironmentVariableException.class, () -> { + database.setSenha(INVALID_ENV); + }); + } + +}