diff --git a/src/main/java/com/apside/assist/db/backend/controller/InfoTableController.java b/src/main/java/com/apside/assist/db/backend/controller/InfoTableController.java index 4a11f77..8f474cc 100644 --- a/src/main/java/com/apside/assist/db/backend/controller/InfoTableController.java +++ b/src/main/java/com/apside/assist/db/backend/controller/InfoTableController.java @@ -105,4 +105,28 @@ public class InfoTableController { } } + @GetMapping("/tables/filter/{filter}") + public ResponseEntity> getTablesWithFilter(@PathVariable("filter") String filter){ + log.debug("getTablesWithFilter called"); + try { + Iterable 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> getTablesWithFilterAndSchema(@PathVariable("filter") String filter, @PathVariable("schema") String schema){ + log.debug("getTablesWithFilterAndSchema called"); + try { + Iterable 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(); + } + } + } diff --git a/src/main/java/com/apside/assist/db/backend/repository/InfoTableRepository.java b/src/main/java/com/apside/assist/db/backend/repository/InfoTableRepository.java index c9b7268..ac262da 100644 --- a/src/main/java/com/apside/assist/db/backend/repository/InfoTableRepository.java +++ b/src/main/java/com/apside/assist/db/backend/repository/InfoTableRepository.java @@ -25,6 +25,15 @@ public interface InfoTableRepository extends CrudRepository { nativeQuery = true) Iterable 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 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 getTablesWithFilterAndSchema (@Param("filter") String filter, @Param("schema") String schema); @Modifying @Transactional diff --git a/src/main/java/com/apside/assist/db/backend/service/InfoTableService.java b/src/main/java/com/apside/assist/db/backend/service/InfoTableService.java index f13c349..8ae840a 100644 --- a/src/main/java/com/apside/assist/db/backend/service/InfoTableService.java +++ b/src/main/java/com/apside/assist/db/backend/service/InfoTableService.java @@ -68,4 +68,21 @@ public interface InfoTableService { * @return */ void truncateMyTable(); + + /** + * Récupère les tables contenant les caractères du filtre + * @param filter + * @return InfoTable, + */ + Iterable 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 getTablesWithFilterAndSchema(String filter, String schema); + + } diff --git a/src/main/java/com/apside/assist/db/backend/implementation/GitServiceImpl.java b/src/main/java/com/apside/assist/db/backend/service/implementation/GitServiceImpl.java similarity index 97% rename from src/main/java/com/apside/assist/db/backend/implementation/GitServiceImpl.java rename to src/main/java/com/apside/assist/db/backend/service/implementation/GitServiceImpl.java index b718389..3a4330f 100644 --- a/src/main/java/com/apside/assist/db/backend/implementation/GitServiceImpl.java +++ b/src/main/java/com/apside/assist/db/backend/service/implementation/GitServiceImpl.java @@ -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 lombok.Data; @@ -15,7 +15,6 @@ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; -@Data @Service @Slf4j public class GitServiceImpl implements GitService { diff --git a/src/main/java/com/apside/assist/db/backend/implementation/InfoColumnServiceImpl.java b/src/main/java/com/apside/assist/db/backend/service/implementation/InfoColumnServiceImpl.java similarity index 97% rename from src/main/java/com/apside/assist/db/backend/implementation/InfoColumnServiceImpl.java rename to src/main/java/com/apside/assist/db/backend/service/implementation/InfoColumnServiceImpl.java index 52ffa33..a6361e9 100644 --- a/src/main/java/com/apside/assist/db/backend/implementation/InfoColumnServiceImpl.java +++ b/src/main/java/com/apside/assist/db/backend/service/implementation/InfoColumnServiceImpl.java @@ -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.model.InfoColumn; diff --git a/src/main/java/com/apside/assist/db/backend/implementation/InfoTableServiceImpl.java b/src/main/java/com/apside/assist/db/backend/service/implementation/InfoTableServiceImpl.java similarity index 79% rename from src/main/java/com/apside/assist/db/backend/implementation/InfoTableServiceImpl.java rename to src/main/java/com/apside/assist/db/backend/service/implementation/InfoTableServiceImpl.java index 0e74555..32221bf 100644 --- a/src/main/java/com/apside/assist/db/backend/implementation/InfoTableServiceImpl.java +++ b/src/main/java/com/apside/assist/db/backend/service/implementation/InfoTableServiceImpl.java @@ -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.model.InfoTable; @@ -71,4 +71,16 @@ public class InfoTableServiceImpl implements InfoTableService { infoTableRepository.truncateMyTable(); } + @Override + public Iterable getTablesWithFilter(String filter){ + log.debug("Start getTablesWithFilter method - Custom"); + return infoTableRepository.getTablesWithFilter(filter); + } + @Override + public Iterable getTablesWithFilterAndSchema(String filter, String schema){ + log.debug("Start getTablesWithFilterAndSchema method - Custom"); + return infoTableRepository.getTablesWithFilterAndSchema(filter, schema); + } + + } diff --git a/src/main/java/com/apside/assist/db/backend/implementation/LinkInfoServiceImpl.java b/src/main/java/com/apside/assist/db/backend/service/implementation/LinkInfoServiceImpl.java similarity index 96% rename from src/main/java/com/apside/assist/db/backend/implementation/LinkInfoServiceImpl.java rename to src/main/java/com/apside/assist/db/backend/service/implementation/LinkInfoServiceImpl.java index 3855729..8be0d0e 100644 --- a/src/main/java/com/apside/assist/db/backend/implementation/LinkInfoServiceImpl.java +++ b/src/main/java/com/apside/assist/db/backend/service/implementation/LinkInfoServiceImpl.java @@ -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.repository.LinkInfoRepository; diff --git a/src/main/java/com/apside/assist/db/backend/implementation/ResetDataServiceImpl.java b/src/main/java/com/apside/assist/db/backend/service/implementation/ResetDataServiceImpl.java similarity index 93% rename from src/main/java/com/apside/assist/db/backend/implementation/ResetDataServiceImpl.java rename to src/main/java/com/apside/assist/db/backend/service/implementation/ResetDataServiceImpl.java index e7f066d..cf50dbf 100644 --- a/src/main/java/com/apside/assist/db/backend/implementation/ResetDataServiceImpl.java +++ b/src/main/java/com/apside/assist/db/backend/service/implementation/ResetDataServiceImpl.java @@ -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.InfoColumn; @@ -11,6 +11,8 @@ import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.json.JSONArray; 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.beans.factory.annotation.Autowired; @@ -19,10 +21,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -@Data @Service @Slf4j -public class ResetDataServiceImpl implements ResetDataService { +public class ResetDataServiceImpl implements ResetDataService, InitializingBean { @Autowired private InfoTableService infoTableService; @@ -31,6 +32,8 @@ public class ResetDataServiceImpl implements ResetDataService { @Autowired private LinkInfoService linkInfoService; + @Value("${PATH_FOR_DATA}") + private String pathForData; private String result; private Path path; @@ -40,17 +43,21 @@ public class ResetDataServiceImpl implements ResetDataService { private static final String LENGTH_STRING = "length"; 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)); } + @Override public void deleteEverything(){ infoColumnService.truncateMyColumn(); infoTableService.truncateMyTable(); linkInfoService.truncateMyLink(); } + @Override public void insertEverything() { log.debug("Start insert every data into DB"); JSONArray jo = new JSONArray(result); @@ -125,6 +132,7 @@ public class ResetDataServiceImpl implements ResetDataService { log.info("Insert all data into DB - success"); } + @Override public void checkAndInsertLinks() { log.debug("Start check data into DB and insert links between table and columns"); JSONArray jo = new JSONArray(result); @@ -187,5 +195,4 @@ public class ResetDataServiceImpl implements ResetDataService { log.info("Check and insert links data into DB - success"); } - } diff --git a/src/main/java/com/apside/assist/db/backend/implementation/ScriptsServiceImpl.java b/src/main/java/com/apside/assist/db/backend/service/implementation/ScriptsServiceImpl.java similarity index 93% rename from src/main/java/com/apside/assist/db/backend/implementation/ScriptsServiceImpl.java rename to src/main/java/com/apside/assist/db/backend/service/implementation/ScriptsServiceImpl.java index 549ebc7..c1bc3eb 100644 --- a/src/main/java/com/apside/assist/db/backend/implementation/ScriptsServiceImpl.java +++ b/src/main/java/com/apside/assist/db/backend/service/implementation/ScriptsServiceImpl.java @@ -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.Script; @@ -9,7 +9,9 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.jgit.api.errors.GitAPIException; import org.json.JSONArray; import org.json.JSONObject; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.File; @@ -22,9 +24,8 @@ import java.nio.file.Paths; import java.util.*; @Service -@Data @Slf4j -public class ScriptsServiceImpl implements ScriptsService { +public class ScriptsServiceImpl implements ScriptsService, InitializingBean { @Autowired private GitService gitService; private String linkScriptTagPath; @@ -35,7 +36,11 @@ public class ScriptsServiceImpl implements ScriptsService { private String tempDirectoryPath; 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 FILE_NAME_STRING = "filename"; 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 DATA_STRING = "data"; - public ScriptsServiceImpl() { - linkScriptTagPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + "/AssistDB_AdditionalFiles/scripts.json"; + @Override + public void afterPropertiesSet() throws Exception { + linkScriptTagPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_FOR_SCRIPT_JSON; pathOfLink = Paths.get(linkScriptTagPath); tempDirectoryPath = new File(System.getProperty(USER_DIR_STRING)).getParent() + PATH_TO_SCRIPT_DIR; scriptDirectory = new File(tempDirectoryPath); @@ -200,4 +206,6 @@ public class ScriptsServiceImpl implements ScriptsService { log.info("Update one link successful"); } + + } diff --git a/src/main/java/com/apside/assist/db/backend/implementation/TagsServiceImpl.java b/src/main/java/com/apside/assist/db/backend/service/implementation/TagsServiceImpl.java similarity index 91% rename from src/main/java/com/apside/assist/db/backend/implementation/TagsServiceImpl.java rename to src/main/java/com/apside/assist/db/backend/service/implementation/TagsServiceImpl.java index 9dafe34..4abcf27 100644 --- a/src/main/java/com/apside/assist/db/backend/implementation/TagsServiceImpl.java +++ b/src/main/java/com/apside/assist/db/backend/service/implementation/TagsServiceImpl.java @@ -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.service.GitService; @@ -8,7 +8,9 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.jgit.api.errors.GitAPIException; import org.json.JSONArray; import org.json.JSONObject; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.File; import java.io.IOException; @@ -20,10 +22,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; -@Data @Service @Slf4j -public class TagsServiceImpl implements TagsService { +public class TagsServiceImpl implements TagsService, InitializingBean { @Autowired private GitService gitService; private String tempDirectoryPath; @@ -31,12 +32,16 @@ public class TagsServiceImpl implements TagsService { private String jsonContent; private JSONObject dataGlobal; 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 DATA_STRING = "data"; private static final String TAG_STRING = "tag"; private static final String DESCRIPTION_STRING = "description"; - public TagsServiceImpl() { - tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/tags.json"; + @Override + public void afterPropertiesSet() throws Exception { + tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + PATH_FOR_TAG_JSON; path = Paths.get(tempDirectoryPath); } @@ -127,4 +132,5 @@ public class TagsServiceImpl implements TagsService { log.info("Update one tag in json successful"); } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e0fd3c8..1fb3b8f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -6,7 +6,12 @@ spring.datasource.password=Pompom.21 server.port=9001 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=error +logging.level.org.springframework=error \ No newline at end of file diff --git a/src/test/java/com/apside/assist/db/backend/AssistDBBackendTest.java b/src/test/java/com/apside/assist/db/backend/AssistDBBackendTest.java index 57e6d16..6de7fbb 100644 --- a/src/test/java/com/apside/assist/db/backend/AssistDBBackendTest.java +++ b/src/test/java/com/apside/assist/db/backend/AssistDBBackendTest.java @@ -16,7 +16,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.test.web.servlet.MockMvc; @Configuration -@PropertySource({"classpath:application.properties"}) +@PropertySource({"classpath:application-test.properties"}) @ComponentScan(basePackages = { "com.apside.assistDbBackend.service", "com.apside.assistDbBackend.model", diff --git a/src/test/java/com/apside/assist/db/backend/service/InfoColumnServiceImplTest.java b/src/test/java/com/apside/assist/db/backend/service/InfoColumnServiceImplTest.java index a0e4d49..35bffb5 100644 --- a/src/test/java/com/apside/assist/db/backend/service/InfoColumnServiceImplTest.java +++ b/src/test/java/com/apside/assist/db/backend/service/InfoColumnServiceImplTest.java @@ -1,6 +1,6 @@ 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.model.InfoColumn; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/apside/assist/db/backend/service/InfoTableServiceImplTest.java b/src/test/java/com/apside/assist/db/backend/service/InfoTableServiceImplTest.java index 806d924..4cb6edf 100644 --- a/src/test/java/com/apside/assist/db/backend/service/InfoTableServiceImplTest.java +++ b/src/test/java/com/apside/assist/db/backend/service/InfoTableServiceImplTest.java @@ -1,6 +1,6 @@ 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.repository.InfoTableRepository; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/apside/assist/db/backend/service/LinkInfoServiceImplTest.java b/src/test/java/com/apside/assist/db/backend/service/LinkInfoServiceImplTest.java index 5032fe6..e72cf3b 100644 --- a/src/test/java/com/apside/assist/db/backend/service/LinkInfoServiceImplTest.java +++ b/src/test/java/com/apside/assist/db/backend/service/LinkInfoServiceImplTest.java @@ -1,6 +1,6 @@ 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.model.LinkInfo; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/apside/assist/db/backend/service/ResetDataServiceImplTest.java b/src/test/java/com/apside/assist/db/backend/service/ResetDataServiceImplTest.java index fbd6e9a..d315c27 100644 --- a/src/test/java/com/apside/assist/db/backend/service/ResetDataServiceImplTest.java +++ b/src/test/java/com/apside/assist/db/backend/service/ResetDataServiceImplTest.java @@ -1,26 +1,26 @@ 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.InfoColumn; 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.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 org.springframework.test.context.TestPropertySource; +import org.springframework.test.util.ReflectionTestUtils; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import static org.mockito.Mockito.*; +@TestPropertySource(locations = {"classpath:application-test.properties"}) @ExtendWith(MockitoExtension.class) class ResetDataServiceImplTest { @@ -32,25 +32,22 @@ class ResetDataServiceImplTest { @Mock private LinkInfoServiceImpl linkInfoServiceImpl; + + @InjectMocks private ResetDataServiceImpl resetDataServiceImpl; @BeforeEach - void setUp() throws IOException { - MockitoAnnotations.openMocks(this); - resetDataServiceImpl = new ResetDataServiceImpl(); - 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))); - + public void setUp() throws Exception { + ReflectionTestUtils.setField(resetDataServiceImpl, "pathForData", "src/test/resources/testDb.json"); + resetDataServiceImpl.afterPropertiesSet(); } @Test void deleteEverything_shouldTruncateTables() { // Arrange - + doNothing().when(infoColumnServiceImpl).truncateMyColumn(); + doNothing().when(infoTableServiceImpl).truncateMyTable(); + doNothing().when(linkInfoServiceImpl).truncateMyLink(); // Act resetDataServiceImpl.deleteEverything(); @@ -63,6 +60,11 @@ class ResetDataServiceImplTest { @Test void insertEverything_shouldInsertData() throws IOException { // 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 resetDataServiceImpl.insertEverything(); diff --git a/src/test/java/com/apside/assist/db/backend/service/ScriptsServiceImplTest.java b/src/test/java/com/apside/assist/db/backend/service/ScriptsServiceImplTest.java index 07f8085..31c36a8 100644 --- a/src/test/java/com/apside/assist/db/backend/service/ScriptsServiceImplTest.java +++ b/src/test/java/com/apside/assist/db/backend/service/ScriptsServiceImplTest.java @@ -1,7 +1,7 @@ package com.apside.assist.db.backend.service; -import com.apside.assist.db.backend.implementation.GitServiceImpl; -import com.apside.assist.db.backend.implementation.ScriptsServiceImpl; +import com.apside.assist.db.backend.service.implementation.GitServiceImpl; +import com.apside.assist.db.backend.service.implementation.ScriptsServiceImpl; import com.apside.assist.db.backend.model.LinkScriptTag; import com.apside.assist.db.backend.model.Script; 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.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; 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.IOException; @@ -28,6 +29,7 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; +@TestPropertySource(locations = {"classpath:application-test.properties"}) @ExtendWith(MockitoExtension.class) class ScriptsServiceImplTest { @Mock @@ -36,32 +38,33 @@ class ScriptsServiceImplTest { @InjectMocks private ScriptsServiceImpl scriptsServiceImpl; - private Path tempDirectoryPathJson; + private Path tempDirectoryPathJsonTest; private String jsonContent; private JSONObject dataGlobal; private JSONArray data; - private String tempDirectoryPath; - private File scriptDirectory; + private String tempDirectoryPathTest; + private File scriptDirectoryTest; @Mock private JSONObject jsonObject; @BeforeEach - void setUp() throws IOException { - MockitoAnnotations.openMocks(this); - scriptsServiceImpl = new ScriptsServiceImpl(); - tempDirectoryPathJson = Paths.get("src/test/resources/scripts.json"); - scriptsServiceImpl.setPathOfLink(tempDirectoryPathJson); + void setUp() throws Exception { + ReflectionTestUtils.setField(scriptsServiceImpl, "PATH_FOR_SCRIPT_JSON", "/AssistDB_V2_Back/src/test/resources/scripts.json"); + ReflectionTestUtils.setField(scriptsServiceImpl, "PATH_TO_SCRIPT_DIR", "/AssistDB_V2_Back/src/test/resources/scriptsTest"); + + 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\"}]}]}"; - Files.write(tempDirectoryPathJson, jsonContent.getBytes()); + Files.write(tempDirectoryPathJsonTest, jsonContent.getBytes()); dataGlobal = new JSONObject(jsonContent); data = dataGlobal.getJSONArray("data"); - scriptsServiceImpl.setDataLinkScriptsTags(data); - tempDirectoryPath = new File("src/test/resources/scriptsTest").toString(); - scriptDirectory = new File(tempDirectoryPath); - scriptsServiceImpl.setTempDirectoryPath(tempDirectoryPath); - scriptsServiceImpl.setScriptDirectory(scriptDirectory); + + tempDirectoryPathTest = new File("src/test/resources/scriptsTest").toString(); + scriptDirectoryTest = new File(tempDirectoryPathTest); + } @@ -69,13 +72,12 @@ class ScriptsServiceImplTest { void retrieveScripts() throws GitAPIException, IOException { // mock git service doNothing().when(gitServiceImpl).pullFromGit(); - scriptsServiceImpl.setGitService(gitServiceImpl); // 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"); + Path filePath1 = Path.of(tempDirectoryPathTest + "/script1.sql"); + Path filePath2 = Path.of(tempDirectoryPathTest + "/script2.sql"); Files.createFile(filePath1); Files.createFile(filePath2); Files.writeString(filePath1, scriptContent1); @@ -121,16 +123,15 @@ class ScriptsServiceImplTest { // Arrange String scriptName = "testDel.txt"; - File scriptFile = new File(scriptDirectory, scriptName); - if (!scriptDirectory.exists()) { - scriptDirectory.mkdirs(); + File scriptFile = new File(scriptDirectoryTest, scriptName); + if (!scriptDirectoryTest.exists()) { + scriptDirectoryTest.mkdirs(); } if (!scriptFile.exists()) { scriptFile.createNewFile(); } doNothing().when(gitServiceImpl).pushToGit(); doNothing().when(gitServiceImpl).pullFromGit(); - scriptsServiceImpl.setGitService(gitServiceImpl); // Act scriptsServiceImpl.deleteOneScript(scriptName); @@ -145,9 +146,9 @@ class ScriptsServiceImplTest { void simpleDeleteScript() throws IOException { // Arrange String scriptName = "testDel.txt"; - File scriptFile = new File(scriptDirectory, scriptName); - if (!scriptDirectory.exists()) { - scriptDirectory.mkdirs(); + File scriptFile = new File(scriptDirectoryTest, scriptName); + if (!scriptDirectoryTest.exists()) { + scriptDirectoryTest.mkdirs(); } if (!scriptFile.exists()) { scriptFile.createNewFile(); @@ -165,9 +166,9 @@ class ScriptsServiceImplTest { // Arrange String content = "Script content"; String name = "new_script.sql"; - File scriptFile = new File(scriptDirectory, name); - if (!scriptDirectory.exists()) { - scriptDirectory.mkdirs(); + File scriptFile = new File(scriptDirectoryTest, name); + if (!scriptDirectoryTest.exists()) { + scriptDirectoryTest.mkdirs(); } if (scriptFile.exists()) { scriptFile.delete(); @@ -193,12 +194,13 @@ class ScriptsServiceImplTest { int linkId = 3; doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pushToGit(); - scriptsServiceImpl.setGitService(gitServiceImpl); scriptsServiceImpl.addOneLink(name, description, tagList, linkId); // 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()); JSONObject newLink = updatedDataLinkScriptsTags.getJSONObject(2); @@ -226,13 +228,13 @@ class ScriptsServiceImplTest { //scriptsService.setDataLinkScriptsTags(dataLinkScriptsTags); doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pushToGit(); - scriptsServiceImpl.setGitService(gitServiceImpl); // Act scriptsServiceImpl.updateOneLink(name, description, tagList, linkId); // Assert - JSONObject dataGlob = scriptsServiceImpl.getDataGlobalScripts(); - JSONArray updatedDataLinkScriptsTags = dataGlob.getJSONArray("data"); + String allScriptsContentTest = new String((Files.readAllBytes(tempDirectoryPathJsonTest))); + JSONObject dataGlobalScriptsTest = new JSONObject(allScriptsContentTest); + JSONArray updatedDataLinkScriptsTags = dataGlobalScriptsTest.getJSONArray("data"); assertEquals(2, updatedDataLinkScriptsTags.length()); JSONObject updatedLink = updatedDataLinkScriptsTags.getJSONObject(1); System.out.println(updatedDataLinkScriptsTags); diff --git a/src/test/java/com/apside/assist/db/backend/service/TagsServiceImplTest.java b/src/test/java/com/apside/assist/db/backend/service/TagsServiceImplTest.java index 4a7de7a..0cde240 100644 --- a/src/test/java/com/apside/assist/db/backend/service/TagsServiceImplTest.java +++ b/src/test/java/com/apside/assist/db/backend/service/TagsServiceImplTest.java @@ -1,7 +1,7 @@ package com.apside.assist.db.backend.service; -import com.apside.assist.db.backend.implementation.GitServiceImpl; -import com.apside.assist.db.backend.implementation.TagsServiceImpl; +import com.apside.assist.db.backend.service.implementation.GitServiceImpl; +import com.apside.assist.db.backend.service.implementation.TagsServiceImpl; import com.apside.assist.db.backend.model.Tag; import org.eclipse.jgit.api.errors.GitAPIException; import org.json.JSONArray; @@ -12,8 +12,9 @@ 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 org.springframework.test.context.TestPropertySource; +import org.springframework.test.util.ReflectionTestUtils; import java.io.IOException; import java.net.URISyntaxException; @@ -24,34 +25,31 @@ import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +@TestPropertySource(locations = {"classpath:application-test.properties"}) @ExtendWith(MockitoExtension.class) class TagsServiceImplTest { @Mock private GitServiceImpl gitServiceImpl; - @InjectMocks private TagsServiceImpl tagsServiceImpl; - - private Path tempDirectoryPath; private String jsonContent; private JSONObject dataGlobal; private JSONArray data; - + private Path tempDirectory; @Mock private JSONObject jsonObject; @BeforeEach - void setUp() throws IOException { - MockitoAnnotations.openMocks(this); - tagsServiceImpl = new TagsServiceImpl(); - tempDirectoryPath = Paths.get("src/test/resources/tagtest.json"); - tagsServiceImpl.setPath(tempDirectoryPath); - jsonContent = "{\"data\":[{\"tagId\":1, \"description\":\"Description 1\", \"tag\":\"Tag1\"},{\"tagId\":2, \"description\":\"Description 2\", \"tag\":\"Tag2\",}]}"; - Files.write(tempDirectoryPath, jsonContent.getBytes()); + void setUp() throws Exception { + ReflectionTestUtils.setField(tagsServiceImpl, "PATH_FOR_TAG_JSON", "/AssistDB_V2_Back/src/test/resources/tagtest.json"); + tagsServiceImpl.afterPropertiesSet(); + jsonContent = "{\"data\":[{\"tagId\":1, \"description\":\"Description 1\", \"tag\":\"Tag1\"},{\"tagId\":2, \"description\":\"Description 2\", \"tag\":\"Tag2\"}]}"; + tempDirectory = Paths.get("src/test/resources/tagtest.json"); + + Files.write(tempDirectory, jsonContent.getBytes()); dataGlobal = new JSONObject(jsonContent); data = dataGlobal.getJSONArray("data"); - } @Test @@ -59,7 +57,6 @@ class TagsServiceImplTest { void getAllTags_shouldReturnListOfTags() throws IOException, GitAPIException { // Arrange doNothing().when(gitServiceImpl).pullFromGit(); - tagsServiceImpl.setGitService(gitServiceImpl); // Act List result = tagsServiceImpl.getAllTags(); @@ -81,7 +78,6 @@ class TagsServiceImplTest { // Arrange doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pushToGit(); - tagsServiceImpl.setGitService(gitServiceImpl); // Act tagsServiceImpl.deleteTag("Tag1"); @@ -90,7 +86,7 @@ class TagsServiceImplTest { verify(gitServiceImpl, times(1)).pullFromGit(); verify(gitServiceImpl, times(1)).pushToGit(); - String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); + String updatedJsonContent = new String(Files.readAllBytes(tempDirectory)); JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); @@ -106,7 +102,6 @@ class TagsServiceImplTest { // Arrange doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pushToGit(); - tagsServiceImpl.setGitService(gitServiceImpl); Tag newTag = new Tag(3, "Tag3", "Description 3"); @@ -117,7 +112,7 @@ class TagsServiceImplTest { verify(gitServiceImpl, times(2)).pullFromGit(); verify(gitServiceImpl, times(1)).pushToGit(); - String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); + String updatedJsonContent = new String(Files.readAllBytes(tempDirectory)); JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); @@ -133,7 +128,6 @@ class TagsServiceImplTest { // Arrange doNothing().when(gitServiceImpl).pullFromGit(); doNothing().when(gitServiceImpl).pushToGit(); - tagsServiceImpl.setGitService(gitServiceImpl); Tag modifiedTag = new Tag(2, "ModifiedTag", "ModifiedDescription"); @@ -144,7 +138,7 @@ class TagsServiceImplTest { verify(gitServiceImpl, times(2)).pullFromGit(); verify(gitServiceImpl, times(1)).pushToGit(); - String updatedJsonContent = new String(Files.readAllBytes(tempDirectoryPath)); + String updatedJsonContent = new String(Files.readAllBytes(tempDirectory)); JSONObject updatedDataGlobal = new JSONObject(updatedJsonContent); JSONArray updatedData = updatedDataGlobal.getJSONArray("data"); diff --git a/src/test/resources/application-test.properties b/src/test/resources/application-test.properties new file mode 100644 index 0000000..87f0b97 --- /dev/null +++ b/src/test/resources/application-test.properties @@ -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 \ No newline at end of file diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties deleted file mode 100644 index 02aeabf..0000000 --- a/src/test/resources/application.properties +++ /dev/null @@ -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 \ No newline at end of file