Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 912e76c4

Přidáno uživatelem stepanekp před asi 3 roky(ů)

Tests for configurations and json parsing added

Zobrazit rozdíly:

pom.xml
50 50
			<artifactId>reflections</artifactId>
51 51
			<version>0.9.12</version>
52 52
		</dependency>
53
		<!-- jsoup HTML parser library @ https://jsoup.org/ -->
53 54
		<dependency>
54
			<!-- jsoup HTML parser library @ https://jsoup.org/ -->
55 55
			<groupId>org.jsoup</groupId>
56 56
			<artifactId>jsoup</artifactId>
57 57
			<version>1.14.3</version>
......
72 72
			<artifactId>jackson-annotations</artifactId>
73 73
			<version>2.13.2</version>
74 74
		</dependency>
75
		<!-- Junit libraries for unit tests-->
76
		<dependency>
77
			<groupId>org.junit.jupiter</groupId>
78
			<artifactId>junit-jupiter-api</artifactId>
79
			<version>5.4.0</version>
80
			<scope>test</scope>
81
		</dependency>
82
		<dependency>
83
			<groupId>org.junit.jupiter</groupId>
84
			<artifactId>junit-jupiter-engine</artifactId>
85
			<version>5.4.0</version>
86
			<scope>test</scope>
87
		</dependency>
75 88
	</dependencies>
76 89

  
77 90
	<build>
......
80 93
				<groupId>org.springframework.boot</groupId>
81 94
				<artifactId>spring-boot-maven-plugin</artifactId>
82 95
			</plugin>
96
			<plugin>
97
				<artifactId>maven-surefire-plugin</artifactId>
98
				<version>2.22.0</version>
99
				<configuration>
100
					<skipTests>false</skipTests>
101
				</configuration>
102
			</plugin>
103
			<plugin>
104
				<artifactId>maven-failsafe-plugin</artifactId>
105
				<version>2.22.0</version>
106
			</plugin>
83 107
		</plugins>
84 108
	</build>
85 109

  
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/ConfigurationRepositoryTest.java
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
}
src/test/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/JsonParserTest.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.utils;
2

  
3
import static org.junit.jupiter.api.Assertions.*;
4

  
5
import com.fasterxml.jackson.databind.JsonNode;
6
import com.fasterxml.jackson.databind.ObjectMapper;
7
import com.fasterxml.jackson.databind.ObjectWriter;
8
import org.junit.jupiter.api.Test;
9

  
10
import java.io.IOException;
11

  
12
public class JsonParserTest {
13

  
14
    @Test
15
    void testGetDefaultObjectMapper(){
16
        ObjectMapper objectMapper = JsonParser.getDefaultObjectMapper();
17
        assertEquals(objectMapper == null,false, "ObjectMapper was not initialized.");
18
    }
19

  
20
    @Test
21
    void testParse(){
22
        String jsonTest = "{\"test\":{\"threshold\":\"value\"}}";
23
        JsonNode nodeTest;
24
        try {
25
            nodeTest = JsonParser.parse(jsonTest);
26
            String result = nodeTest.get("test").get("threshold").asText();
27
            assertEquals(result, "value", "Json string was not parsed correctly.");
28
        } catch (IOException e) {
29
            e.printStackTrace();
30
        }
31
    }
32

  
33
    @Test
34
    void testGetObjectWriter(){
35
        ObjectWriter objectWriter = JsonParser.getObjectWriter();
36
        assertEquals(objectWriter == null, false, "ObjectWriter was not initialized.");
37
    }
38

  
39
    @Test
40
    void testGetObjectMapper(){
41
        JsonParser.getDefaultObjectMapper();
42
        ObjectMapper objectMapper = JsonParser.getObjectMapper();
43
        assertEquals(objectMapper == null, false, "Object mapper was not returned correctly.");
44
    }
45
}

Také k dispozici: Unified diff