-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2270: wip - settings.yml not being read for some classes?
- Loading branch information
Showing
6 changed files
with
165 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
77 changes: 77 additions & 0 deletions
77
src/test/java/stirling/software/SPDF/config/security/database/DatabaseConfigTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package stirling.software.SPDF.config.security.database; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import stirling.software.SPDF.model.ApplicationProperties; | ||
import stirling.software.SPDF.model.provider.UnsupportedProviderException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DatabaseConfigTest { | ||
|
||
@Mock | ||
private ApplicationProperties applicationProperties; | ||
|
||
@InjectMocks | ||
private DatabaseConfig databaseConfig; | ||
|
||
@Test | ||
void testDefaultConfigurationForDataSource() throws UnsupportedProviderException { | ||
var system = mock(ApplicationProperties.System.class); | ||
var datasource = mock(ApplicationProperties.Datasource.class); | ||
var testUrl = "jdbc:h2:mem:test"; | ||
|
||
when(applicationProperties.getSystem()).thenReturn(system); | ||
when(system.getDatasource()).thenReturn(datasource); | ||
when(datasource.isUseDefault()).thenReturn(true); | ||
when(datasource.getDefaultUrl()).thenReturn(testUrl); | ||
|
||
var result = databaseConfig.dataSource(); | ||
|
||
assertInstanceOf(DataSource.class, result); | ||
} | ||
|
||
@Test | ||
void testCustomConfigurationForDataSource() throws UnsupportedProviderException { | ||
var system = mock(ApplicationProperties.System.class); | ||
var datasource = mock(ApplicationProperties.Datasource.class); | ||
|
||
when(applicationProperties.getSystem()).thenReturn(system); | ||
when(system.getDatasource()).thenReturn(datasource); | ||
when(datasource.isUseDefault()).thenReturn(false); | ||
when(datasource.getType()).thenReturn("postgresql"); | ||
when(datasource.getHostName()).thenReturn("localhost"); | ||
when(datasource.getPort()).thenReturn(5432); | ||
when(datasource.getName()).thenReturn("postgres"); | ||
when(datasource.getUsername()).thenReturn("postgres"); | ||
when(datasource.getPassword()).thenReturn("postgres"); | ||
|
||
var result = databaseConfig.dataSource(); | ||
|
||
assertInstanceOf(DataSource.class, result); | ||
} | ||
|
||
@ParameterizedTest(name = "Exception thrown when the DB type [{arguments}] is not supported") | ||
@ValueSource(strings = {"oracle", "mysql", "mongoDb"}) | ||
void exceptionThrownWhenDBTypeIsUnsupported(String datasourceType) { | ||
var system = mock(ApplicationProperties.System.class); | ||
var datasource = mock(ApplicationProperties.Datasource.class); | ||
|
||
when(applicationProperties.getSystem()).thenReturn(system); | ||
when(system.getDatasource()).thenReturn(datasource); | ||
when(datasource.isUseDefault()).thenReturn(false); | ||
when(datasource.getType()).thenReturn(datasourceType); | ||
|
||
assertThrows(UnsupportedProviderException.class, () -> databaseConfig.dataSource()); | ||
} | ||
} |