Update Test + move impl + add query to sort table in backend

version_2
floxx2112 1 year ago
parent e9347aaa9a
commit 7d36756f15
  1. 24
      src/main/java/com/apside/assist/db/backend/controller/InfoTableController.java
  2. 9
      src/main/java/com/apside/assist/db/backend/repository/InfoTableRepository.java
  3. 17
      src/main/java/com/apside/assist/db/backend/service/InfoTableService.java
  4. 3
      src/main/java/com/apside/assist/db/backend/service/implementation/GitServiceImpl.java
  5. 2
      src/main/java/com/apside/assist/db/backend/service/implementation/InfoColumnServiceImpl.java
  6. 14
      src/main/java/com/apside/assist/db/backend/service/implementation/InfoTableServiceImpl.java
  7. 2
      src/main/java/com/apside/assist/db/backend/service/implementation/LinkInfoServiceImpl.java
  8. 19
      src/main/java/com/apside/assist/db/backend/service/implementation/ResetDataServiceImpl.java
  9. 20
      src/main/java/com/apside/assist/db/backend/service/implementation/ScriptsServiceImpl.java
  10. 16
      src/main/java/com/apside/assist/db/backend/service/implementation/TagsServiceImpl.java
  11. 7
      src/main/resources/application.properties
  12. 2
      src/test/java/com/apside/assist/db/backend/AssistDBBackendTest.java
  13. 2
      src/test/java/com/apside/assist/db/backend/service/InfoColumnServiceImplTest.java
  14. 2
      src/test/java/com/apside/assist/db/backend/service/InfoTableServiceImplTest.java
  15. 2
      src/test/java/com/apside/assist/db/backend/service/LinkInfoServiceImplTest.java
  16. 40
      src/test/java/com/apside/assist/db/backend/service/ResetDataServiceImplTest.java
  17. 72
      src/test/java/com/apside/assist/db/backend/service/ScriptsServiceImplTest.java
  18. 38
      src/test/java/com/apside/assist/db/backend/service/TagsServiceImplTest.java
  19. 14
      src/test/resources/application-test.properties
  20. 9
      src/test/resources/application.properties

