Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b1499562

Přidáno uživatelem Martin Matas před asi 6 roky(ů)

Added dependency for Mockito, test for GraphFactory

Zobrazit rozdíly:

sources/imiger-dot-converter/pom.xml
94 94
            <version>4.12</version>
95 95
            <scope>test</scope>
96 96
        </dependency>
97

  
98
        <dependency>
99
            <groupId>org.mockito</groupId>
100
            <artifactId>mockito-all</artifactId>
101
            <version>1.10.19</version>
102
            <scope>test</scope>
103
        </dependency>
97 104
    </dependencies>
98 105
</project>
sources/imiger-dot-converter/src/test/java/cz/zcu/kiv/imiger/plugin/dot/GraphFactoryTest.java
1
package cz.zcu.kiv.imiger.plugin.dot;
2

  
3
import cz.zcu.kiv.imiger.plugin.dot.dto.EdgeDTO;
4
import cz.zcu.kiv.imiger.plugin.dot.dto.VertexDTO;
5
import cz.zcu.kiv.imiger.plugin.dot.loader.BaseDOTLoader;
6
import cz.zcu.kiv.imiger.vo.AttributeDataType;
7
import cz.zcu.kiv.imiger.vo.AttributeType;
8
import cz.zcu.kiv.imiger.vo.Graph;
9
import cz.zcu.kiv.imiger.vo.Vertex;
10
import org.junit.Test;
11
import org.junit.runner.RunWith;
12
import org.mockito.Mock;
13
import org.mockito.runners.MockitoJUnitRunner;
14

  
15
import java.util.*;
16

  
17
import static org.hamcrest.CoreMatchers.*;
18
import static org.junit.Assert.*;
19
import static org.mockito.Mockito.verify;
20
import static org.mockito.Mockito.when;
21

  
22
/**
23
 * Tests process of data transformation from DOT file into Graph structure.
24
 *
25
 * Date: 02.04.2019
26
 *
27
 * @author Martin Matas
28
 */
