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