Add unit test and opti with sonar

version_2
floxx2112 2 years ago
parent c0100c5d68
commit 951904394b
  1. 30
      pom.xml
  2. 6
      src/main/java/com/apside/assistDbBackend/service/GitService.java
  3. 62
      src/main/java/com/apside/assistDbBackend/service/ResetDataService.java
  4. 75
      src/main/java/com/apside/assistDbBackend/service/ScriptsService.java
  5. 42
      src/main/java/com/apside/assistDbBackend/service/TagsService.java
  6. 2222
      src/main/resources/assistDbData.json
  7. 2221
      src/main/resources/assistDbDataTest.json
  8. 30
      src/test/java/com/apside/assistDbBackend/AssistDBBackendTest.java
  9. 135
      src/test/java/com/apside/assistDbBackend/repository/InfoColumnRepositoryTest.java
  10. 97
      src/test/java/com/apside/assistDbBackend/repository/InfoTableRepositoryTest.java
  11. 37
      src/test/java/com/apside/assistDbBackend/repository/LinkInfoRepositoryTest.java
  12. 127
      src/test/java/com/apside/assistDbBackend/service/GitServiceTest.java
  13. 228
      src/test/java/com/apside/assistDbBackend/service/InfoColumnServiceTest.java
  14. 157
      src/test/java/com/apside/assistDbBackend/service/InfoTableServiceTest.java
  15. 96
      src/test/java/com/apside/assistDbBackend/service/LinkInfoServiceTest.java
  16. 82
      src/test/java/com/apside/assistDbBackend/service/ResetDataServiceTest.java
  17. 251
      src/test/java/com/apside/assistDbBackend/service/ScriptsServiceTest.java
  18. 156
      src/test/java/com/apside/assistDbBackend/service/TagsServiceTest.java
  19. 9
      src/test/resources/application.properties
  20. 0
      src/test/resources/db/initDb/initDatas.sql
  21. 27
      src/test/resources/db/initDb/initDb.sql
  22. 1
      src/test/resources/scripts.json
  23. 1
      src/test/resources/tagtest.json
  24. 18
      src/test/resources/testDb.json

@ -30,7 +30,6 @@
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
@ -66,7 +65,34 @@
<artifactId>org.eclipse.jgit</artifactId>
<version>6.4.0.202211300538-r</version>
</dependency>
</dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.6.28</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

@ -19,6 +19,8 @@ public class GitService {
private final String tempDirectoryPath;
private UsernamePasswordCredentialsProvider userPass;
public GitService(){
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
}
@ -40,7 +42,9 @@ public class GitService {
git.add().addFilepattern(".").call();
git.commit().setMessage("commited").call();
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call();
userPass = new UsernamePasswordCredentialsProvider(userGit, accesToken);
git.push().setCredentialsProvider(userPass).call();
}
}

