Add unit test and opti with sonar

version_2
floxx2112 1 year ago
parent c0100c5d68
commit 951904394b
  1. 28
      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> <artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.mysql</groupId> <groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId> <artifactId>mysql-connector-j</artifactId>
@ -66,6 +65,33 @@
<artifactId>org.eclipse.jgit</artifactId> <artifactId>org.eclipse.jgit</artifactId>
<version>6.4.0.202211300538-r</version> <version>6.4.0.202211300538-r</version>
</dependency> </dependency>
<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> </dependencies>
<build> <build>

@ -19,6 +19,8 @@ public class GitService {
private final String tempDirectoryPath; private final String tempDirectoryPath;
private UsernamePasswordCredentialsProvider userPass;
public GitService(){ public GitService(){
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles"; tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles";
} }
@ -40,7 +42,9 @@ public class GitService {
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setMessage("commited").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.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@Data @Data
@ -24,11 +25,19 @@ public class ResetDataService {
@Autowired @Autowired
private LinkInfoService linkInfoService; private LinkInfoService linkInfoService;
private static final String tableStr = "table"; private String result;
private static final String nameStr = "name"; private Path path;
private static final String dataTypeStr = "data_type";
private static final String lengthStr = "length"; private static final String TABLE_STRING = "table";
private static final String columnTextStr = "column_text"; 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(){ public void deleteEverything(){
infoColumnService.truncateMyColumn(); infoColumnService.truncateMyColumn();
@ -37,7 +46,6 @@ public class ResetDataService {
} }
public void insertEverything() throws IOException { public void insertEverything() throws IOException {
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/assistDbData.json")));
JSONArray jo = new JSONArray(result); JSONArray jo = new JSONArray(result);
for (int i=0; i<jo.length(); i++){ for (int i=0; i<jo.length(); i++){
JSONObject schema = jo.getJSONObject(i); JSONObject schema = jo.getJSONObject(i);
@ -53,8 +61,8 @@ public class ResetDataService {
} else { } else {
tableText = ""; tableText = "";
} }
if(apra.has(tableStr)){ if(apra.has(TABLE_STRING)){
tableName = apra.getString(tableStr); tableName = apra.getString(TABLE_STRING);
} else { } else {
tableName = ""; tableName = "";
} }
@ -72,23 +80,23 @@ public class ResetDataService {
String dataType; String dataType;
String columnText; String columnText;
int lengthCol; int lengthCol;
if(colData.has(nameStr)){ if(colData.has(NAME_STRING)){
nameCol = colData.getString(nameStr); nameCol = colData.getString(NAME_STRING);
} else { } else {
nameCol = ""; nameCol = "";
} }
if (colData.has(dataTypeStr)){ if (colData.has(DATA_TYPE_STRING)){
dataType = colData.getString(dataTypeStr); dataType = colData.getString(DATA_TYPE_STRING);
} else { } else {
dataType = ""; dataType = "";
} }
if (colData.has(lengthStr)){ if (colData.has(LENGTH_STRING)){
lengthCol = colData.getInt(lengthStr); lengthCol = colData.getInt(LENGTH_STRING);
} else { } else {
lengthCol = 0; lengthCol = 0;
} }
if(colData.has(columnTextStr)) { if(colData.has(COLUMN_TEXT_STRING)) {
columnText = colData.getString(columnTextStr); columnText = colData.getString(COLUMN_TEXT_STRING);
} else { } else {
columnText = ""; columnText = "";
} }
@ -110,9 +118,7 @@ public class ResetDataService {
} }
public void checkAndInsertLinks() throws IOException { public void checkAndInsertLinks() throws IOException {
String result = new String(Files.readAllBytes(Paths.get("src/main/resources/assistDbData.json")));
JSONArray jo = new JSONArray(result); JSONArray jo = new JSONArray(result);
for (int i=0; i<jo.length(); i++){ for (int i=0; i<jo.length(); i++){
JSONObject schema = jo.getJSONObject(i); JSONObject schema = jo.getJSONObject(i);
String schemaName = schema.getString("schema"); String schemaName = schema.getString("schema");
@ -121,8 +127,8 @@ public class ResetDataService {
for (int ia = 0; ia<data.length(); ia++){ for (int ia = 0; ia<data.length(); ia++){
JSONObject apra = data.getJSONObject(ia); JSONObject apra = data.getJSONObject(ia);
String tableName; String tableName;
if(apra.has(tableStr)){ if(apra.has(TABLE_STRING)){
tableName = apra.getString(tableStr); tableName = apra.getString(TABLE_STRING);
} else { } else {
tableName = ""; tableName = "";
} }
@ -134,23 +140,23 @@ public class ResetDataService {
String dataType; String dataType;
String columnText; String columnText;
int lengthCol; int lengthCol;
if(colData.has(nameStr)){ if(colData.has(NAME_STRING)){
nameCol = colData.getString(nameStr); nameCol = colData.getString(NAME_STRING);
} else { } else {
nameCol = ""; nameCol = "";
} }
if (colData.has(dataTypeStr)){ if (colData.has(DATA_TYPE_STRING)){
dataType = colData.getString(dataTypeStr); dataType = colData.getString(DATA_TYPE_STRING);
} else { } else {
dataType = ""; dataType = "";
} }
if (colData.has(lengthStr)){ if (colData.has(LENGTH_STRING)){
lengthCol = colData.getInt(lengthStr); lengthCol = colData.getInt(LENGTH_STRING);
} else { } else {
lengthCol = 0; lengthCol = 0;
} }
if(colData.has(columnTextStr)) { if(colData.has(COLUMN_TEXT_STRING)) {
columnText = colData.getString(columnTextStr); columnText = colData.getString(COLUMN_TEXT_STRING);
} else { } else {
columnText = ""; columnText = "";
} }

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

@ -22,15 +22,15 @@ import java.util.Objects;
public class TagsService { public class TagsService {
@Autowired @Autowired
private GitService gitService; private GitService gitService;
private final String tempDirectoryPath; private String tempDirectoryPath;
private final Path path; private Path path;
private String jsonContent; private String jsonContent;
private JSONObject dataGlobal; private JSONObject dataGlobal;
private JSONArray data; private JSONArray data;
private static final String tagIdStr = "tagId"; private static final String TAG_ID_STRING = "tagId";
private static final String dataStr = "data"; private static final String DATA_STRING = "data";
private static final String tagStr = "tag"; private static final String TAG_STRING = "tag";
private static final String descriptionStr = "description"; private static final String DESCRIPTION_STRING = "description";
public TagsService() throws IOException { public TagsService() throws IOException {
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/tags.json"; tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/tags.json";
path = Paths.get(tempDirectoryPath); path = Paths.get(tempDirectoryPath);
@ -39,7 +39,7 @@ public class TagsService {
private void initialize() throws IOException { private void initialize() throws IOException {
jsonContent = new String(Files.readAllBytes(path)); jsonContent = new String(Files.readAllBytes(path));
dataGlobal = new JSONObject(jsonContent); dataGlobal = new JSONObject(jsonContent);
data = dataGlobal.getJSONArray(dataStr); data = dataGlobal.getJSONArray(DATA_STRING);
} }
public List<Tag> getAllTags() throws IOException, GitAPIException { public List<Tag> getAllTags() throws IOException, GitAPIException {
@ -48,9 +48,9 @@ public class TagsService {
List<Tag> listOfTag = new ArrayList<>(); List<Tag> listOfTag = new ArrayList<>();
for (int i=0; i<data.length(); i++){ for (int i=0; i<data.length(); i++){
JSONObject tag = data.getJSONObject(i); JSONObject tag = data.getJSONObject(i);
int tagId = tag.getInt(tagIdStr); int tagId = tag.getInt(TAG_ID_STRING);
String tagName = tag.getString(tagStr); String tagName = tag.getString(TAG_STRING);
String tagDescription = tag.getString(descriptionStr); String tagDescription = tag.getString(DESCRIPTION_STRING);
Tag tempTag = new Tag(tagId, tagName, tagDescription); Tag tempTag = new Tag(tagId, tagName, tagDescription);
listOfTag.add(tempTag); listOfTag.add(tempTag);
} }
@ -62,12 +62,12 @@ public class TagsService {
JSONArray newArr = new JSONArray(); JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) { for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i); JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString(tagStr); String tagName = tag.getString(TAG_STRING);
if(!Objects.equals(tagName, deleteNameTag)){ if(!Objects.equals(tagName, deleteNameTag)){
newArr.put(tag); newArr.put(tag);
} }
} }
JSONObject newObj = dataGlobal.put(dataStr, newArr); JSONObject newObj = dataGlobal.put(DATA_STRING, newArr);
Files.write(path, newObj.toString().getBytes()); Files.write(path, newObj.toString().getBytes());
gitService.pushToGit(); gitService.pushToGit();
gitService.pullFromGit(); gitService.pullFromGit();
@ -77,11 +77,11 @@ public class TagsService {
initialize(); initialize();
gitService.pullFromGit(); gitService.pullFromGit();
JSONObject newTag = new JSONObject(); JSONObject newTag = new JSONObject();
newTag.put(tagIdStr, tag.getTagId()); newTag.put(TAG_ID_STRING, tag.getTagId());
newTag.put(tagStr, tag.getNameTag()); newTag.put(TAG_STRING, tag.getNameTag());
newTag.put(descriptionStr, tag.getDescriptionTag()); newTag.put(DESCRIPTION_STRING, tag.getDescriptionTag());
JSONArray newArr = data.put(newTag); JSONArray newArr = data.put(newTag);
JSONObject newObj = dataGlobal.put(dataStr, newArr); JSONObject newObj = dataGlobal.put(DATA_STRING, newArr);
Files.write(path, newObj.toString().getBytes()); Files.write(path, newObj.toString().getBytes());
gitService.pushToGit(); gitService.pushToGit();
gitService.pullFromGit(); gitService.pullFromGit();
@ -92,18 +92,18 @@ public class TagsService {
JSONArray newArr = new JSONArray(); JSONArray newArr = new JSONArray();
for (int i=0; i<data.length(); i++) { for (int i=0; i<data.length(); i++) {
JSONObject tag = data.getJSONObject(i); JSONObject tag = data.getJSONObject(i);
String tagName = tag.getString(tagStr); String tagName = tag.getString(TAG_STRING);
if(!Objects.equals(tagName, prevTag)){ if(!Objects.equals(tagName, prevTag)){
newArr.put(tag); newArr.put(tag);
} else { } else {
JSONObject newTag = new JSONObject(); JSONObject newTag = new JSONObject();
newTag.put(tagIdStr, modTag.getTagId()); newTag.put(TAG_ID_STRING, modTag.getTagId());
newTag.put(tagStr, modTag.getNameTag()); newTag.put(TAG_STRING, modTag.getNameTag());
newTag.put(descriptionStr, modTag.getDescriptionTag()); newTag.put(DESCRIPTION_STRING, modTag.getDescriptionTag());
newArr.put(newTag); newArr.put(newTag);
} }
} }
JSONObject newObj = dataGlobal.put(dataStr, newArr); JSONObject newObj = dataGlobal.put(DATA_STRING, newArr);
Files.write(path, newObj.toString().getBytes()); Files.write(path, newObj.toString().getBytes());
gitService.pushToGit(); gitService.pushToGit();
gitService.pullFromGit(); 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 static org.hamcrest.CoreMatchers.is;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; 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; import org.springframework.test.web.servlet.MockMvc;
@Configuration
@PropertySource({"classpath:application.properties"})
@ComponentScan(basePackages = {
"com.apside.assistDbBackend.service",
"com.apside.assistDbBackend.model",
})
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc public class AssistDBBackendTest {
class AssistDBBackendTest {
@Test
@Autowired void contextLoads(){
private MockMvc mockMvc;
}
/*@Test
public void testGetEmployees() throws Exception {
mockMvc.perform(get("/api/employee/all"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].firstName", is("Laurent")));
}*/
} }

@ -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