29
@RunWith(MockitoJUnitRunner.class)
30
public class GraphFactoryTest {
31

  
32
	/** Name of attribute type */
33
	private static final String ATTR_TYPE_NAME = "test";
34

  
35
	/** Text of attribute type */
36
	private static final String ATTR_TYPE_TEXT = "test attribute type";
37

  
38
	/** List index / Attribute identifier */
39
	private static final int ATTR_TYPE_ID = 0;
40

  
41
	/** Vertex / Edge attribute value */
42
	private static final String ATTR_VALUE = "test value";
43

  
44
	/** Vertices names */
45
	private static final String VERTEX_A = "A";
46
	private static final String VERTEX_B = "B";
47

  
48
	/** Lists indexes / Vertices identifiers */
49
	private static final int VERTEX_A_ID = 0;
50
	private static final int VERTEX_B_ID = 1;
51

  
52
	/** Edge name */
53
	private static final String EDGE_AB = "A-B";
54

  
55
	/** Edge identifier */
56
	private static final int EDGE_AB_ID = 0;
57

  
58
	/** Archetypes names */
59
	private static final String EDGE_ARCHETYPE = "Edge";
60
	private static final String VERTEX_ARCHETYPE = "Vertex";
61

  
62
	/**
63
	 * Mock object of {@link BaseDOTLoader}.
64
	 */
65
	@Mock
66
	private BaseDOTLoader<VertexDTO, EdgeDTO> loaderMock;
67

  
68
	/**
69
	 * Expects that retrieves correct non-empty data from DOTLoader.
70
	 */
71
	@Test
72
	public void loadDataFromCorrectLoader() {
73
		final int expectedAttributeTypeSize = 1, expectedEdgesSize = 1, expectedVerticesSize = 2;
74

  
75
		initCorrectLoader();
76
		BaseGraphFactory<VertexDTO, EdgeDTO> factory = new GraphFactory(loaderMock);
77

  
78
		// test method logic
79
		verify(loaderMock).getAttributeTypes();
80
		verify(loaderMock).getEdges();
81
		verify(loaderMock).getVertices();
82
		// test attributes initialization
83
		assertThat(factory.attributeTypes.size(), is(expectedAttributeTypeSize));
84
		assertThat(factory.edges.size(), is(expectedEdgesSize));
85
		assertThat(factory.vertices.size(), is(expectedVerticesSize));
86
		assertThat(factory.edgeArchetypes, is(not(nullValue())));
87
		assertThat(factory.vertexArchetypes, is(not(nullValue())));
88
		// test attribute type
89
		assertThat(factory.attributeTypes.get(ATTR_TYPE_ID).getName(), is(equalTo(ATTR_TYPE_NAME)));
90
		assertThat(factory.attributeTypes.get(ATTR_TYPE_ID).getText(), is(equalTo(ATTR_TYPE_TEXT)));
91
		// test vertices
92
		assertThat(factory.vertices.get(VERTEX_A_ID).getName(), is(equalTo(VERTEX_A)));
93
		assertThat(factory.vertices.get(VERTEX_A_ID).getAttributes().get(ATTR_TYPE_NAME), is(equalTo(ATTR_VALUE)));
94
		assertThat(factory.vertices.get(VERTEX_B_ID).getName(), is(equalTo(VERTEX_B)));
95
		assertThat(factory.vertices.get(VERTEX_B_ID).getAttributes().get(ATTR_TYPE_NAME), is(equalTo(ATTR_VALUE)));
96
		// test edges
97
		assertThat(factory.edges.get(EDGE_AB_ID).getName(), is(equalTo(EDGE_AB)));
98
		// test archetypes
99
		assertThat(factory.edgeArchetypes.get(0).getName(), is(equalTo(EDGE_ARCHETYPE)));
100
		assertThat(factory.vertexArchetypes.get(0).getName(), is(equalTo(VERTEX_ARCHETYPE)));
101
	}
102

  
103
	/**
104
	 * Expects that retrieves empty data from DOTLoader. In that case all factory attributes
105
	 * will empty except archetypes that will be created in factory.
106
	 */
107
	@Test
108
	public void loadDataFromEmptyLoader() {
109
		final int expectedAttributeTypeSize = 0, expectedEdgesSize = 0, expectedVerticesSize = 0;
110

  
111
		initEmptyLoader();
112
		BaseGraphFactory<VertexDTO, EdgeDTO> factory = new GraphFactory(loaderMock);
113

  
114
		verify(loaderMock).getAttributeTypes();
115
		verify(loaderMock).getEdges();
116
		verify(loaderMock).getVertices();
117

  
118
		assertThat(factory.attributeTypes.size(), is(expectedAttributeTypeSize));
119
		assertThat(factory.edges.size(), is(expectedEdgesSize));
120
		assertThat(factory.vertices.size(), is(expectedVerticesSize));
121
		assertThat(factory.edgeArchetypes, is(not(nullValue())));
122
		assertThat(factory.vertexArchetypes, is(not(nullValue())));
123
	}
124

  
125
	/**
126
	 * Expects that retrieves uninitialized data from DOTLoader. In that case all factory attributes
127
	 * will be initialized empty except archetypes that will be created in factory.
128
	 */
129
	@Test
130
	public void loadDataFromIncorrectLoader() {
131
		final int expectedAttributeTypeSize = 0, expectedEdgesSize = 0, expectedVerticesSize = 0;
132

  
133
		initNullValueLoader();
134
		BaseGraphFactory<VertexDTO, EdgeDTO> factory = new GraphFactory(loaderMock);
135

  
136
		verify(loaderMock).getAttributeTypes();
137
		verify(loaderMock).getEdges();
138
		verify(loaderMock).getVertices();
139

  
140
		assertThat(factory.attributeTypes.size(), is(expectedAttributeTypeSize));
141
		assertThat(factory.edges.size(), is(expectedEdgesSize));
142
		assertThat(factory.vertices.size(), is(expectedVerticesSize));
143
		assertThat(factory.edgeArchetypes, is(not(nullValue())));
144
		assertThat(factory.vertexArchetypes, is(not(nullValue())));
145
	}
146

  
147
	/**
148
	 * Tests correct creation of the graph instance. Especially vertex's and edge's attributes must be in
149
	 * correct form.
150
	 */
151
	@Test
152
	public void createGraph() {
153
		final int expectedAttributeTypeSize = 1, expectedEdgesSize = 1, expectedVerticesSize = 2;
154

  
155
		// correct loader initialized
156
		initCorrectLoader();
157
		// loaded data from loader
158
		BaseGraphFactory<VertexDTO, EdgeDTO> factory = new GraphFactory(loaderMock);
159

  
160
		// final graph
161
		Graph graph = factory.createGraph();
162

  
163
		assertThat(graph, is(not(nullValue())));
164
		assertThat(graph.getAttributeTypes().size(), is(expectedAttributeTypeSize));
165
		assertThat(graph.getEdges().size(), is(expectedEdgesSize));
166
		assertThat(graph.getVertices().size(), is(expectedVerticesSize));
167
		assertThat(graph.getEdgeArchetypes(), is(not(nullValue())));
168
		assertThat(graph.getVertexArchetypes(), is(not(nullValue())));
169
		// test attribute type
170
		assertThat(graph.getAttributeTypes().get(ATTR_TYPE_ID).getName(), is(equalTo(ATTR_TYPE_NAME)));
171
		assertThat(graph.getAttributeTypes().get(ATTR_TYPE_ID).getText(), is(equalTo(ATTR_TYPE_TEXT)));
172
		// test vertices
173
		Iterator<Vertex> iterator = graph.getVertices().iterator();
174
		Vertex a = iterator.next();
175
		Vertex b = iterator.next();
176
		assertThat(a.getName(), is(equalTo(VERTEX_A)));
177
		assertThat(a.getAttributes().get(0)[0], is(equalTo(ATTR_TYPE_NAME)));
178
		assertThat(a.getAttributes().get(0)[1], is(equalTo(ATTR_VALUE)));
179
		assertThat(b.getName(), is(equalTo(VERTEX_B)));
180
		assertThat(b.getAttributes().get(0)[0], is(equalTo(ATTR_TYPE_NAME)));
181
		assertThat(b.getAttributes().get(0)[1], is(equalTo(ATTR_VALUE)));
182
		// test edges
183
		assertThat(graph.getEdges().get(EDGE_AB_ID).getText(), is(equalTo(EDGE_AB)));
184
		assertThat(graph.getEdges().get(EDGE_AB_ID).getSubedgeInfo().get(0).getAttributes().get(0)[0], is(equalTo(ATTR_TYPE_NAME)));
185
		assertThat(graph.getEdges().get(EDGE_AB_ID).getSubedgeInfo().get(0).getAttributes().get(0)[1], is(equalTo(ATTR_VALUE)));
186
		// test archetypes
187
		assertThat(graph.getEdgeArchetypes().get(0).getName(), is(equalTo(EDGE_ARCHETYPE)));
188
		assertThat(graph.getVertexArchetypes().get(0).getName(), is(equalTo(VERTEX_ARCHETYPE)));
189
	}
190

  
191
	/**
192
	 * Initializes loader with correct data.
193
	 */
194
	private void initCorrectLoader() {
195
		HashMap<String, String> map = new HashMap<>();
196
		map.put(ATTR_TYPE_NAME, ATTR_VALUE);
197

  
198
		Set<AttributeType> attributeTypes = new HashSet<>();
199
		attributeTypes.add(new AttributeType(ATTR_TYPE_NAME, AttributeDataType.STRING, ATTR_TYPE_TEXT));
200

  
201
		List<VertexDTO> vertices = new ArrayList<>();
202
		vertices.add(new VertexDTO(VERTEX_A, VERTEX_A_ID, map));
203
		vertices.add(new VertexDTO(VERTEX_B, VERTEX_B_ID, map));
204

  
205
		List<EdgeDTO> edges = new ArrayList<>();
206
		edges.add(new EdgeDTO(EDGE_AB, VERTEX_A_ID, VERTEX_B_ID, EDGE_AB_ID, map));
207

  
208
		when(loaderMock.getAttributeTypes()).thenReturn(attributeTypes);
209
		when(loaderMock.getVertices()).thenReturn(vertices);
210
		when(loaderMock.getEdges()).thenReturn(edges);
211
	}
212

  
213
	/**
214
	 * Initializes loader for returning empty data.
215
	 */
216
	private void initEmptyLoader() {
217
		Set<AttributeType> attributeTypes = new HashSet<>();
218
		List<VertexDTO> vertices = new ArrayList<>();
219
		List<EdgeDTO> edges = new ArrayList<>();
220

  
221
		when(loaderMock.getAttributeTypes()).thenReturn(attributeTypes);
222
		when(loaderMock.getVertices()).thenReturn(vertices);
223
		when(loaderMock.getEdges()).thenReturn(edges);
224
	}
225

  
226
	/**
227
	 * Initializes loader for returning uninitialized data.
228
	 */
229
	private void initNullValueLoader() {
230
		when(loaderMock.getAttributeTypes()).thenReturn(null);
231
		when(loaderMock.getVertices()).thenReturn(null);
232
		when(loaderMock.getEdges()).thenReturn(null);
233
	}
234

  
235
}

Také k dispozici: Unified diff