@ -105,4 +105,28 @@ public class InfoTableController {
} }
} }
@GetMapping("/tables/filter/{filter}")
public ResponseEntity<Iterable<InfoTable>> getTablesWithFilter(@PathVariable("filter") String filter){
log.debug("getTablesWithFilter called");
try {
Iterable<InfoTable> tables = infoTableService.getTablesWithFilter(filter);
return ResponseEntity.ok(tables);
} catch (Exception e) {
log.error("Error occurred while deleting tables", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@GetMapping("/tables/filter/{schema}/{filter}")
public ResponseEntity<Iterable<InfoTable>> getTablesWithFilterAndSchema(@PathVariable("filter") String filter, @PathVariable("schema") String schema){
log.debug("getTablesWithFilterAndSchema called");
try {
Iterable<InfoTable> tables = infoTableService.getTablesWithFilterAndSchema(filter, schema);
return ResponseEntity.ok(tables);
} catch (Exception e) {
log.error("Error occurred while deleting tables", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
} }

@ -25,6 +25,15 @@ public interface InfoTableRepository extends CrudRepository<InfoTable, Long> {
nativeQuery = true) nativeQuery = true)
Iterable<InfoTable> getSchemaByTableName(@Param("table") String table); Iterable<InfoTable> getSchemaByTableName(@Param("table") String table);
@Query(
value = "SELECT * FROM informations_table WHERE name_table LIKE %:filter% ORDER BY CHAR_LENGTH(name_table)",
nativeQuery = true)
Iterable<InfoTable> getTablesWithFilter (@Param("filter") String filter);
@Query(
value = "SELECT * FROM informations_table WHERE name_table LIKE %:filter% AND name_schema=:schema ORDER BY CHAR_LENGTH(name_table)",
nativeQuery = true)
Iterable<InfoTable> getTablesWithFilterAndSchema (@Param("filter") String filter, @Param("schema") String schema);
@Modifying @Modifying
@Transactional @Transactional

@ -68,4 +68,21 @@ public interface InfoTableService {
* @return * @return
*/ */
void truncateMyTable(); void truncateMyTable();
/**
* Récupère les tables contenant les caractères du filtre
* @param filter
* @return InfoTable,
*/
Iterable<InfoTable> getTablesWithFilter(String filter);
/**
* Récupère les tables contenant les caractères du filtre et un schéma spécifique
* @param filter
* @param schema
* @return InfoTable,
*/
Iterable<InfoTable> getTablesWithFilterAndSchema(String filter, String schema);
} }

@ -1,4 +1,4 @@
package com.apside.assist.db.backend.implementation; package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.service.GitService; import com.apside.assist.db.backend.service.GitService;
import lombok.Data; import lombok.Data;
@ -15,7 +15,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@Data
@Service @Service
@Slf4j @Slf4j
public class GitServiceImpl implements GitService { public class GitServiceImpl implements GitService {

@ -1,4 +1,4 @@
package com.apside.assist.db.backend.implementation; package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.repository.InfoColumnRepository; import com.apside.assist.db.backend.repository.InfoColumnRepository;
import com.apside.assist.db.backend.model.InfoColumn; import com.apside.assist.db.backend.model.InfoColumn;

@ -1,4 +1,4 @@
package com.apside.assist.db.backend.implementation; package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.repository.InfoTableRepository; import com.apside.assist.db.backend.repository.InfoTableRepository;
import com.apside.assist.db.backend.model.InfoTable; import com.apside.assist.db.backend.model.InfoTable;
@ -71,4 +71,16 @@ public class InfoTableServiceImpl implements InfoTableService {
infoTableRepository.truncateMyTable(); infoTableRepository.truncateMyTable();
} }
@Override
public Iterable<InfoTable> getTablesWithFilter(String filter){
log.debug("Start getTablesWithFilter method - Custom");
return infoTableRepository.getTablesWithFilter(filter);
}
@Override
public Iterable<InfoTable> getTablesWithFilterAndSchema(String filter, String schema){
log.debug("Start getTablesWithFilterAndSchema method - Custom");
return infoTableRepository.getTablesWithFilterAndSchema(filter, schema);
}
} }

@ -1,4 +1,4 @@
package com.apside.assist.db.backend.implementation; package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.LinkInfo; import com.apside.assist.db.backend.model.LinkInfo;
import com.apside.assist.db.backend.repository.LinkInfoRepository; import com.apside.assist.db.backend.repository.LinkInfoRepository;

@ -1,4 +1,4 @@
package com.apside.assist.db.backend.implementation; package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.LinkInfo; import com.apside.assist.db.backend.model.LinkInfo;
import com.apside.assist.db.backend.model.InfoColumn; import com.apside.assist.db.backend.model.InfoColumn;
@ -11,6 +11,8 @@ import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -19,10 +21,9 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@Data
@Service @Service
@Slf4j @Slf4j
public class ResetDataServiceImpl implements ResetDataService { public class ResetDataServiceImpl implements ResetDataService, InitializingBean {
@Autowired @Autowired
private InfoTableService infoTableService; private InfoTableService infoTableService;
@ -31,6 +32,8 @@ public class ResetDataServiceImpl implements ResetDataService {
@Autowired @Autowired
private LinkInfoService linkInfoService; private LinkInfoService linkInfoService;
@Value("${PATH_FOR_DATA}")
private String pathForData;
private String result; private String result;
private Path path; private Path path;
@ -40,17 +43,21 @@ public class ResetDataServiceImpl implements ResetDataService {
private static final String LENGTH_STRING = "length"; private static final String LENGTH_STRING = "length";
private static final String COLUMN_TEXT_STRING = "column_text"; private static final String COLUMN_TEXT_STRING = "column_text";
public ResetDataServiceImpl() throws IOException {
path = Paths.get("src/main/resources/assistDbData.json"); @Override
public void afterPropertiesSet() throws Exception {
path = Paths.get(pathForData);
result = new String(Files.readAllBytes(path)); result = new String(Files.readAllBytes(path));
} }
@Override
public void deleteEverything(){ public void deleteEverything(){
infoColumnService.truncateMyColumn(); infoColumnService.truncateMyColumn();
infoTableService.truncateMyTable(); infoTableService.truncateMyTable();
linkInfoService.truncateMyLink(); linkInfoService.truncateMyLink();
} }
@Override
public void insertEverything() { public void insertEverything() {
log.debug("Start insert every data into DB"); log.debug("Start insert every data into DB");
JSONArray jo = new JSONArray(result); JSONArray jo = new JSONArray(result);
@ -125,6 +132,7 @@ public class ResetDataServiceImpl implements ResetDataService {
log.info("Insert all data into DB - success"); log.info("Insert all data into DB - success");
} }
@Override
public void checkAndInsertLinks() { public void checkAndInsertLinks() {
log.debug("Start check data into DB and insert links between table and columns"); log.debug("Start check data into DB and insert links between table and columns");
JSONArray jo = new JSONArray(result); JSONArray jo = new JSONArray(result);
@ -187,5 +195,4 @@ public class ResetDataServiceImpl implements ResetDataService {
log.info("Check and insert links data into DB - success"); log.info("Check and insert links data into DB - success");
} }
} }

@ -1,4 +1,4 @@
package com.apside.assist.db.backend.implementation; package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.LinkScriptTag; import com.apside.assist.db.backend.model.LinkScriptTag;
import com.apside.assist.db.backend.model.Script; import com.apside.assist.db.backend.model.Script;
@ -9,7 +9,9 @@ import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.File; import java.io.File;
@ -22,9 +24,8 @@ import java.nio.file.Paths;
import java.util.*; import java.util.*;
@Service @Service
@Data
@Slf4j @Slf4j
public class ScriptsServiceImpl implements ScriptsService { public class ScriptsServiceImpl implements ScriptsService, InitializingBean {
@Autowired @Autowired
private GitService gitService; private GitService gitService;
private String linkScriptTagPath; private String linkScriptTagPath;
@ -35,7 +36,11 @@ public class ScriptsServiceImpl implements ScriptsService {
private String tempDirectoryPath; private String tempDirectoryPath;
private File scriptDirectory; private File scriptDirectory;
private static final String PATH_TO_SCRIPT_DIR = "/AssistDB_AdditionalFiles/Scripts"; @Value("${PATH_FOR_SCRIPT_JSON}")
private String PATH_FOR_SCRIPT_JSON;
@Value("${PATH_TO_SCRIPT_DIR}")
private String PATH_TO_SCRIPT_DIR;
private static final String USER_DIR_STRING = "user.dir"; private static final String USER_DIR_STRING = "user.dir";
private static final String FILE_NAME_STRING = "filename"; private static final String FILE_NAME_STRING = "filename";
private static final String DESCRIPTION_STRING = "description"; private static final String DESCRIPTION_STRING = "description";
@ -44,8 +49,9 @@ public class ScriptsServiceImpl implements ScriptsService {
private static final String TAGS_STRING = "tags"; private static final String TAGS_STRING = "tags";
private static final String DATA_STRING = "data"; private static final String DATA_STRING = "data";
public ScriptsServiceImpl() { @Override
linkScriptTagPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + "/AssistDB_AdditionalFiles/scripts.json"; public void afterPropertiesSet() throws Exception {
linkScriptTagPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_FOR_SCRIPT_JSON;
pathOfLink = Paths.get(linkScriptTagPath); pathOfLink = Paths.get(linkScriptTagPath);
tempDirectoryPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_TO_SCRIPT_DIR; tempDirectoryPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_TO_SCRIPT_DIR;
scriptDirectory = new File(tempDirectoryPath); scriptDirectory = new File(tempDirectoryPath);
@ -200,4 +206,6 @@ public class ScriptsServiceImpl implements ScriptsService {
log.info("Update one link successful"); log.info("Update one link successful");
} }
} }

@ -1,4 +1,4 @@
package com.apside.assist.db.backend.implementation; package com.apside.assist.db.backend.service.implementation;
import com.apside.assist.db.backend.model.Tag; import com.apside.assist.db.backend.model.Tag;
import com.apside.assist.db.backend.service.GitService; import com.apside.assist.db.backend.service.GitService;
@ -8,7 +8,9 @@ import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -20,10 +22,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@Data
@Service @Service
@Slf4j @Slf4j
public class TagsServiceImpl implements TagsService { public class TagsServiceImpl implements TagsService, InitializingBean {
@Autowired @Autowired
private GitService gitService; private GitService gitService;
private String tempDirectoryPath; private String tempDirectoryPath;
@ -31,12 +32,16 @@ public class TagsServiceImpl implements TagsService {
private String jsonContent; private String jsonContent;
private JSONObject dataGlobal; private JSONObject dataGlobal;
private JSONArray data; private JSONArray data;
@Value("${PATH_FOR_TAG_JSON}")
private String PATH_FOR_TAG_JSON;
private static final String TAG_ID_STRING = "tagId"; private static final String TAG_ID_STRING = "tagId";
private static final String DATA_STRING = "data"; private static final String DATA_STRING = "data";
private static final String TAG_STRING = "tag"; private static final String TAG_STRING = "tag";
private static final String DESCRIPTION_STRING = "description"; private static final String DESCRIPTION_STRING = "description";
public TagsServiceImpl() { @Override
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/tags.json"; public void afterPropertiesSet() throws Exception {
tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + PATH_FOR_TAG_JSON;
path = Paths.get(tempDirectoryPath); path = Paths.get(tempDirectoryPath);
} }
@ -127,4 +132,5 @@ public class TagsServiceImpl implements TagsService {
log.info("Update one tag in json successful"); log.info("Update one tag in json successful");
} }
} }

@ -6,7 +6,12 @@ spring.datasource.password=Pompom.21
server.port=9001 server.port=9001
USERNAME_GIT = fhibert@apside.fr USERNAME_GIT = fhibert@apside.fr
ACCESS_TOKEN_GIT = accesstokenexemple ACCESS_TOKEN_GIT = e54612b6d1d73eef4f0a49c88b0e35ccf02d45eb
PATH_FOR_DATA = src/main/resources/assistDbData.json
PATH_FOR_SCRIPT_JSON = /AssistDB_AdditionalFiles/scripts.json
PATH_TO_SCRIPT_DIR = /AssistDB_AdditionalFiles/Scripts
PATH_FOR_TAG_JSON = /AssistDB_AdditionalFiles/tags.json
logging.level.org.springframework.boot.web.embedded.tomcat=INFO logging.level.org.springframework.boot.web.embedded.tomcat=INFO
logging.level.org.springframework=error logging.level.org.springframework=error

@ -16,7 +16,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
@Configuration @Configuration
@PropertySource({"classpath:application.properties"}) @PropertySource({"classpath:application-test.properties"})
@ComponentScan(basePackages = { @ComponentScan(basePackages = {
"com.apside.assistDbBackend.service", "com.apside.assistDbBackend.service",
"com.apside.assistDbBackend.model", "com.apside.assistDbBackend.model",

@ -1,6 +1,6 @@
package com.apside.assist.db.backend.service; package com.apside.assist.db.backend.service;
import com.apside.assist.db.backend.implementation.InfoColumnServiceImpl; import com.apside.assist.db.backend.service.implementation.InfoColumnServiceImpl;
import com.apside.assist.db.backend.repository.InfoColumnRepository; import com.apside.assist.db.backend.repository.InfoColumnRepository;
import com.apside.assist.db.backend.model.InfoColumn; import com.apside.assist.db.backend.model.InfoColumn;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

@ -1,6 +1,6 @@
package com.apside.assist.db.backend.service; package com.apside.assist.db.backend.service;
import com.apside.assist.db.backend.implementation.InfoTableServiceImpl; import com.apside.assist.db.backend.service.implementation.InfoTableServiceImpl;
import com.apside.assist.db.backend.model.InfoTable; import com.apside.assist.db.backend.model.InfoTable;
import com.apside.assist.db.backend.repository.InfoTableRepository; import com.apside.assist.db.backend.repository.InfoTableRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

@ -1,6 +1,6 @@
package com.apside.assist.db.backend.service; package com.apside.assist.db.backend.service;
import com.apside.assist.db.backend.implementation.LinkInfoServiceImpl; import com.apside.assist.db.backend.service.implementation.LinkInfoServiceImpl;
import com.apside.assist.db.backend.repository.LinkInfoRepository; import com.apside.assist.db.backend.repository.LinkInfoRepository;
import com.apside.assist.db.backend.model.LinkInfo; import com.apside.assist.db.backend.model.LinkInfo;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;

@ -1,26 +1,26 @@
package com.apside.assist.db.backend.service; package com.apside.assist.db.backend.service;
import com.apside.assist.db.backend.implementation.InfoColumnServiceImpl;
import com.apside.assist.db.backend.implementation.InfoTableServiceImpl;
import com.apside.assist.db.backend.implementation.LinkInfoServiceImpl;
import com.apside.assist.db.backend.implementation.ResetDataServiceImpl;
import com.apside.assist.db.backend.model.InfoTable; import com.apside.assist.db.backend.model.InfoTable;
import com.apside.assist.db.backend.model.InfoColumn; import com.apside.assist.db.backend.model.InfoColumn;
import com.apside.assist.db.backend.model.LinkInfo; import com.apside.assist.db.backend.model.LinkInfo;
import com.apside.assist.db.backend.service.implementation.InfoColumnServiceImpl;
import com.apside.assist.db.backend.service.implementation.InfoTableServiceImpl;
import com.apside.assist.db.backend.service.implementation.LinkInfoServiceImpl;
import com.apside.assist.db.backend.service.implementation.ResetDataServiceImpl;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@TestPropertySource(locations = {"classpath:application-test.properties"})
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class ResetDataServiceImplTest { class ResetDataServiceImplTest {
@ -32,25 +32,22 @@ class ResetDataServiceImplTest {
@Mock @Mock
private LinkInfoServiceImpl linkInfoServiceImpl; private LinkInfoServiceImpl linkInfoServiceImpl;
@InjectMocks
private ResetDataServiceImpl resetDataServiceImpl; private ResetDataServiceImpl resetDataServiceImpl;
@BeforeEach @BeforeEach
void setUp() throws IOException { public void setUp() throws Exception {
MockitoAnnotations.openMocks(this); ReflectionTestUtils.setField(resetDataServiceImpl, "pathForData", "src/test/resources/testDb.json");
resetDataServiceImpl = new ResetDataServiceImpl(); resetDataServiceImpl.afterPropertiesSet();
resetDataServiceImpl.setInfoTableService(infoTableServiceImpl);
resetDataServiceImpl.setInfoColumnService(infoColumnServiceImpl);
resetDataServiceImpl.setLinkInfoService(linkInfoServiceImpl);
Path tempPath = Paths.get("src/test/resources/testDb.json");
resetDataServiceImpl.setPath(tempPath);
resetDataServiceImpl.setResult(new String(Files.readAllBytes(tempPath)));
} }
@Test @Test
void deleteEverything_shouldTruncateTables() { void deleteEverything_shouldTruncateTables() {
// Arrange // Arrange
doNothing().when(infoColumnServiceImpl).truncateMyColumn();
doNothing().when(infoTableServiceImpl).truncateMyTable();
doNothing().when(linkInfoServiceImpl).truncateMyLink();
// Act // Act
resetDataServiceImpl.deleteEverything(); resetDataServiceImpl.deleteEverything();
@ -63,6 +60,11 @@ class ResetDataServiceImplTest {
@Test @Test
void insertEverything_shouldInsertData() throws IOException { void insertEverything_shouldInsertData() throws IOException {
// Arrange // Arrange
InfoTable infoTable = new InfoTable(1L, "test", "test", "test");
InfoColumn infoColumn = new InfoColumn(1L, "test", "test", 1, "test");
when(infoTableServiceImpl.addOrUpdateTable(any(InfoTable.class))).thenReturn(infoTable);
when(infoColumnServiceImpl.addOrUpdateColumn(any(InfoColumn.class))).thenReturn(infoColumn);
// Act // Act
resetDataServiceImpl.insertEverything(); resetDataServiceImpl.insertEverything();

@ -1,7 +1,7 @@
package com.apside.assist.db.backend.service; package com.apside.assist.db.backend.service;
import com.apside.assist.db.backend.implementation.GitServiceImpl; import com.apside.assist.db.backend.service.implementation.GitServiceImpl;
import com.apside.assist.db.backend.implementation.ScriptsServiceImpl; import com.apside.assist.db.backend.service.implementation.ScriptsServiceImpl;
import com.apside.assist.db.backend.model.LinkScriptTag; import com.apside.assist.db.backend.model.LinkScriptTag;
import com.apside.assist.db.backend.model.Script; import com.apside.assist.db.backend.model.Script;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
@ -12,8 +12,9 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -28,6 +29,7 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@TestPropertySource(locations = {"classpath:application-test.properties"})
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class ScriptsServiceImplTest { class ScriptsServiceImplTest {
@Mock @Mock
@ -36,32 +38,33 @@ class ScriptsServiceImplTest {
@InjectMocks @InjectMocks
private ScriptsServiceImpl scriptsServiceImpl; private ScriptsServiceImpl scriptsServiceImpl;
private Path tempDirectoryPathJson; private Path tempDirectoryPathJsonTest;
private String jsonContent; private String jsonContent;
private JSONObject dataGlobal; private JSONObject dataGlobal;
private JSONArray data; private JSONArray data;
private String tempDirectoryPath; private String tempDirectoryPathTest;
private File scriptDirectory; private File scriptDirectoryTest;
@Mock @Mock
private JSONObject jsonObject; private JSONObject jsonObject;
@BeforeEach @BeforeEach
void setUp() throws IOException { void setUp() throws Exception {
MockitoAnnotations.openMocks(this); ReflectionTestUtils.setField(scriptsServiceImpl, "PATH_FOR_SCRIPT_JSON", "/AssistDB_V2_Back/src/test/resources/scripts.json");
scriptsServiceImpl = new ScriptsServiceImpl(); ReflectionTestUtils.setField(scriptsServiceImpl, "PATH_TO_SCRIPT_DIR", "/AssistDB_V2_Back/src/test/resources/scriptsTest");
tempDirectoryPathJson = Paths.get("src/test/resources/scripts.json");
scriptsServiceImpl.setPathOfLink(tempDirectoryPathJson); scriptsServiceImpl.afterPropertiesSet();
tempDirectoryPathJsonTest = Paths.get("src/test/resources/scripts.json");
jsonContent = "{\"data\":[{\"filename\":\"test.txt\", \"description\":\"Description test\", \"id\":1, \"tags\":[{\"tagname\":\"TXT\"}]}, {\"filename\":\"test2.txt\", \"description\":\"Description test2\", \"id\":2, \"tags\":[{\"tagname\":\"JOIN\"}]}]}"; 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()); Files.write(tempDirectoryPathJsonTest, jsonContent.getBytes());
dataGlobal = new JSONObject(jsonContent); dataGlobal = new JSONObject(jsonContent);
data = dataGlobal.getJSONArray("data"); data = dataGlobal.getJSONArray("data");
scriptsServiceImpl.setDataLinkScriptsTags(data);
tempDirectoryPath = new File("src/test/resources/scriptsTest").toString();
scriptDirectory = new File(tempDirectoryPath); tempDirectoryPathTest = new File("src/test/resources/scriptsTest").toString();
scriptsServiceImpl.setTempDirectoryPath(tempDirectoryPath); scriptDirectoryTest = new File(tempDirectoryPathTest);
scriptsServiceImpl.setScriptDirectory(scriptDirectory);
} }
@ -69,13 +72,12 @@ class ScriptsServiceImplTest {
void retrieveScripts() throws GitAPIException, IOException { void retrieveScripts() throws GitAPIException, IOException {
// mock git service // mock git service
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
scriptsServiceImpl.setGitService(gitServiceImpl);
// create temp directory for test // create temp directory for test
String scriptContent1 = "Script 1 content"; String scriptContent1 = "Script 1 content";
String scriptContent2 = "Script 2 content"; String scriptContent2 = "Script 2 content";
Path filePath1 = Path.of(tempDirectoryPath + "/script1.sql"); Path filePath1 = Path.of(tempDirectoryPathTest + "/script1.sql");
Path filePath2 = Path.of(tempDirectoryPath + "/script2.sql"); Path filePath2 = Path.of(tempDirectoryPathTest + "/script2.sql");
Files.createFile(filePath1); Files.createFile(filePath1);
Files.createFile(filePath2); Files.createFile(filePath2);
Files.writeString(filePath1, scriptContent1); Files.writeString(filePath1, scriptContent1);
@ -121,16 +123,15 @@ class ScriptsServiceImplTest {
// Arrange // Arrange
String scriptName = "testDel.txt"; String scriptName = "testDel.txt";
File scriptFile = new File(scriptDirectory, scriptName); File scriptFile = new File(scriptDirectoryTest, scriptName);
if (!scriptDirectory.exists()) { if (!scriptDirectoryTest.exists()) {
scriptDirectory.mkdirs(); scriptDirectoryTest.mkdirs();
} }
if (!scriptFile.exists()) { if (!scriptFile.exists()) {
scriptFile.createNewFile(); scriptFile.createNewFile();
} }
doNothing().when(gitServiceImpl).pushToGit(); doNothing().when(gitServiceImpl).pushToGit();
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
scriptsServiceImpl.setGitService(gitServiceImpl);
// Act // Act
scriptsServiceImpl.deleteOneScript(scriptName); scriptsServiceImpl.deleteOneScript(scriptName);
@ -145,9 +146,9 @@ class ScriptsServiceImplTest {
void simpleDeleteScript() throws IOException { void simpleDeleteScript() throws IOException {
// Arrange // Arrange
String scriptName = "testDel.txt"; String scriptName = "testDel.txt";
File scriptFile = new File(scriptDirectory, scriptName); File scriptFile = new File(scriptDirectoryTest, scriptName);
if (!scriptDirectory.exists()) { if (!scriptDirectoryTest.exists()) {
scriptDirectory.mkdirs(); scriptDirectoryTest.mkdirs();
} }
if (!scriptFile.exists()) { if (!scriptFile.exists()) {
scriptFile.createNewFile(); scriptFile.createNewFile();
@ -165,9 +166,9 @@ class ScriptsServiceImplTest {
// Arrange // Arrange
String content = "Script content"; String content = "Script content";
String name = "new_script.sql"; String name = "new_script.sql";
File scriptFile = new File(scriptDirectory, name); File scriptFile = new File(scriptDirectoryTest, name);
if (!scriptDirectory.exists()) { if (!scriptDirectoryTest.exists()) {
scriptDirectory.mkdirs(); scriptDirectoryTest.mkdirs();
} }
if (scriptFile.exists()) { if (scriptFile.exists()) {
scriptFile.delete(); scriptFile.delete();
@ -193,12 +194,13 @@ class ScriptsServiceImplTest {
int linkId = 3; int linkId = 3;
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
doNothing().when(gitServiceImpl).pushToGit(); doNothing().when(gitServiceImpl).pushToGit();
scriptsServiceImpl.setGitService(gitServiceImpl);
scriptsServiceImpl.addOneLink(name, description, tagList, linkId); scriptsServiceImpl.addOneLink(name, description, tagList, linkId);
// Assert // Assert
JSONArray updatedDataLinkScriptsTags = scriptsServiceImpl.getDataLinkScriptsTags(); String allScriptsContentTest = new String((Files.readAllBytes(tempDirectoryPathJsonTest)));
JSONObject dataGlobalScriptsTest = new JSONObject(allScriptsContentTest);
JSONArray updatedDataLinkScriptsTags = dataGlobalScriptsTest.getJSONArray("data");
assertEquals(3, updatedDataLinkScriptsTags.length()); assertEquals(3, updatedDataLinkScriptsTags.length());
JSONObject newLink = updatedDataLinkScriptsTags.getJSONObject(2); JSONObject newLink = updatedDataLinkScriptsTags.getJSONObject(2);
@ -226,13 +228,13 @@ class ScriptsServiceImplTest {
//scriptsService.setDataLinkScriptsTags(dataLinkScriptsTags); //scriptsService.setDataLinkScriptsTags(dataLinkScriptsTags);
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
doNothing().when(gitServiceImpl).pushToGit(); doNothing().when(gitServiceImpl).pushToGit();
scriptsServiceImpl.setGitService(gitServiceImpl);
// Act // Act
scriptsServiceImpl.updateOneLink(name, description, tagList, linkId); scriptsServiceImpl.updateOneLink(name, description, tagList, linkId);
// Assert // Assert
JSONObject dataGlob = scriptsServiceImpl.getDataGlobalScripts(); String allScriptsContentTest = new String((Files.readAllBytes(tempDirectoryPathJsonTest)));
JSONArray updatedDataLinkScriptsTags = dataGlob.getJSONArray("data"); JSONObject dataGlobalScriptsTest = new JSONObject(allScriptsContentTest);
JSONArray updatedDataLinkScriptsTags = dataGlobalScriptsTest.getJSONArray("data");
assertEquals(2, updatedDataLinkScriptsTags.length()); assertEquals(2, updatedDataLinkScriptsTags.length());
JSONObject updatedLink = updatedDataLinkScriptsTags.getJSONObject(1); JSONObject updatedLink = updatedDataLinkScriptsTags.getJSONObject(1);
System.out.println(updatedDataLinkScriptsTags); System.out.println(updatedDataLinkScriptsTags);

@ -1,7 +1,7 @@
package com.apside.assist.db.backend.service; package com.apside.assist.db.backend.service;
import com.apside.assist.db.backend.implementation.GitServiceImpl; import com.apside.assist.db.backend.service.implementation.GitServiceImpl;
import com.apside.assist.db.backend.implementation.TagsServiceImpl; import com.apside.assist.db.backend.service.implementation.TagsServiceImpl;
import com.apside.assist.db.backend.model.Tag; import com.apside.assist.db.backend.model.Tag;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.json.JSONArray; import org.json.JSONArray;
@ -12,8 +12,9 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -24,34 +25,31 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@TestPropertySource(locations = {"classpath:application-test.properties"})
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class TagsServiceImplTest { class TagsServiceImplTest {
@Mock @Mock
private GitServiceImpl gitServiceImpl; private GitServiceImpl gitServiceImpl;
@InjectMocks @InjectMocks
private TagsServiceImpl tagsServiceImpl; private TagsServiceImpl tagsServiceImpl;
private Path tempDirectoryPath;
private String jsonContent; private String jsonContent;
private JSONObject dataGlobal; private JSONObject dataGlobal;
private JSONArray data; private JSONArray data;
private Path tempDirectory;
@Mock @Mock
private JSONObject jsonObject; private JSONObject jsonObject;
@BeforeEach @BeforeEach
void setUp() throws IOException { void setUp() throws Exception {
MockitoAnnotations.openMocks(this); ReflectionTestUtils.setField(tagsServiceImpl, "PATH_FOR_TAG_JSON", "/AssistDB_V2_Back/src/test/resources/tagtest.json");
tagsServiceImpl = new TagsServiceImpl(); tagsServiceImpl.afterPropertiesSet();
tempDirectoryPath = Paths.get("src/test/resources/tagtest.json"); jsonContent = "{\"data\":[{\"tagId\":1, \"description\":\"Description 1\", \"tag\":\"Tag1\"},{\"tagId\":2, \"description\":\"Description 2\", \"tag\":\"Tag2\"}]}";
tagsServiceImpl.setPath(tempDirectoryPath); tempDirectory = Paths.get("src/test/resources/tagtest.json");
jsonContent = "{\"data\":[{\"tagId\":1, \"description\":\"Description 1\", \"tag\":\"Tag1\"},{\"tagId\":2, \"description\":\"Description 2\", \"tag\":\"Tag2\",}]}";
Files.write(tempDirectoryPath, jsonContent.getBytes()); Files.write(tempDirectory, jsonContent.getBytes());
dataGlobal = new JSONObject(jsonContent); dataGlobal = new JSONObject(jsonContent);
data = dataGlobal.getJSONArray("data"); data = dataGlobal.getJSONArray("data");
} }
@Test @Test
@ -59,7 +57,6 @@ class TagsServiceImplTest {
void getAllTags_shouldReturnListOfTags() throws IOException, GitAPIException { void getAllTags_shouldReturnListOfTags() throws IOException, GitAPIException {
// Arrange // Arrange
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
tagsServiceImpl.setGitService(gitServiceImpl);
// Act // Act
List<Tag> result = tagsServiceImpl.getAllTags(); List<Tag> result = tagsServiceImpl.getAllTags();
@ -81,7 +78,6 @@ class TagsServiceImplTest {
// Arrange // Arrange
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
doNothing().when(gitServiceImpl).pushToGit(); doNothing().when(gitServiceImpl).pushToGit();
tagsServiceImpl.setGitService(gitServiceImpl);
// Act // Act
tagsServiceImpl.deleteTag("Tag1"); tagsServiceImpl.deleteTag("Tag1");
@ -90,7 +86,7 @@ class TagsServiceImplTest {
verify(gitServiceImpl, times(1)).pullFromGit(); verify(gitServiceImpl, times(1)).pullFromGit();
verify(gitServiceImpl, times(1)).pushToGit(); verify(gitServiceImpl, times(1)).pushToGit();
String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); String updatedJsonContent = new String(Files.readAllBytes(tempDirectory));
JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent);
JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); JSONArray updatedData = updatedDataGlobal.getJSONArray("data");
@ -106,7 +102,6 @@ class TagsServiceImplTest {
// Arrange // Arrange
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
doNothing().when(gitServiceImpl).pushToGit(); doNothing().when(gitServiceImpl).pushToGit();
tagsServiceImpl.setGitService(gitServiceImpl);
Tag newTag = new Tag(3, "Tag3", "Description 3"); Tag newTag = new Tag(3, "Tag3", "Description 3");
@ -117,7 +112,7 @@ class TagsServiceImplTest {
verify(gitServiceImpl, times(2)).pullFromGit(); verify(gitServiceImpl, times(2)).pullFromGit();
verify(gitServiceImpl, times(1)).pushToGit(); verify(gitServiceImpl, times(1)).pushToGit();
String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); String updatedJsonContent = new String(Files.readAllBytes(tempDirectory));
JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent);
JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); JSONArray updatedData = updatedDataGlobal.getJSONArray("data");
@ -133,7 +128,6 @@ class TagsServiceImplTest {
// Arrange // Arrange
doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pullFromGit();
doNothing().when(gitServiceImpl).pushToGit(); doNothing().when(gitServiceImpl).pushToGit();
tagsServiceImpl.setGitService(gitServiceImpl);
Tag modifiedTag = new Tag(2, "ModifiedTag", "ModifiedDescription"); Tag modifiedTag = new Tag(2, "ModifiedTag", "ModifiedDescription");
@ -144,7 +138,7 @@ class TagsServiceImplTest {
verify(gitServiceImpl, times(2)).pullFromGit(); verify(gitServiceImpl, times(2)).pullFromGit();
verify(gitServiceImpl, times(1)).pushToGit(); verify(gitServiceImpl, times(1)).pushToGit();
String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); String updatedJsonContent = new String(Files.readAllBytes(tempDirectory));
JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent);
JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); JSONArray updatedData = updatedDataGlobal.getJSONArray("data");

@ -0,0 +1,14 @@
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
PATH_FOR_DATA = src/test/resources/testDb.json
PATH_FOR_SCRIPT_JSON = /AssistDB_V2_Back/src/test/resources/scripts.json
PATH_TO_SCRIPT_DIR = /AssistDB_V2_Back/src/test/resources/scriptsTest
PATH_FOR_TAG_JSON = /AssistDB_V2_Back/src/test/resources/tagtest.json

@ -1,9 +0,0 @@
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
Loading…
Cancel
Save