parent
c0100c5d68
commit
951904394b
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,135 @@ |
||||
package com.apside.assistDbBackend.repository; |
||||
|
||||
import com.apside.assistDbBackend.model.InfoColumn; |
||||
import com.apside.assistDbBackend.model.LinkInfo; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
||||
@DataJpaTest |
||||
class InfoColumnRepositoryTest { |
||||
|
||||
@Autowired |
||||
private InfoColumnRepository infoColumnRepository; |
||||
|
||||
@Autowired |
||||
private LinkInfoRepository linkInfoRepository; |
||||
|
||||
@Test |
||||
void testGetSelectedColumns() { |
||||
// Arrange
|
||||
InfoColumn infoColumn1 = new InfoColumn(1L, "Column1", "DataType1", 10, "Column Text 1"); |
||||
InfoColumn infoColumn2 = new InfoColumn(2L, "Column2", "DataType2", 20, "Column Text 2"); |
||||
// Save the InfoColumns to the database
|
||||
infoColumnRepository.save(infoColumn1); |
||||
infoColumnRepository.save(infoColumn2); |
||||
List<LinkInfo> linkInfos = new ArrayList<>(); |
||||
linkInfos.add(new LinkInfo(1L, "table1", "schema1", 1)); |
||||
linkInfos.add(new LinkInfo(2L, "table2", "schema1", 2)); |
||||
linkInfoRepository.saveAll(linkInfos); |
||||
|
||||
// Act
|
||||
Iterable<InfoColumn> selectedColumns = infoColumnRepository.getSelectedColumns("table1", "schema1"); |
||||
|
||||
// Assert
|
||||
List<InfoColumn> columns = new ArrayList<>(); |
||||
selectedColumns.forEach(columns::add); |
||||
assertEquals(1, columns.size()); |
||||
assertEquals("Column1", columns.get(0).getNameColumn()); |
||||
// Perform assertions on the selected columns
|
||||
// ...
|
||||
} |
||||
|
||||
@Test |
||||
void testGetColumnsForJoin() { |
||||
// Arrange
|
||||
InfoColumn infoColumn1 = new InfoColumn(1L, "Column1", "DataType1", 10, "Column Text 1"); |
||||
InfoColumn infoColumn2 = new InfoColumn(2L, "Column2", "DataType2", 20, "Column Text 2"); |
||||
// Save the InfoColumns to the database
|
||||
infoColumnRepository.save(infoColumn1); |
||||
infoColumnRepository.save(infoColumn2); |
||||
List<LinkInfo> linkInfos = new ArrayList<>(); |
||||
linkInfos.add(new LinkInfo(1L, "table1", "schema1", 1)); |
||||
linkInfos.add(new LinkInfo(2L, "table2", "schema2", 1)); |
||||
linkInfoRepository.saveAll(linkInfos); |
||||
|
||||
// Act
|
||||
Iterable<InfoColumn> columnsForJoin = infoColumnRepository.getColumnsForJoin("table1", "table2", "schema1", "schema2"); |
||||
|
||||
// Assert
|
||||
List<InfoColumn> columns = new ArrayList<>(); |
||||
columnsForJoin.forEach(columns::add); |
||||
assertEquals(1, columns.size()); |
||||
// Perform assertions on the columns for join
|
||||
// ...
|
||||
} |
||||
|
||||
@Test |
||||
void testGetColumnsForJoinTwo() { |
||||
// Arrange
|
||||
InfoColumn infoColumn1 = new InfoColumn(1L, "Column1", "DataType1", 10, "Column Text 1"); |
||||
InfoColumn infoColumn2 = new InfoColumn(2L, "Column2", "DataType2", 20, "Column Text 2"); |
||||
// Save the InfoColumns to the database
|
||||
infoColumnRepository.save(infoColumn1); |
||||
infoColumnRepository.save(infoColumn2); |
||||
List<LinkInfo> linkInfos = new ArrayList<>(); |
||||
linkInfos.add(new LinkInfo(1L, "table1", "schema1", 1)); |
||||
linkInfos.add(new LinkInfo(2L, "table2", "schema2", 1)); |
||||
linkInfoRepository.saveAll(linkInfos); |
||||
|
||||
// Act
|
||||
List<String> tables = new ArrayList<>(); |
||||
tables.add("table1"); |
||||
tables.add("table2"); |
||||
List<String> schemas = new ArrayList<>(); |
||||
schemas.add("schema1"); |
||||
schemas.add("schema2"); |
||||
Iterable<InfoColumn> columnsForJoinTwo = infoColumnRepository.getColumnsForJoinTwo(tables, schemas); |
||||
|
||||
// Assert
|
||||
List<InfoColumn> columns = new ArrayList<>(); |
||||
columnsForJoinTwo.forEach(columns::add); |
||||
assertEquals(1, columns.size()); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testGetSpecColumn() { |
||||
// Arrange
|
||||
InfoColumn infoColumn = new InfoColumn(1L, "Column1", "DataType1", 10, "Column Text 1"); |
||||
// Save the InfoColumn to the database
|
||||
infoColumnRepository.save(infoColumn); |
||||
|
||||
// Act
|
||||
InfoColumn specColumn = infoColumnRepository.getSpecColumn("Column1", "DataType1", 10, "Column Text 1"); |
||||
|
||||
// Assert
|
||||
assertEquals(infoColumn.getId(), specColumn.getId()); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testTruncateMyColumn() { |
||||
// Arrange
|
||||
InfoColumn infoColumn1 = new InfoColumn(1L, "Column1", "DataType1", 10, "Column Text 1"); |
||||
InfoColumn infoColumn2 = new InfoColumn(2L, "Column2", "DataType2", 20, "Column Text 2"); |
||||
// Save the InfoColumns to the database
|
||||
infoColumnRepository.save(infoColumn1); |
||||
infoColumnRepository.save(infoColumn2); |
||||
|
||||
// Act
|
||||
infoColumnRepository.truncateMyColumn(); |
||||
|
||||
// Assert
|
||||
Iterable<InfoColumn> columns = infoColumnRepository.findAll(); |
||||
List<InfoColumn> columnList = new ArrayList<>(); |
||||
columns.forEach(columnList::add); |
||||
assertEquals(0, columnList.size()); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,97 @@ |
||||
package com.apside.assistDbBackend.repository; |
||||
|
||||
import com.apside.assistDbBackend.model.InfoTable; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
|
||||
@DataJpaTest |
||||
class InfoTableRepositoryTest { |
||||
|
||||
@Autowired |
||||
private InfoTableRepository infoTableRepository; |
||||
|
||||
@Test |
||||
void testGetAllSchemas() { |
||||
// Arrange
|
||||
List<InfoTable> infoTables = new ArrayList<>(); |
||||
infoTables.add(new InfoTable(1L, "table1", "schema1", "table1text")); |
||||
infoTables.add(new InfoTable(2L, "table2", "schema2", "table2text")); |
||||
infoTables.add(new InfoTable(3L, "table3", "schema1", "table3text")); |
||||
|
||||
infoTableRepository.saveAll(infoTables); |
||||
|
||||
// Act
|
||||
Iterable<String> schemas = infoTableRepository.getAllSchemas(); |
||||
|
||||
// Assert
|
||||
List<String> schemaList = new ArrayList<>(); |
||||
schemas.forEach(schemaList::add); |
||||
assertEquals(2, schemaList.size()); |
||||
assertEquals("schema1", schemaList.get(0)); |
||||
assertEquals("schema2", schemaList.get(1)); |
||||
} |
||||
|
||||
@Test |
||||
void testGetTablesBySchemaName() { |
||||
// Arrange
|
||||
List<InfoTable> infoTables = new ArrayList<>(); |
||||
infoTables.add(new InfoTable(1L, "table1", "schema1", "table1text")); |
||||
infoTables.add(new InfoTable(2L, "table2", "schema1", "table2text")); |
||||
infoTables.add(new InfoTable(3L, "table3", "schema2", "table3text")); |
||||
infoTableRepository.saveAll(infoTables); |
||||
|
||||
// Act
|
||||
Iterable<InfoTable> tables = infoTableRepository.getTablesBySchemaName("schema1"); |
||||
|
||||
// Assert
|
||||
List<InfoTable> tableList = new ArrayList<>(); |
||||
tables.forEach(tableList::add); |
||||
assertEquals(2, tableList.size()); |
||||
assertEquals("table1", tableList.get(0).getNameTable()); |
||||
assertEquals("table2", tableList.get(1).getNameTable()); |
||||
} |
||||
|
||||
@Test |
||||
void testGetSchemaByTableName() { |
||||
// Arrange
|
||||
List<InfoTable> infoTables = new ArrayList<>(); |
||||
infoTables.add(new InfoTable(1L, "table1", "schema1", "table1text")); |
||||
infoTables.add(new InfoTable(2L, "table2", "schema1", "table2text")); |
||||
infoTables.add(new InfoTable(3L, "table3", "schema2", "table3text")); |
||||
infoTableRepository.saveAll(infoTables); |
||||
|
||||
// Act
|
||||
Iterable<InfoTable> schemas = infoTableRepository.getSchemaByTableName("table2"); |
||||
|
||||
// Assert
|
||||
List<InfoTable> schemaList = new ArrayList<>(); |
||||
schemas.forEach(schemaList::add); |
||||
assertEquals(1, schemaList.size()); |
||||
assertEquals("schema1", schemaList.get(0).getNameSchema()); |
||||
} |
||||
|
||||
@Test |
||||
void testTruncateMyTable() { |
||||
// Arrange
|
||||
List<InfoTable> infoTables = new ArrayList<>(); |
||||
infoTables.add(new InfoTable(1L, "table1", "schema1", "table1text")); |
||||
infoTables.add(new InfoTable(2L, "table2", "schema1", "table2text")); |
||||
infoTables.add(new InfoTable(3L, "table3", "schema2", "table3text")); |
||||
infoTableRepository.saveAll(infoTables); |
||||
|
||||
// Act
|
||||
infoTableRepository.truncateMyTable(); |
||||
|
||||
// Assert
|
||||
Iterable<InfoTable> tables = infoTableRepository.findAll(); |
||||
List<InfoTable> tableList = new ArrayList<>(); |
||||
tables.forEach(tableList::add); |
||||
assertEquals(0, tableList.size()); |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
package com.apside.assistDbBackend.repository; |
||||
|
||||
import com.apside.assistDbBackend.model.InfoTable; |
||||
import com.apside.assistDbBackend.model.LinkInfo; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
|
||||
@DataJpaTest |
||||
class LinkInfoRepositoryTest { |
||||
@Autowired |
||||
private LinkInfoRepository linkInfoRepository; |
||||
|
||||
|
||||
@Test |
||||
void truncateMyLink() { |
||||
List<LinkInfo> linkInfos = new ArrayList<>(); |
||||
linkInfos.add(new LinkInfo(1L, "table1", "schema1", 1)); |
||||
linkInfos.add(new LinkInfo(2L, "table2", "schema1", 1)); |
||||
linkInfos.add(new LinkInfo(3L, "table3", "schema2", 2)); |
||||
linkInfoRepository.saveAll(linkInfos); |
||||
|
||||
// Act
|
||||
linkInfoRepository.truncateMyLink(); |
||||
|
||||
// Assert
|
||||
Iterable<LinkInfo> links = linkInfoRepository.findAll(); |
||||
List<InfoTable> tableList = new ArrayList<>(); |
||||
links.forEach(linkInfos::add); |
||||
assertEquals(0, tableList.size()); |
||||
} |
||||
} |
@ -0,0 +1,127 @@ |
||||
//package com.apside.assistDbBackend.service;
|
||||
//
|
||||
//import org.eclipse.jgit.api.*;
|
||||
//import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
//import org.eclipse.jgit.api.errors.InvalidRemoteException;
|
||||
//import org.eclipse.jgit.api.errors.JGitInternalException;
|
||||
//import org.eclipse.jgit.api.errors.RefNotFoundException;
|
||||
//import org.eclipse.jgit.lib.Repository;
|
||||
//import org.eclipse.jgit.transport.PushResult;
|
||||
//import org.eclipse.jgit.transport.URIish;
|
||||
//import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
||||
//import org.junit.jupiter.api.AfterEach;
|
||||
//import org.junit.jupiter.api.BeforeEach;
|
||||
//import org.junit.jupiter.api.DisplayName;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.junit.runner.RunWith;
|
||||
//import org.mockito.InjectMocks;
|
||||
//import org.mockito.Mock;
|
||||
//import org.mockito.Mockito;
|
||||
//import org.mockito.MockitoAnnotations;
|
||||
//import org.mockito.junit.MockitoJUnitRunner;
|
||||
//import org.springframework.beans.factory.annotation.Value;
|
||||
//
|
||||
//import java.io.File;
|
||||
//import java.io.IOException;
|
||||
//import java.net.URISyntaxException;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.List;
|
||||
//
|
||||
//import static org.junit.Assert.assertTrue;
|
||||
//import static org.mockito.Mockito.*;
|
||||
//
|
||||
//@RunWith(MockitoJUnitRunner.class)
|
||||
//class GitServiceTest {
|
||||
//
|
||||
// @InjectMocks
|
||||
// private GitService gitService;
|
||||
//
|
||||
// @Mock
|
||||
// private Git git;
|
||||
//
|
||||
// @Mock
|
||||
// private RemoteAddCommand remoteAddCommand;
|
||||
//
|
||||
// private PushResult pushResult;
|
||||
// private PushCommand pushCommand;
|
||||
//
|
||||
// @BeforeEach
|
||||
// void setUp() throws IOException {
|
||||
// MockitoAnnotations.openMocks(this);
|
||||
// gitService = new GitService();
|
||||
// //Runtime.getRuntime().exec("cmd.exe /c ");
|
||||
// String pathForExec = new File(System.getProperty("user.dir")).getParent();
|
||||
//
|
||||
//
|
||||
// ProcessBuilder processBuilder = new ProcessBuilder();
|
||||
// processBuilder.directory(new File(pathForExec));
|
||||
// processBuilder.command("cmd.exe", "/c", "mkdir test_repo & mkdir repo & cd " + pathForExec + "/test_repo" + " & git --bare init & cd " + pathForExec + "/repo" + " & git clone " + pathForExec + "/test_repo" + " test & cd " + pathForExec + "/repo/test & mkdir change");
|
||||
//
|
||||
// processBuilder.start();
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @AfterEach
|
||||
// void closeUp() throws IOException {
|
||||
//
|
||||
// String pathForExec = new File(System.getProperty("user.dir")).getParent();
|
||||
//
|
||||
// ProcessBuilder processBuilder = new ProcessBuilder();
|
||||
// processBuilder.directory(new File(pathForExec));
|
||||
// processBuilder.command("cmd.exe", "/c", "rmdir test_repo & rmdir repo");
|
||||
//
|
||||
// processBuilder.start();
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @DisplayName("Test push to git success")
|
||||
// @Test
|
||||
// void pushToGitSuccess() {
|
||||
// // TODO
|
||||
// }
|
||||
//
|
||||
//// @DisplayName("Test push to git fail cause error with remote add command")
|
||||
//// @Test
|
||||
//// void pushToGitFailCauseGitAPIExceptionWithRemoteAddCommand() throws Exception {
|
||||
//// when(git.remoteAdd()).thenReturn(remoteAddCommand);
|
||||
//// when(remoteAddCommand.setName("origin")).thenReturn(remoteAddCommand);
|
||||
//// when(remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git"))).thenReturn(remoteAddCommand);
|
||||
////
|
||||
//// when(git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider("fhibert@apside.fr", "e54612b6d1d73eef4f0a49c88b0e35ccf02d45eb")).call()).thenThrow(JGitInternalException.class);
|
||||
////
|
||||
//// gitService.pushToGit();
|
||||
//// }
|
||||
//
|
||||
//
|
||||
// @Test
|
||||
// void pushToGit_shouldPushChangesToRemoteRepository() throws GitAPIException, IOException, URISyntaxException {
|
||||
// // Arrange
|
||||
//
|
||||
// String pathPush = new File(System.getProperty("user.dir")).getParent() + "\\test_repo";
|
||||
// // Arrange
|
||||
// when(git.remoteAdd()).thenReturn(remoteAddCommand);
|
||||
// when(remoteAddCommand.setName("origin")).thenReturn(remoteAddCommand);
|
||||
// remoteAddCommand.setUri(new URIish(pathPush));
|
||||
// when(remoteAddCommand.setUri(Mockito.any(URIish.class))).thenReturn(remoteAddCommand);
|
||||
// gitService.setAccesToken("");
|
||||
//
|
||||
// gitService.pushToGit();
|
||||
//
|
||||
// // Assert
|
||||
// verify(git, times(1)).add();
|
||||
// verify(git, times(1)).commit();
|
||||
// verify(git, times(1)).push();
|
||||
// }
|
||||
////
|
||||
//// @Test
|
||||
//// void pullFromGit_shouldPullChangesFromRemoteRepository() throws GitAPIException, IOException {
|
||||
//// // Arrange
|
||||
//// doNothing().when(git).pull();
|
||||
////
|
||||
//// // Act
|
||||
//// gitService.pullFromGit();
|
||||
////
|
||||
//// // Assert
|
||||
//// verify(git, times(1)).pull();
|
||||
//// }
|
||||
//}
|
@ -0,0 +1,228 @@ |
||||
package com.apside.assistDbBackend.service; |
||||
|
||||
import com.apside.assistDbBackend.model.InfoColumn; |
||||
import com.apside.assistDbBackend.repository.InfoColumnRepository; |
||||
import org.junit.Assert; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
import static org.assertj.core.api.Assertions.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class InfoColumnServiceTest { |
||||
@Mock |
||||
private InfoColumnRepository infoColumnRepository; |
||||
@InjectMocks |
||||
private InfoColumnService underTest; |
||||
|
||||
|
||||
@Test |
||||
void testGetColumn() { |
||||
InfoColumn infoColumn = new InfoColumn( |
||||
1L, |
||||
"test", |
||||
"test", |
||||
10, |
||||
"test" |
||||
); |
||||
Long id = 1L; |
||||
when(infoColumnRepository.findById(id)).thenReturn(Optional.of(infoColumn)); |
||||
Optional<InfoColumn> result = underTest.getColumn(id); |
||||
assertTrue(result.isPresent()); |
||||
assertEquals(infoColumn, result.get()); |
||||
} |
||||
|
||||
@Test |
||||
public void testGetColumnNotFound() { |
||||
// Given
|
||||
Long id = 1L; |
||||
when(infoColumnRepository.findById(id)).thenReturn(Optional.empty()); |
||||
|
||||
// When
|
||||
Optional<InfoColumn> result = underTest.getColumn(id); |
||||
|
||||
// Then
|
||||
assertFalse(result.isPresent()); |
||||
} |
||||
|
||||
@Test |
||||
void testGetAllColumns() { |
||||
List<InfoColumn> res = new ArrayList<>(); |
||||
res.add(new InfoColumn()); |
||||
when(infoColumnRepository.findAll()).thenReturn(res); |
||||
Iterable<InfoColumn> expected = underTest.getAllColumns(); |
||||
|
||||
assertEquals(expected, res); |
||||
} |
||||
|
||||
@Test |
||||
void testGetSelectedColumns() { |
||||
// Given
|
||||
List<InfoColumn> infoColumns = Arrays.asList( |
||||
new InfoColumn(1L, |
||||
"test", |
||||
"test", |
||||
10, |
||||
"test"), |
||||
new InfoColumn(2L, |
||||
"test2", |
||||
"test2", |
||||
11, |
||||
"test2") |
||||
); |
||||
String table = "myTable"; |
||||
String schema = "mySchema"; |
||||
when(infoColumnRepository.getSelectedColumns(table, schema)).thenReturn(infoColumns); |
||||
|
||||
// When
|
||||
Iterable<InfoColumn> result = underTest.getSelectedColumns(table, schema); |
||||
|
||||
// Then
|
||||
assertNotNull(result); |
||||
assertThat(result).hasSize(2); |
||||
assertThat(result).contains(infoColumns.get(0), infoColumns.get(1)); |
||||
} |
||||
|
||||
@Test |
||||
void testGetColumnsForJoin() { |
||||
// Given
|
||||
List<InfoColumn> infoColumns = Arrays.asList( |
||||
new InfoColumn(1L, |
||||
"test", |
||||
"test", |
||||
10, |
||||
"test"), |
||||
new InfoColumn(2L, |
||||
"test2", |
||||
"test2", |
||||
11, |
||||
"test2") |
||||
); |
||||
String firstTable = "firstTable"; |
||||
String secondTable = "secondTable"; |
||||
String firstSchema = "firstSchema"; |
||||
String secondSchema = "secondSchema"; |
||||
when(infoColumnRepository.getColumnsForJoin(firstTable, secondTable, firstSchema, secondSchema)).thenReturn(infoColumns); |
||||
|
||||
// When
|
||||
Iterable<InfoColumn> result = underTest.getColumnsForJoin(firstTable, secondTable, firstSchema, secondSchema); |
||||
|
||||
// Then
|
||||
assertNotNull(result); |
||||
assertThat(result).hasSize(2); |
||||
assertThat(result).contains(infoColumns.get(0), infoColumns.get(1)); |
||||
} |
||||
|
||||
@Test |
||||
void testGetColumnsForJoinTwo() { |
||||
// Given
|
||||
List<String> tables = Arrays.asList("table1", "table2"); |
||||
List<String> schemas = Arrays.asList("schema1", "schema2"); |
||||
List<InfoColumn> infoColumns = Arrays.asList( |
||||
new InfoColumn(1L, |
||||
"test", |
||||
"test", |
||||
10, |
||||
"test"), |
||||
new InfoColumn(2L, |
||||
"test2", |
||||
"test2", |
||||
11, |
||||
"test2"), |
||||
new InfoColumn(3L, |
||||
"test3", |
||||
"test3", |
||||
12, |
||||
"test3") |
||||
); |
||||
when(infoColumnRepository.getColumnsForJoinTwo(tables, schemas)).thenReturn(infoColumns); |
||||
|
||||
// When
|
||||
Iterable<InfoColumn> result = underTest.getColumnsForJoinTwo(tables, schemas); |
||||
|
||||
// Then
|
||||
assertNotNull(result); |
||||
assertThat(result).hasSize(3); |
||||
assertThat(result).contains(infoColumns.get(0), infoColumns.get(1), infoColumns.get(2)); |
||||
} |
||||
|
||||
@Test |
||||
void testDeleteColumn() { |
||||
// Given
|
||||
Long id = 1L; |
||||
|
||||
// When
|
||||
underTest.deleteColumn(id); |
||||
|
||||
// Then
|
||||
verify(infoColumnRepository, times(1)).deleteById(id); |
||||
} |
||||
|
||||
@Test |
||||
void testDeleteAllColumn() { |
||||
// When
|
||||
underTest.deleteAllColumn(); |
||||
|
||||
// Then
|
||||
verify(infoColumnRepository, times(1)).deleteAll(); |
||||
} |
||||
|
||||
@Test |
||||
void testAddOrUpdateColumn() { |
||||
// Given
|
||||
InfoColumn infoColumn = new InfoColumn( |
||||
1L, |
||||
"test", |
||||
"test", |
||||
10, |
||||
"test"); |
||||
|
||||
// When
|
||||
when(infoColumnRepository.save(infoColumn)).thenReturn(infoColumn); |
||||
InfoColumn savedInfoColumn = underTest.addOrUpdateColumn(infoColumn); |
||||
|
||||
// Then
|
||||
verify(infoColumnRepository, times(1)).save(infoColumn); |
||||
assertThat(savedInfoColumn.getNameColumn()).isEqualTo("test"); |
||||
} |
||||
|
||||
@Test |
||||
void testGetSpecCol() { |
||||
String nameCol = "columnName"; |
||||
String dataType = "dataType"; |
||||
int lengthCol = 100; |
||||
String columnText = "sometext"; |
||||
InfoColumn expectedInfoColumn = new InfoColumn(); |
||||
expectedInfoColumn.setNameColumn(nameCol); |
||||
expectedInfoColumn.setDataType(dataType); |
||||
expectedInfoColumn.setLengthColumn(lengthCol); |
||||
expectedInfoColumn.setColumnText(columnText); |
||||
|
||||
// When
|
||||
when(infoColumnRepository.getSpecColumn(nameCol, dataType, lengthCol, columnText)).thenReturn(expectedInfoColumn); |
||||
InfoColumn actualInfoColumn = underTest.getSpecCol(nameCol, dataType, lengthCol, columnText); |
||||
|
||||
// Then
|
||||
verify(infoColumnRepository, times(1)).getSpecColumn(nameCol, dataType, lengthCol, columnText); |
||||
assertThat(actualInfoColumn).isEqualTo(expectedInfoColumn); |
||||
} |
||||
|
||||
@Test |
||||
void testTruncateMyColumn() { |
||||
// When
|
||||
underTest.truncateMyColumn(); |
||||
|
||||
// Then
|
||||
verify(infoColumnRepository, times(1)).truncateMyColumn(); |
||||
} |
||||
} |
@ -0,0 +1,157 @@ |
||||
package com.apside.assistDbBackend.service; |
||||
|
||||
import com.apside.assistDbBackend.model.InfoColumn; |
||||
import com.apside.assistDbBackend.model.InfoTable; |
||||
import com.apside.assistDbBackend.repository.InfoTableRepository; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class InfoTableServiceTest { |
||||
|
||||
@Mock |
||||
private InfoTableRepository infoTableRepository; |
||||
|
||||
@InjectMocks |
||||
private InfoTableService infoTableService; |
||||
|
||||
@Test |
||||
void testGetTable() { |
||||
InfoTable infoTable = new InfoTable( |
||||
1L, |
||||
"nameTable", |
||||
"nameSchema", |
||||
"tableText" |
||||
); |
||||
Long id = 1L; |
||||
when(infoTableRepository.findById(id)).thenReturn(Optional.of(infoTable)); |
||||
Optional<InfoTable> result = infoTableService.getTable(id); |
||||
assertTrue(result.isPresent()); |
||||
assertEquals(infoTable, result.get()); |
||||
} |
||||
|
||||
@Test |
||||
void testGetAllTables() { |
||||
List<InfoTable> res = new ArrayList<>(); |
||||
res.add(new InfoTable()); |
||||
when(infoTableRepository.findAll()).thenReturn(res); |
||||
Iterable<InfoTable> expected = infoTableService.getAllTables(); |
||||
|
||||
assertEquals(expected, res); |
||||
} |
||||
|
||||
@Test |
||||
void testGetTablesBySchemaName() { |
||||
String schemaName = "nameSchema"; |
||||
List<InfoTable> infoTables = Arrays.asList( |
||||
new InfoTable( |
||||
1L, |
||||
"nameTableOne", |
||||
"nameSchema", |
||||
"tableTextOne"), |
||||
new InfoTable( |
||||
2L, |
||||
"nameTableTwo", |
||||
"nameSchema", |
||||
"tableTextTwo") |
||||
); |
||||
when(infoTableRepository.getTablesBySchemaName(schemaName)).thenReturn(infoTables); |
||||
|
||||
// When
|
||||
Iterable<InfoTable> result = infoTableService.getTablesBySchemaName(schemaName); |
||||
|
||||
// Then
|
||||
assertNotNull(result); |
||||
assertThat(result).hasSize(2); |
||||
assertThat(result).contains(infoTables.get(0), infoTables.get(1)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testGetSchemaByTableName() { |
||||
String tableName = "nameTable"; |
||||
List<InfoTable> infoTables = Arrays.asList( |
||||
new InfoTable( |
||||
1L, |
||||
"nameTable", |
||||
"nameSchemaOne", |
||||
"tableTextOne"), |
||||
new InfoTable( |
||||
2L, |
||||
"nameTable", |
||||
"nameSchemaTwo", |
||||
"tableTextTwo") |
||||
); |
||||
when(infoTableRepository.getSchemaByTableName(tableName)).thenReturn(infoTables); |
||||
|
||||
// When
|
||||
Iterable<InfoTable> result = infoTableService.getSchemaByTableName(tableName); |
||||
|
||||
// Then
|
||||
assertNotNull(result); |
||||
assertThat(result).hasSize(2); |
||||
assertThat(result).contains(infoTables.get(0), infoTables.get(1)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testGetAllSchemas() { |
||||
List<String> schemasList = Arrays.asList(new String("shcemaOne"), new String("schemaTwo")); |
||||
when(infoTableRepository.getAllSchemas()).thenReturn(schemasList); |
||||
Iterable<String> expectedAllSchemas = infoTableService.getAllSchemas(); |
||||
assertEquals(expectedAllSchemas, schemasList); |
||||
} |
||||
|
||||
@Test |
||||
void testDeleteTable() { |
||||
Long id = 1L; |
||||
|
||||
infoTableService.deleteTable(id); |
||||
|
||||
verify(infoTableRepository, times(1)).deleteById(id); |
||||
} |
||||
|
||||
@Test |
||||
void testDeleteAllTable() { |
||||
infoTableService.deleteAllTable(); |
||||
|
||||
verify(infoTableRepository, times(1)).deleteAll(); |
||||
} |
||||
|
||||
@Test |
||||
void testAddOrUpdateTable() { |
||||
InfoTable infoTable = new InfoTable( |
||||
1L, |
||||
"nameTable", |
||||
"nameSchema", |
||||
"tableText"); |
||||
|
||||
// When
|
||||
when(infoTableRepository.save(infoTable)).thenReturn(infoTable); |
||||
InfoTable savedInfoTable = infoTableService.addOrUpdateTable(infoTable); |
||||
|
||||
// Then
|
||||
verify(infoTableRepository, times(1)).save(infoTable); |
||||
assertThat(savedInfoTable.getNameTable()).isEqualTo("nameTable"); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testTruncateMyTable() { |
||||
infoTableService.truncateMyTable(); |
||||
|
||||
verify(infoTableRepository, times(1)).truncateMyTable(); |
||||
} |
||||
} |
@ -0,0 +1,96 @@ |
||||
package com.apside.assistDbBackend.service; |
||||
|
||||
import com.apside.assistDbBackend.model.InfoColumn; |
||||
import com.apside.assistDbBackend.model.InfoTable; |
||||
import com.apside.assistDbBackend.model.LinkInfo; |
||||
import com.apside.assistDbBackend.repository.LinkInfoRepository; |
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class LinkInfoServiceTest { |
||||
|
||||
@Mock |
||||
private LinkInfoRepository linkInfoRepository; |
||||
@InjectMocks |
||||
private LinkInfoService linkInfoService; |
||||
|
||||
@Test |
||||
void testGetLinkInfo() { |
||||
LinkInfo linkInfo = new LinkInfo( |
||||
1L, |
||||
"nameTable", |
||||
"nameSchema", |
||||
1 |
||||
); |
||||
Long id = 1L; |
||||
when(linkInfoRepository.findById(id)).thenReturn(Optional.of(linkInfo)); |
||||
Optional<LinkInfo> result = linkInfoService.getLinkInfo(id); |
||||
assertTrue(result.isPresent()); |
||||
assertEquals(linkInfo, result.get()); |
||||
} |
||||
|
||||
@Test |
||||
void testGetAllLinksInfos() { |
||||
List<LinkInfo> res = new ArrayList<>(); |
||||
res.add(new LinkInfo()); |
||||
when(linkInfoRepository.findAll()).thenReturn(res); |
||||
Iterable<LinkInfo> expected = linkInfoService.getAllLinksInfos(); |
||||
|
||||
assertEquals(expected, res); |
||||
} |
||||
|
||||
@Test |
||||
void testDeleteLinkInfo() { |
||||
Long id = 1L; |
||||
|
||||
linkInfoService.deleteLinkInfo(id); |
||||
|
||||
verify(linkInfoRepository, times(1)).deleteById(id); |
||||
} |
||||
|
||||
@Test |
||||
void testDeleteAllLinks() { |
||||
linkInfoService.deleteAllLinks(); |
||||
|
||||
verify(linkInfoRepository, times(1)).deleteAll(); |
||||
} |
||||
|
||||
@Test |
||||
void testAddOrUpdateLink() { |
||||
LinkInfo linkInfo = new LinkInfo( |
||||
1L, |
||||
"nameTable", |
||||
"nameSchema", |
||||
1 |
||||
); |
||||
|
||||
// When
|
||||
when(linkInfoRepository.save(linkInfo)).thenReturn(linkInfo); |
||||
LinkInfo savedLinkInfo = linkInfoService.addOrUpdateLink(linkInfo); |
||||
|
||||
// Then
|
||||
verify(linkInfoRepository, times(1)).save(linkInfo); |
||||
assertThat(savedLinkInfo.getNameTable()).isEqualTo("nameTable"); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testTruncateMyLink() { |
||||
linkInfoService.truncateMyLink(); |
||||
|
||||
verify(linkInfoRepository, times(1)).truncateMyLink(); |
||||
} |
||||
} |
@ -0,0 +1,82 @@ |
||||
package com.apside.assistDbBackend.service; |
||||
|
||||
import com.apside.assistDbBackend.model.InfoColumn; |
||||
import com.apside.assistDbBackend.model.InfoTable; |
||||
import com.apside.assistDbBackend.model.LinkInfo; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.MockitoAnnotations; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
import java.io.IOException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.nio.file.Paths; |
||||
|
||||
import static org.mockito.Mockito.*; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class ResetDataServiceTest { |
||||
|
||||
@Mock |
||||
private InfoTableService infoTableService; |
||||
|
||||
@Mock |
||||
private InfoColumnService infoColumnService; |
||||
|
||||
@Mock |
||||
private LinkInfoService linkInfoService; |
||||
private ResetDataService resetDataService; |
||||
|
||||
@BeforeEach |
||||
void setUp() throws IOException { |
||||
MockitoAnnotations.openMocks(this); |
||||
resetDataService = new ResetDataService(); |
||||
resetDataService.setInfoTableService(infoTableService); |
||||
resetDataService.setInfoColumnService(infoColumnService); |
||||
resetDataService.setLinkInfoService(linkInfoService); |
||||
Path tempPath = Paths.get("src/test/resources/testDb.json"); |
||||
resetDataService.setPath(tempPath); |
||||
resetDataService.setResult(new String(Files.readAllBytes(tempPath))); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void deleteEverything_shouldTruncateTables() { |
||||
// Arrange
|
||||
|
||||
// Act
|
||||
resetDataService.deleteEverything(); |
||||
|
||||
// Assert
|
||||
verify(infoColumnService, times(1)).truncateMyColumn(); |
||||
verify(infoTableService, times(1)).truncateMyTable(); |
||||
verify(linkInfoService, times(1)).truncateMyLink(); |
||||
} |
||||
|
||||
@Test |
||||
void insertEverything_shouldInsertData() throws IOException { |
||||
// Arrange
|
||||
// Act
|
||||
resetDataService.insertEverything(); |
||||
|
||||
// Assert
|
||||
verify(infoTableService, times(1)).addOrUpdateTable(any(InfoTable.class)); |
||||
verify(infoColumnService, times(1)).addOrUpdateColumn(any(InfoColumn.class)); |
||||
} |
||||
|
||||
@Test |
||||
void checkAndInsertLinks_shouldInsertLinks() throws IOException { |
||||
// Arrange
|
||||
when(infoColumnService.getSpecCol(anyString(), anyString(), anyInt(), anyString())) |
||||
.thenReturn(new InfoColumn(1L, "col1", "type1", 10, "coltext1")); // Mocking infoColumnService.getSpecCol()
|
||||
|
||||
// Act
|
||||
resetDataService.checkAndInsertLinks(); |
||||
|
||||
// Assert
|
||||
verify(linkInfoService, times(1)).addOrUpdateLink(any(LinkInfo.class)); |
||||
} |
||||
} |
@ -0,0 +1,251 @@ |
||||
package com.apside.assistDbBackend.service; |
||||
|
||||
import com.apside.assistDbBackend.model.LinkScriptTag; |
||||
import com.apside.assistDbBackend.model.Script; |
||||
import org.eclipse.jgit.api.errors.GitAPIException; |
||||
import org.json.JSONArray; |
||||
import org.json.JSONObject; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.MockitoAnnotations; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.net.URISyntaxException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.nio.file.Paths; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class ScriptsServiceTest { |
||||
@Mock |
||||
private GitService gitService; |
||||
|
||||
@InjectMocks |
||||
private ScriptsService scriptsService; |
||||
|
||||
private Path tempDirectoryPathJson; |
||||
private String jsonContent; |
||||
private JSONObject dataGlobal; |
||||
private JSONArray data; |
||||
private String tempDirectoryPath; |
||||
private File scriptDirectory; |
||||
|
||||
@Mock |
||||
private JSONObject jsonObject; |
||||
|
||||
@BeforeEach |
||||
void setUp() throws IOException { |
||||
MockitoAnnotations.openMocks(this); |
||||
scriptsService = new ScriptsService(); |
||||
tempDirectoryPathJson = Paths.get("src/test/resources/scripts.json"); |
||||
scriptsService.setPathOfLink(tempDirectoryPathJson); |
||||
jsonContent = "{\"data\":[{\"filename\":\"test.txt\", \"description\":\"Description test\", \"id\":1, \"tags\":[{\"tagname\":\"TXT\"}]}, {\"filename\":\"test2.txt\", \"description\":\"Description test2\", \"id\":2, \"tags\":[{\"tagname\":\"JOIN\"}]}]}"; |
||||
Files.write(tempDirectoryPathJson, jsonContent.getBytes()); |
||||
dataGlobal = new JSONObject(jsonContent); |
||||
data = dataGlobal.getJSONArray("data"); |
||||
scriptsService.setDataLinkScriptsTags(data); |
||||
|
||||
tempDirectoryPath = new File("src/test/resources/scriptsTest").toString(); |
||||
scriptDirectory = new File(tempDirectoryPath); |
||||
scriptsService.setTempDirectoryPath(tempDirectoryPath); |
||||
scriptsService.setScriptDirectory(scriptDirectory); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void retrieveScripts() throws GitAPIException, IOException { |
||||
// mock git service
|
||||
doNothing().when(gitService).pullFromGit(); |
||||
scriptsService.setGitService(gitService); |
||||
|
||||
// create temp directory for test
|
||||
String scriptContent1 = "Script 1 content"; |
||||
String scriptContent2 = "Script 2 content"; |
||||
Path filePath1 = Path.of(tempDirectoryPath + "/script1.sql"); |
||||
Path filePath2 = Path.of(tempDirectoryPath + "/script2.sql"); |
||||
Files.createFile(filePath1); |
||||
Files.createFile(filePath2); |
||||
Files.writeString(filePath1, scriptContent1); |
||||
Files.writeString(filePath2, scriptContent2); |
||||
|
||||
// Act
|
||||
List<Script> result = scriptsService.retrieveScripts(); |
||||
|
||||
verify(gitService, times(1)).pullFromGit(); |
||||
assertEquals(2, result.size()); |
||||
assertEquals("sql", result.get(0).getExtension()); |
||||
assertEquals("script1.sql", result.get(0).getName()); |
||||
assertEquals(scriptContent1, result.get(0).getData()); |
||||
assertEquals("sql", result.get(1).getExtension()); |
||||
assertEquals("script2.sql", result.get(1).getName()); |
||||
assertEquals(scriptContent2, result.get(1).getData()); |
||||
|
||||
// Clean up
|
||||
Files.deleteIfExists(filePath1); |
||||
Files.deleteIfExists(filePath2); |
||||
} |
||||
|
||||
@Test |
||||
void getAllScriptTag() throws IOException { |
||||
// Arrange
|
||||
List<LinkScriptTag> expectedTags = new ArrayList<>(); |
||||
List<String> tagList1 = Arrays.asList("TXT"); |
||||
List<String> tagList2 = Arrays.asList("JOIN"); |
||||
LinkScriptTag tag1 = new LinkScriptTag(1, "test.txt", "Description test", tagList1); |
||||
LinkScriptTag tag2 = new LinkScriptTag(2, "test2.txt", "Description test2", tagList2); |
||||
expectedTags.add(tag1); |
||||
expectedTags.add(tag2); |
||||
|
||||
// Act
|
||||
List<LinkScriptTag> result = scriptsService.getAllScriptTag(); |
||||
|
||||
// Assert
|
||||
assertEquals(expectedTags, result); |
||||
} |
||||
|
||||
@Test |
||||
void deleteOneScript() throws IOException, GitAPIException, URISyntaxException { |
||||
// Arrange
|
||||
String scriptName = "testDel.txt"; |
||||
|
||||
File scriptFile = new File(scriptDirectory, scriptName); |
||||
if (!scriptDirectory.exists()) { |
||||
scriptDirectory.mkdirs(); |
||||
} |
||||
if (!scriptFile.exists()) { |
||||
scriptFile.createNewFile(); |
||||
} |
||||
doNothing().when(gitService).pushToGit(); |
||||
doNothing().when(gitService).pullFromGit(); |
||||
scriptsService.setGitService(gitService); |
||||
|
||||
// Act
|
||||
scriptsService.deleteOneScript(scriptName); |
||||
|
||||
// Assert
|
||||
assertFalse(scriptFile.exists()); |
||||
verify(gitService, times(1)).pushToGit(); |
||||
verify(gitService, times(1)).pullFromGit(); |
||||
} |
||||
|
||||
@Test |
||||
void simpleDeleteScript() throws IOException { |
||||
// Arrange
|
||||
String scriptName = "testDel.txt"; |
||||
File scriptFile = new File(scriptDirectory, scriptName); |
||||
if (!scriptDirectory.exists()) { |
||||
scriptDirectory.mkdirs(); |
||||
} |
||||
if (!scriptFile.exists()) { |
||||
scriptFile.createNewFile(); |
||||
} |
||||
|
||||
// Act
|
||||
scriptsService.simpleDeleteScript(scriptName); |
||||
|
||||
// Assert
|
||||
assertFalse(scriptFile.exists()); |
||||
} |
||||
|
||||
@Test |
||||
void addOneScript() throws IOException { |
||||
// Arrange
|
||||
String content = "Script content"; |
||||
String name = "new_script.sql"; |
||||
File scriptFile = new File(scriptDirectory, name); |
||||
if (!scriptDirectory.exists()) { |
||||
scriptDirectory.mkdirs(); |
||||
} |
||||
if (scriptFile.exists()) { |
||||
scriptFile.delete(); |
||||
} |
||||
|
||||
// Act
|
||||
scriptsService.addOneScript(content, name); |
||||
|
||||
// Assert
|
||||
assertTrue(scriptFile.exists()); |
||||
String fileContent = Files.readString(scriptFile.toPath()); |
||||
assertEquals(content, fileContent); |
||||
|
||||
// Clean up
|
||||
scriptFile.delete(); |
||||
} |
||||
|
||||
@Test |
||||
void addOneLink() throws GitAPIException, IOException, URISyntaxException { |
||||
String name = "new_script.sql"; |
||||
String description = "New script"; |
||||
List<String> tagList = Arrays.asList("TAG1", "TAG2"); |
||||
int linkId = 3; |
||||
doNothing().when(gitService).pullFromGit(); |
||||
doNothing().when(gitService).pushToGit(); |
||||
scriptsService.setGitService(gitService); |
||||
|
||||
scriptsService.addOneLink(name, description, tagList, linkId); |
||||
|
||||
// Assert
|
||||
JSONArray updatedDataLinkScriptsTags = scriptsService.getDataLinkScriptsTags(); |
||||
assertEquals(3, updatedDataLinkScriptsTags.length()); |
||||
JSONObject newLink = updatedDataLinkScriptsTags.getJSONObject(2); |
||||
|
||||
assertEquals(linkId, newLink.getInt("id")); |
||||
assertEquals(name, newLink.getString("filename")); |
||||
assertEquals(description, newLink.getString("description")); |
||||
JSONArray newTags = newLink.getJSONArray("tags"); |
||||
assertEquals(2, newTags.length()); |
||||
JSONObject tagObj1 = newTags.getJSONObject(0); |
||||
assertEquals("TAG1", tagObj1.getString("tagname")); |
||||
JSONObject tagObj2 = newTags.getJSONObject(1); |
||||
assertEquals("TAG2", tagObj2.getString("tagname")); |
||||
verify(gitService, times(1)).pullFromGit(); |
||||
verify(gitService, times(1)).pushToGit(); |
||||
} |
||||
|
||||
@Test |
||||
void updateOneLink() throws GitAPIException, IOException, URISyntaxException { |
||||
// Arrange
|
||||
String name = "updated_script.sql"; |
||||
String description = "Updated script"; |
||||
List<String> tagList = Arrays.asList("TAG1", "TAG3"); |
||||
int linkId = 2; |
||||
|
||||
//scriptsService.setDataLinkScriptsTags(dataLinkScriptsTags);
|
||||
doNothing().when(gitService).pullFromGit(); |
||||
doNothing().when(gitService).pushToGit(); |
||||
scriptsService.setGitService(gitService); |
||||
// Act
|
||||
scriptsService.updateOneLink(name, description, tagList, linkId); |
||||
|
||||
// Assert
|
||||
JSONObject dataGlob = scriptsService.getDataGlobalScripts(); |
||||
JSONArray updatedDataLinkScriptsTags = dataGlob.getJSONArray("data"); |
||||
assertEquals(2, updatedDataLinkScriptsTags.length()); |
||||
JSONObject updatedLink = updatedDataLinkScriptsTags.getJSONObject(1); |
||||
System.out.println(updatedDataLinkScriptsTags); |
||||
|
||||
assertEquals(linkId, updatedLink.getInt("id")); |
||||
assertEquals(name, updatedLink.getString("filename")); |
||||
assertEquals(description, updatedLink.getString("description")); |
||||
JSONArray updatedTags = updatedLink.getJSONArray("tags"); |
||||
assertEquals(2, updatedTags.length()); |
||||
JSONObject tagObj1 = updatedTags.getJSONObject(0); |
||||
assertEquals("TAG1", tagObj1.getString("tagname")); |
||||
JSONObject tagObj2 = updatedTags.getJSONObject(1); |
||||
assertEquals("TAG3", tagObj2.getString("tagname")); |
||||
verify(gitService, times(1)).pullFromGit(); |
||||
verify(gitService, times(1)).pushToGit(); |
||||
} |
||||
} |
@ -0,0 +1,156 @@ |
||||
package com.apside.assistDbBackend.service; |
||||
|
||||
import com.apside.assistDbBackend.model.Tag; |
||||
import org.eclipse.jgit.api.errors.GitAPIException; |
||||
import org.json.JSONArray; |
||||
import org.json.JSONObject; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Order; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.MockitoAnnotations; |
||||
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.URISyntaxException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.nio.file.Paths; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
@ExtendWith(MockitoExtension.class) |
||||
class TagsServiceTest { |
||||
|
||||
@Mock |
||||
private GitService gitService; |
||||
|
||||
@InjectMocks |
||||
private TagsService tagsService; |
||||
|
||||
private Path tempDirectoryPath; |
||||
private String jsonContent; |
||||
private JSONObject dataGlobal; |
||||
private JSONArray data; |
||||
|
||||
@Mock |
||||
private JSONObject jsonObject; |
||||
|
||||
@BeforeEach |
||||
void setUp() throws IOException { |
||||
MockitoAnnotations.openMocks(this); |
||||
tagsService = new TagsService(); |
||||
tempDirectoryPath = Paths.get("src/test/resources/tagtest.json"); |
||||
tagsService.setPath(tempDirectoryPath); |
||||
jsonContent = "{\"data\":[{\"tagId\":1, \"description\":\"Description 1\", \"tag\":\"Tag1\"},{\"tagId\":2, \"description\":\"Description 2\", \"tag\":\"Tag2\",}]}"; |
||||
Files.write(tempDirectoryPath, jsonContent.getBytes()); |
||||
dataGlobal = new JSONObject(jsonContent); |
||||
data = dataGlobal.getJSONArray("data"); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
@Order(1) |
||||
void getAllTags_shouldReturnListOfTags() throws IOException, GitAPIException { |
||||
// Arrange
|
||||
doNothing().when(gitService).pullFromGit(); |
||||
tagsService.setGitService(gitService); |
||||
|
||||
// Act
|
||||
List<Tag> result = tagsService.getAllTags(); |
||||
|
||||
// Assert
|
||||
verify(gitService, times(1)).pullFromGit(); |
||||
assertEquals(2, result.size()); |
||||
assertEquals(1, result.get(0).getTagId()); |
||||
assertEquals("Tag1", result.get(0).getNameTag()); |
||||
assertEquals("Description 1", result.get(0).getDescriptionTag()); |
||||
assertEquals(2, result.get(1).getTagId()); |
||||
assertEquals("Tag2", result.get(1).getNameTag()); |
||||
assertEquals("Description 2", result.get(1).getDescriptionTag()); |
||||
} |
||||
|
||||
@Test |
||||
@Order(2) |
||||
void deleteTag_shouldDeleteTag() throws IOException, GitAPIException, URISyntaxException { |
||||
// Arrange
|
||||
doNothing().when(gitService).pullFromGit(); |
||||
doNothing().when(gitService).pushToGit(); |
||||
tagsService.setGitService(gitService); |
||||
|
||||
// Act
|
||||
tagsService.deleteTag("Tag1"); |
||||
|
||||
// Assert
|
||||
verify(gitService, times(1)).pullFromGit(); |
||||
verify(gitService, times(1)).pushToGit(); |
||||
|
||||
String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); |
||||
JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); |
||||
JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); |
||||
|
||||
assertEquals(1, updatedData.length()); |
||||
JSONObject tag = updatedData.getJSONObject(0); |
||||
assertEquals(2, tag.getInt("tagId")); |
||||
assertEquals("Tag2", tag.getString("tag")); |
||||
assertEquals("Description 2", tag.getString("description")); |
||||
} |
||||
|
||||
@Test |
||||
void addTag_shouldAddTag() throws IOException, GitAPIException, URISyntaxException { |
||||
// Arrange
|
||||
doNothing().when(gitService).pullFromGit(); |
||||
doNothing().when(gitService).pushToGit(); |
||||
tagsService.setGitService(gitService); |
||||
|
||||
Tag newTag = new Tag(3, "Tag3", "Description 3"); |
||||
|
||||
// Act
|
||||
tagsService.addTag(newTag); |
||||
|
||||
// Assert
|
||||
verify(gitService, times(2)).pullFromGit(); |
||||
verify(gitService, times(1)).pushToGit(); |
||||
|
||||
String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); |
||||
JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); |
||||
JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); |
||||
|
||||
assertEquals(3, updatedData.length()); |
||||
JSONObject tag = updatedData.getJSONObject(2); |
||||
assertEquals(3, tag.getInt("tagId")); |
||||
assertEquals("Tag3", tag.getString("tag")); |
||||
assertEquals("Description 3", tag.getString("description")); |
||||
} |
||||
|
||||
@Test |
||||
void updateTag_shouldUpdateTag() throws IOException, GitAPIException, URISyntaxException { |
||||
// Arrange
|
||||
doNothing().when(gitService).pullFromGit(); |
||||
doNothing().when(gitService).pushToGit(); |
||||
tagsService.setGitService(gitService); |
||||
|
||||
Tag modifiedTag = new Tag(2, "ModifiedTag", "ModifiedDescription"); |
||||
|
||||
// Act
|
||||
tagsService.updateTag("Tag2", modifiedTag); |
||||
|
||||
// Assert
|
||||
verify(gitService, times(2)).pullFromGit(); |
||||
verify(gitService, times(1)).pushToGit(); |
||||
|
||||
String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); |
||||
JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); |
||||
JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); |
||||
|
||||
assertEquals(2, updatedData.length()); |
||||
JSONObject tag = updatedData.getJSONObject(1); |
||||
assertEquals(2, tag.getInt("tagId")); |
||||
assertEquals("ModifiedTag", tag.getString("tag")); |
||||
assertEquals("ModifiedDescription", tag.getString("description")); |
||||
} |
||||
} |
@ -0,0 +1,9 @@ |
||||
spring.datasource.url=jdbc:h2://mem:DB;DB_CLOSE_DELAY=-1 |
||||
spring.datasource.username=sa |
||||
spring.datasource.password=sa |
||||
spring.datasource.driver-class-name=org.h2.Driver |
||||
spring.jpa.hibernate.ddl-auto=create-drop |
||||
spring.jpa.show-sql=true |
||||
|
||||
USERNAME_GIT = fhibert@apside.fr |
||||
ACCESS_TOKEN_GIT = e54612b6d1d73eef4f0a49c88b0e35ccf02d45eb |
@ -0,0 +1,27 @@ |
||||
DROP TABLE IF EXISTS `informations_column`; |
||||
CREATE TABLE `informations_column` ( |
||||
`id` int NOT NULL AUTO_INCREMENT, |
||||
`name_column` varchar(100) DEFAULT NULL, |
||||
`data_type` varchar(100) DEFAULT NULL, |
||||
`length_column` int DEFAULT NULL, |
||||
`column_text` varchar(255) DEFAULT NULL, |
||||
PRIMARY KEY (`id`) |
||||
) |
||||
|
||||
DROP TABLE IF EXISTS `informations_table`; |
||||
CREATE TABLE `informations_table` ( |
||||
`id` int NOT NULL AUTO_INCREMENT, |
||||
`name_table` varchar(100) DEFAULT NULL, |
||||
`name_schema` varchar(100) DEFAULT NULL, |
||||
`table_text` varchar(255) DEFAULT NULL, |
||||
PRIMARY KEY (`id`) |
||||
) |
||||
|
||||
DROP TABLE IF EXISTS `link_informations`; |
||||
CREATE TABLE `link_informations` ( |
||||
`id` int NOT NULL AUTO_INCREMENT, |
||||
`name_table` varchar(100) DEFAULT NULL, |
||||
`name_schema` varchar(100) DEFAULT NULL, |
||||
`column_id` int DEFAULT NULL, |
||||
PRIMARY KEY (`id`) |
||||
) |
@ -0,0 +1 @@ |
||||
{"data":[{"filename":"test.txt","description":"Description test","id":1,"tags":[{"tagname":"TXT"}]},{"filename":"updated_script.sql","description":"Updated script","id":2,"tags":[{"tagname":"TAG1"},{"tagname":"TAG3"}]}]} |
@ -0,0 +1 @@ |
||||
{"data":[{"tagId":2,"description":"Description 2","tag":"Tag2"}]} |
@ -0,0 +1,18 @@ |
||||
[ |
||||
{ |
||||
"schema": "schema1", |
||||
"data": [ |
||||
{ |
||||
"columns": [ |
||||
{ |
||||
"name": "col1", |
||||
"data_type": "type1", |
||||
"length": 10, |
||||
"column_text": "coltext1" |
||||
}], |
||||
"table_text": "testtable1", |
||||
"table": "table1" |
||||
} |
||||
] |
||||
} |
||||
] |
Loading…
Reference in new issue