@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Data
@ -24,11 +25,19 @@ public class ResetDataService {
@Autowired
private LinkInfoService linkInfoService;
private static final String tableStr = "table";
private static final String nameStr = "name";
private static final String dataTypeStr = "data_type";
private static final String lengthStr = "length";
private static final String columnTextStr = "column_text";
private String result;
private Path path;
private static final String TABLE_STRING = "table";
private static final String NAME_STRING = "name";
private static final String DATA_TYPE_STRING = "data_type";
private static final String LENGTH_STRING = "length";
private static final String COLUMN_TEXT_STRING = "column_text";
public ResetDataService() throws IOException {
path = Paths.get("src/main/resources/assistDbData.json");
result = new String(Files.readAllBytes(path));
}
public void deleteEverything(){
infoColumnService.truncateMyColumn();
@ -37,7 +46,6 @@ public class ResetDataService {
}
public void insertEverything() throws IOException {
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/assistDbData.json")));
JSONArray jo = new JSONArray(result);
for (int i=0; i<jo.length(); i++){
JSONObject schema = jo.getJSONObject(i);
@ -53,8 +61,8 @@ public class ResetDataService {
} else {
tableText = "";
}
if(apra.has(tableStr)){
tableName = apra.getString(tableStr);
if(apra.has(TABLE_STRING)){
tableName = apra.getString(TABLE_STRING);
} else {
tableName = "";
}
@ -72,23 +80,23 @@ public class ResetDataService {
String dataType;
String columnText;
int lengthCol;
if(colData.has(nameStr)){
nameCol = colData.getString(nameStr);
if(colData.has(NAME_STRING)){
nameCol = colData.getString(NAME_STRING);
} else {
nameCol = "";
}
if (colData.has(dataTypeStr)){
dataType = colData.getString(dataTypeStr);
if (colData.has(DATA_TYPE_STRING)){
dataType = colData.getString(DATA_TYPE_STRING);
} else {
dataType = "";
}
if (colData.has(lengthStr)){
lengthCol = colData.getInt(lengthStr);
if (colData.has(LENGTH_STRING)){
lengthCol = colData.getInt(LENGTH_STRING);
} else {
lengthCol = 0;
}
if(colData.has(columnTextStr)) {
columnText = colData.getString(columnTextStr);
if(colData.has(COLUMN_TEXT_STRING)) {
columnText = colData.getString(COLUMN_TEXT_STRING);
} else {
columnText = "";
}
@ -110,9 +118,7 @@ public class ResetDataService {
}
public void checkAndInsertLinks() throws IOException {
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/assistDbData.json")));
JSONArray jo = new JSONArray(result);
for (int i=0; i<jo.length(); i++){
JSONObject schema = jo.getJSONObject(i);
String schemaName = schema.getString("schema");
@ -121,8 +127,8 @@ public class ResetDataService {
for (int ia = 0; ia<data.length(); ia++){
JSONObject apra = data.getJSONObject(ia);
String tableName;
if(apra.has(tableStr)){
tableName = apra.getString(tableStr);
if(apra.has(TABLE_STRING)){
tableName = apra.getString(TABLE_STRING);
} else {
tableName = "";
}
@ -134,23 +140,23 @@ public class ResetDataService {
String dataType;
String columnText;
int lengthCol;
if(colData.has(nameStr)){
nameCol = colData.getString(nameStr);
if(colData.has(NAME_STRING)){
nameCol = colData.getString(NAME_STRING);
} else {
nameCol = "";
}
if (colData.has(dataTypeStr)){
dataType = colData.getString(dataTypeStr);
if (colData.has(DATA_TYPE_STRING)){
dataType = colData.getString(DATA_TYPE_STRING);
} else {
dataType = "";
}
if (colData.has(lengthStr)){
lengthCol = colData.getInt(lengthStr);
if (colData.has(LENGTH_STRING)){
lengthCol = colData.getInt(LENGTH_STRING);
} else {
lengthCol = 0;
}
if(colData.has(columnTextStr)) {
columnText = colData.getString(columnTextStr);
if(colData.has(COLUMN_TEXT_STRING)) {
columnText = colData.getString(COLUMN_TEXT_STRING);
} else {
columnText = "";
}

@ -23,39 +23,40 @@ import java.util.*;
public class ScriptsService {
@Autowired
private GitService gitService;
private final String linkScriptTagPath;
private final Path pathOfLink;
private String linkScriptTagPath;
private Path pathOfLink;
private String allScriptsContent;
private JSONObject dataGlobalScripts;
private JSONArray dataLinkScriptsTags;
private static final String pathToScriptDir = "/AssistDB_AdditionalFiles/Scripts";
private static final String userDirString = "user.dir";
private static final String filenameString = "filename";
private static final String descriptionString = "description";
private static final String tagNameString = "tagname";
private static final String idString = "id";
private static final String tagsString = "tags";
private static final String dataString = "data";
private String tempDirectoryPath;
private File scriptDirectory;
private static final String PATH_TO_SCRIPT_DIR = "/AssistDB_AdditionalFiles/Scripts";
private static final String USER_DIR_STRING = "user.dir";
private static final String FILE_NAME_STRING = "filename";
private static final String DESCRIPTION_STRING = "description";
private static final String TAG_NAME_STRING = "tagname";
private static final String ID_STRING = "id";
private static final String TAGS_STRING = "tags";
private static final String DATA_STRING = "data";
public ScriptsService() throws IOException {
linkScriptTagPath = new File(System.getProperty(userDirString)).getParent() + "/AssistDB_AdditionalFiles/scripts.json";
linkScriptTagPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + "/AssistDB_AdditionalFiles/scripts.json";
pathOfLink = Paths.get(linkScriptTagPath);
tempDirectoryPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_TO_SCRIPT_DIR;
scriptDirectory = new File(tempDirectoryPath);
}
private void initialize() throws IOException {
allScriptsContent = new String((Files.readAllBytes(pathOfLink)));
dataGlobalScripts = new JSONObject(allScriptsContent);
dataLinkScriptsTags = dataGlobalScripts.getJSONArray(dataString);
dataLinkScriptsTags = dataGlobalScripts.getJSONArray(DATA_STRING);
}
public List<Script> retrieveScripts() throws IOException, GitAPIException {
gitService.pullFromGit();
List<Script> listOfScripts = new ArrayList<>();
//Creating a File object for directory
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
//List of all files and directories
File scriptDirectory = new File(tempDirectoryPath);
String[] contents = scriptDirectory.list();
for(int i=0; i<contents.length; i++) {
Path filePath = Path.of(tempDirectoryPath + "/" + contents[i]);
@ -73,14 +74,14 @@ public class ScriptsService {
if (dataLinkScriptsTags.length()>0){
for (int i = 0; i<dataLinkScriptsTags.length(); i++){
JSONObject link = dataLinkScriptsTags.getJSONObject(i);
int linkId = link.getInt(idString);
String scriptName = link.getString(filenameString);
String desc = link.getString(descriptionString);
int linkId = link.getInt(ID_STRING);
String scriptName = link.getString(FILE_NAME_STRING);
String desc = link.getString(DESCRIPTION_STRING);
List<String> tagList = new ArrayList<>();
JSONArray tags = link.getJSONArray(tagsString);
JSONArray tags = link.getJSONArray(TAGS_STRING);
for (int j=0; j<tags.length(); j++){
JSONObject unitTag = tags.getJSONObject(j);
String tagName = unitTag.getString(tagNameString);
String tagName = unitTag.getString(TAG_NAME_STRING);
tagList.add(tagName);
}
LinkScriptTag newLinkScriptTag = new LinkScriptTag(linkId, scriptName, desc, tagList);
@ -92,7 +93,6 @@ public class ScriptsService {
}
public void deleteOneScript(final String name){
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
try {
File scriptDirectory = new File(tempDirectoryPath + "/" + name);
scriptDirectory.delete();
@ -104,7 +104,6 @@ public class ScriptsService {
}
public void simpleDeleteScript(final String name){
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
try {
File scriptDirectory = new File(tempDirectoryPath + "/" + name);
scriptDirectory.delete();
@ -115,33 +114,29 @@ public class ScriptsService {
}
public void addOneScript(final String content, final String name) throws IOException {
String tempDirectoryPath = new File(System.getProperty(userDirString)).getParent() + pathToScriptDir;
File newFile = new File(tempDirectoryPath + "/" + name);
newFile.createNewFile();
try (FileWriter writerDataFile = new FileWriter(tempDirectoryPath + "/" + name);) {
writerDataFile.write(content);
}
}
public void addOneLink(final String name, final String description, final List<String> tagList, final int linkId) throws IOException, GitAPIException, URISyntaxException {
initialize();
gitService.pullFromGit();
JSONObject newLink = new JSONObject();
newLink.put(idString, linkId);
newLink.put(filenameString, name);
newLink.put(descriptionString, description);
newLink.put(ID_STRING, linkId);
newLink.put(FILE_NAME_STRING, name);
newLink.put(DESCRIPTION_STRING, description);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
tempTag.put(tagNameString, tagList.get(i));
tempTag.put(TAG_NAME_STRING, tagList.get(i));
tagArray.put(tempTag);
}
newLink.put(tagsString, tagArray);
newLink.put(TAGS_STRING, tagArray);
JSONArray newArr = dataLinkScriptsTags.put(newLink);
JSONObject newObj = dataGlobalScripts.put(dataString, newArr);
JSONObject newObj = dataGlobalScripts.put(DATA_STRING, newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
gitService.pushToGit();
}
@ -152,25 +147,25 @@ public class ScriptsService {
JSONArray newArr = new JSONArray();
for (int d = 0; d<dataLinkScriptsTags.length(); d++){
JSONObject actualLink = dataLinkScriptsTags.getJSONObject(d);
int actualId = actualLink.getInt(idString);
int actualId = actualLink.getInt(ID_STRING);
if (!Objects.equals(linkId, actualId)){
newArr.put(actualLink);
} else {
JSONObject newLink = new JSONObject();
newLink.put(idString, linkId);
newLink.put(filenameString, name);
newLink.put(descriptionString, description);
newLink.put(ID_STRING, linkId);
newLink.put(FILE_NAME_STRING, name);
newLink.put(DESCRIPTION_STRING, description);
JSONArray tagArray = new JSONArray();
for(int i=0; i<tagList.size(); i++){
JSONObject tempTag = new JSONObject();
tempTag.put(tagNameString, tagList.get(i));
tempTag.put(TAG_NAME_STRING, tagList.get(i));
tagArray.put(tempTag);
}
newLink.put(tagsString, tagArray);
newLink.put(TAGS_STRING, tagArray);
newArr.put(newLink);
}
}
JSONObject newObj = dataGlobalScripts.put(dataString, newArr);
JSONObject newObj = dataGlobalScripts.put(DATA_STRING, newArr);
Files.write(pathOfLink, newObj.toString().getBytes());
gitService.pushToGit();

@ -22,15 +22,15 @@ import java.util.Objects;
public class TagsService {
@Autowired
private GitService gitService;
private final String tempDirectoryPath;
private final Path path;
private String tempDirectoryPath;
private Path path;
private String jsonContent;
private JSONObject dataGlobal;
private JSONArray data;
private static final String tagIdStr = "tagId";
private static final String dataStr = "data";
private static final String tagStr = "tag";
private static final String descriptionStr = "description";
private static final String TAG_ID_STRING = "tagId";
private static final String DATA_STRING = "data";
private static final String TAG_STRING = "tag";
private static final String DESCRIPTION_STRING = "description";
public TagsService() throws IOException {
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/tags.json";
path = Paths.get(tempDirectoryPath);
@ -39,7 +39,7 @@ public class TagsService {
private void initialize() throws IOException {
jsonContent = new String(Files.readAllBytes(path));
dataGlobal = new JSONObject(jsonContent);
data = dataGlobal.getJSONArray(dataStr);
data = dataGlobal.getJSONArray(DATA_STRING);
}
public List<Tag> getAllTags() throws IOException, GitAPIException {
@ -48,9 +48,9 @@ public class TagsService {
List<Tag> listOfTag = new ArrayList<>();
for (int i=0; i<data.length(); i++){
JSONObject tag = data.getJSONObject(i);
int tagId = tag.getInt(tagIdStr);
String tagName = tag.getString(tagStr);
String tagDescription = tag.getString(descriptionStr);
int tagId = tag.getInt(TAG_ID_STRING);
String tagName = tag.getString(TAG_STRING);
String tagDescription = tag.getString(DESCRIPTION_STRING);
Tag tempTag = new Tag(tagId, tagName, tagDescription);
listOfTag.add(tempTag);
}
@ -62,12 +62,12 @@ public class TagsService {
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString(tagStr);
String tagName = tag.getString(TAG_STRING);
if(!Objects.equals(tagName, deleteNameTag)){
newArr.put(tag);
}
}
JSONObject newObj = dataGlobal.put(dataStr, newArr);
JSONObject newObj = dataGlobal.put(DATA_STRING, newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();
@ -77,11 +77,11 @@ public class TagsService {
initialize();
gitService.pullFromGit();
JSONObject newTag = new JSONObject();
newTag.put(tagIdStr, tag.getTagId());
newTag.put(tagStr, tag.getNameTag());
newTag.put(descriptionStr, tag.getDescriptionTag());
newTag.put(TAG_ID_STRING, tag.getTagId());
newTag.put(TAG_STRING, tag.getNameTag());
newTag.put(DESCRIPTION_STRING, tag.getDescriptionTag());
JSONArray newArr = data.put(newTag);
JSONObject newObj = dataGlobal.put(dataStr, newArr);
JSONObject newObj = dataGlobal.put(DATA_STRING, newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();
@ -92,18 +92,18 @@ public class TagsService {
JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString(tagStr);
String tagName = tag.getString(TAG_STRING);
if(!Objects.equals(tagName, prevTag)){
newArr.put(tag);
} else {
JSONObject newTag = new JSONObject();
newTag.put(tagIdStr, modTag.getTagId());
newTag.put(tagStr, modTag.getNameTag());
newTag.put(descriptionStr, modTag.getDescriptionTag());
newTag.put(TAG_ID_STRING, modTag.getTagId());
newTag.put(TAG_STRING, modTag.getNameTag());
newTag.put(DESCRIPTION_STRING, modTag.getDescriptionTag());
newArr.put(newTag);
}
}
JSONObject newObj = dataGlobal.put(dataStr, newArr);
JSONObject newObj = dataGlobal.put(DATA_STRING, newArr);
Files.write(path, newObj.toString().getBytes());
gitService.pushToGit();
gitService.pullFromGit();

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -5,23 +5,29 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.hamcrest.CoreMatchers.is;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.test.web.servlet.MockMvc;
@Configuration
@PropertySource({"classpath:application.properties"})
@ComponentScan(basePackages = {
"com.apside.assistDbBackend.service",
"com.apside.assistDbBackend.model",
})
@SpringBootTest
@AutoConfigureMockMvc
class AssistDBBackendTest {
@Autowired
private MockMvc mockMvc;
/*@Test
public void testGetEmployees() throws Exception {
mockMvc.perform(get("/api/employee/all"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].firstName", is("Laurent")));
}*/
public class AssistDBBackendTest {
@Test
void contextLoads(){
}
}

@ -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…
Cancel
Save