|
1 |
package cz.zcu.fav.kiv.antipatterndetectionapp.repository;
|
|
2 |
|
|
3 |
import static org.junit.jupiter.api.Assertions.*;
|
|
4 |
|
|
5 |
import com.fasterxml.jackson.databind.JsonNode;
|
|
6 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.AntiPattern;
|
|
7 |
import cz.zcu.fav.kiv.antipatterndetectionapp.model.Threshold;
|
|
8 |
import cz.zcu.fav.kiv.antipatterndetectionapp.utils.JsonParser;
|
|
9 |
import org.apache.tomcat.util.http.fileupload.FileUtils;
|
|
10 |
import org.junit.jupiter.api.BeforeEach;
|
|
11 |
import org.junit.jupiter.api.Test;
|
|
12 |
import org.springframework.core.io.FileSystemResource;
|
|
13 |
|
|
14 |
import java.io.BufferedReader;
|
|
15 |
import java.io.File;
|
|
16 |
import java.io.IOException;
|
|
17 |
import java.io.InputStreamReader;
|
|
18 |
import java.util.ArrayList;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.List;
|
|
21 |
import java.util.Map;
|
|
22 |
|
|
23 |
public class ConfigurationRepositoryTest {
|
|
24 |
|
|
25 |
ConfigurationRepository configurationRepository;
|
|
26 |
|
|
27 |
@BeforeEach
|
|
28 |
void setUp(){
|
|
29 |
configurationRepository = new ConfigurationRepository("data/");
|
|
30 |
}
|
|
31 |
|
|
32 |
@Test
|
|
33 |
void testSaveNewConfiguration(){
|
|
34 |
// creating test threshold
|
|
35 |
Threshold threshold = new Threshold("test", "test", "test", "test", "Integer");
|
|
36 |
Map<String, Threshold> thresholdMap = new HashMap<>();
|
|
37 |
thresholdMap.put("testThreshold", threshold);
|
|
38 |
|
|
39 |
// creating test anti-pattern
|
|
40 |
AntiPattern antiPattern = new AntiPattern(1L, "test", "test", "test", thresholdMap);
|
|
41 |
List<AntiPattern> antiPatternList = new ArrayList<>();
|
|
42 |
antiPatternList.add(antiPattern);
|
|
43 |
String[] antiPatternNames ={"test"};
|
|
44 |
String[] thresholdNames ={"testThreshold"};
|
|
45 |
String[] thresholdValues ={"1"};
|
|
46 |
|
|
47 |
// testing of new configuration save
|
|
48 |
List<String> resultOfSave = configurationRepository.saveNewConfiguration(antiPatternList, "test/testConfiguration", antiPatternNames, thresholdNames, thresholdValues, true);
|
|
49 |
|
|
50 |
// checking results
|
|
51 |
assertEquals(0, resultOfSave.size(), "Configuration was not set correctly.");
|
|
52 |
assertEquals(1, configurationRepository.allConfigurations.get("test/testConfiguration").size(), "Configuration was not set correctly.");
|
|
53 |
assertEquals("1", configurationRepository.allConfigurations.get("test/testConfiguration").get("test").get("testThreshold"), "Configuration was not set correctly.");
|
|
54 |
|
|
55 |
// testing of configuration update
|
|
56 |
thresholdValues[0] = "2";
|
|
57 |
configurationRepository.saveNewConfiguration(antiPatternList, "test/testConfiguration", antiPatternNames, thresholdNames, thresholdValues, false);
|
|
58 |
|
|
59 |
// checking results
|
|
60 |
assertEquals(1, configurationRepository.allConfigurations.get("test/testConfiguration").size(), "Configuration was not set correctly.");
|
|
61 |
assertEquals("2", configurationRepository.allConfigurations.get("test/testConfiguration").get("test").get("testThreshold"), "Configuration was not set correctly.");
|
|
62 |
|
|
63 |
// testing of configuration wrong threshold value update
|
|
64 |
thresholdValues[0] = "test";
|
|
65 |
List<String> resultOfUpdate = configurationRepository.saveNewConfiguration(antiPatternList, "test/testConfiguration", antiPatternNames, thresholdNames, thresholdValues, false);
|
|
66 |
assertEquals(1, resultOfUpdate.size(), "Configuration was not set correctly.");
|
|
67 |
}
|
|
68 |
|
|
69 |
@Test
|
|
70 |
void testSaveConfigurationToFile(){
|
|
71 |
// preparing configuration to save
|
|
72 |
String testName = "test/test";
|
|
73 |
Map<String, Map<String, String>> testConfiguration = new HashMap<>();
|
|
74 |
Map<String, String> testAntiPattern = new HashMap<>();
|
|
75 |
|
|
76 |
testAntiPattern.put("testThreshold", "testValue");
|
|
77 |
testConfiguration.put("testAntiPattern", testAntiPattern);
|
|
78 |
new File(new FileSystemResource("data/configurations/test/").getFile().getAbsolutePath()).mkdir();
|
|
79 |
|
|
80 |
// saving configuration
|
|
81 |
configurationRepository.saveConfigurationToFile(testName, testConfiguration);
|
|
82 |
|
|
83 |
// checking result json file
|
|
84 |
File testConfigFile = new File(new FileSystemResource("data/configurations/test/test.json").getFile().getAbsolutePath());
|
|
85 |
|
|
86 |
String jsonContent = ""; // json configuration file content
|
|
87 |
try {
|
|
88 |
BufferedReader read = new BufferedReader(new InputStreamReader(testConfigFile.toURI().toURL().openStream()));
|
|
89 |
|
|
90 |
String line;
|
|
91 |
while ((line = read.readLine()) != null) {
|
|
92 |
jsonContent += line;
|
|
93 |
}
|
|
94 |
read.close();
|
|
95 |
}
|
|
96 |
catch(Exception e){}
|
|
97 |
|
|
98 |
JsonNode rootNode = null;
|
|
99 |
try {
|
|
100 |
rootNode = JsonParser.parse(jsonContent);
|
|
101 |
} catch (IOException e) {}
|
|
102 |
|
|
103 |
JsonNode configurations = rootNode.get("configuration");
|
|
104 |
JsonNode thresholds = configurations.get(0).get("thresholds");
|
|
105 |
|
|
106 |
// checking results
|
|
107 |
assertEquals("testAntiPattern", configurations.get(0).get("antiPattern").asText());
|
|
108 |
assertEquals("testThreshold", thresholds.get(0).get("thresholdName").asText());
|
|
109 |
assertEquals("testValue", thresholds.get(0).get("value").asText());
|
|
110 |
|
|
111 |
// deleting test temp file
|
|
112 |
try {
|
|
113 |
FileUtils.forceDelete(new File(new FileSystemResource("data/configurations/test").getFile().getAbsolutePath()));
|
|
114 |
} catch (IOException e) {
|
|
115 |
e.printStackTrace();
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
@Test
|
|
120 |
void testConfigurationRepository(){
|
|
121 |
for(String configuration : configurationRepository.allConfigurations.keySet()){
|
|
122 |
assertEquals(configurationRepository.allConfigurations.get(configuration) == null, false, "Configuration was not loaded correctly.");
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
Tests for configurations and json parsing added