Skip to content

Commit 297e18c

Browse files
committed
Added deserialization tests for the POJOs
1 parent f16c335 commit 297e18c

File tree

9 files changed

+220
-2
lines changed

9 files changed

+220
-2
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package es.upv.i3m.grycap.im.pojo;
2+
3+
import com.fasterxml.jackson.core.JsonParseException;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
import es.upv.i3m.grycap.ImTestWatcher;
8+
import es.upv.i3m.grycap.file.Utf8File;
9+
import es.upv.i3m.grycap.im.exceptions.FileException;
10+
import es.upv.i3m.grycap.im.exceptions.InfrastructureUuidNotFoundException;
11+
12+
import org.junit.Assert;
13+
import org.junit.Test;
14+
15+
import java.io.IOException;
16+
import java.nio.file.Paths;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
public class PojoDeserializationTest extends ImTestWatcher {
21+
22+
private static final String JSON_FILES_FOLDER = "./src/test/resources/json/";
23+
24+
@Test
25+
@SuppressWarnings("unchecked")
26+
public void testInfOuputValuesDeserialization() throws JsonParseException,
27+
JsonMappingException, IOException, FileException {
28+
String jsonString = getFileContentAsString("infrastructure-ouputs.json");
29+
InfOutputValues result =
30+
new ObjectMapper().readValue(jsonString, InfOutputValues.class);
31+
Map<String, Object> outputs = result.getOutputs();
32+
33+
String serverUrl = (String) outputs.get("galaxy_url");
34+
Assert.assertEquals("http://127.0.0.1:8080", serverUrl);
35+
36+
Map<String, Object> credentials =
37+
(Map<String, Object>) outputs.get("cluster_creds");
38+
String user = (String) credentials.get("user");
39+
String token = (String) credentials.get("token");
40+
Assert.assertEquals("username", user);
41+
Assert.assertEquals("password", token);
42+
}
43+
44+
@Test
45+
public void testInfrastructureStateDeserialization()
46+
throws JsonParseException, JsonMappingException, IOException,
47+
FileException {
48+
String jsonString = getFileContentAsString("infrastructure-state.json");
49+
InfrastructureState infState =
50+
new ObjectMapper().readValue(jsonString, InfrastructureState.class);
51+
52+
Assert.assertEquals("configured", infState.getState());
53+
String vm0State = infState.getVmStates().get("0");
54+
Assert.assertEquals("configured", vm0State);
55+
}
56+
57+
@Test
58+
public void testInfrastructureUriDeserialization()
59+
throws JsonParseException, JsonMappingException, IOException,
60+
FileException, InfrastructureUuidNotFoundException {
61+
String jsonString = getFileContentAsString("infrastructure-uri.json");
62+
InfrastructureUri infUri =
63+
new ObjectMapper().readValue(jsonString, InfrastructureUri.class);
64+
65+
Assert.assertEquals(
66+
"http://127.0.0.1:8800/infrastructures/02a04d9e-0d36-11e6-a466-300000000002",
67+
infUri.getUri());
68+
Assert.assertEquals("02a04d9e-0d36-11e6-a466-300000000002",
69+
infUri.getInfrastructureId());
70+
}
71+
72+
@Test
73+
public void testInfrastructureUrisDeserialization()
74+
throws JsonParseException, JsonMappingException, IOException,
75+
FileException, InfrastructureUuidNotFoundException {
76+
String jsonString = getFileContentAsString("infrastructure-uris.json");
77+
InfrastructureUris infUris =
78+
new ObjectMapper().readValue(jsonString, InfrastructureUris.class);
79+
80+
Assert.assertEquals(
81+
new InfrastructureUri(
82+
"http://127.0.0.1:8800/infrastructures/02a04d9e-0d36-11e6-a466-300000000002"),
83+
infUris.getUris().get(0));
84+
Assert.assertEquals(
85+
new InfrastructureUri(
86+
"http://127.0.0.1:8800/infrastructures/0f915ff0-f023-11e5-a466-300000000002"),
87+
infUris.getUris().get(1));
88+
}
89+
90+
@Test
91+
public void testPropertyDeserialization() throws JsonParseException,
92+
JsonMappingException, IOException, FileException {
93+
String jsonString = getFileContentAsString("property.json");
94+
Property property =
95+
new ObjectMapper().readValue(jsonString, Property.class);
96+
Assert.assertEquals("key", property.getKey());
97+
Assert.assertEquals("value", property.getValue());
98+
}
99+
100+
@Test
101+
public void testResponseErrorDeserialization() throws JsonParseException,
102+
JsonMappingException, IOException, FileException {
103+
String jsonString = getFileContentAsString("response-error.json");
104+
ResponseError responseError =
105+
new ObjectMapper().readValue(jsonString, ResponseError.class);
106+
107+
Assert.assertEquals("Not found: '/infrastructures/'",
108+
responseError.getMessage());
109+
Assert.assertEquals((Integer) 404, responseError.getCode());
110+
111+
String expectedFormattedMessage =
112+
"Error 404: Not found: '/infrastructures/'";
113+
Assert.assertEquals(expectedFormattedMessage,
114+
responseError.getFormattedErrorMessage());
115+
}
116+
117+
@Test
118+
public void testVirtualMachineInfoDeserialization() throws JsonParseException,
119+
JsonMappingException, IOException, FileException {
120+
String jsonString = getFileContentAsString("virtual-machine-info.json");
121+
VirtualMachineInfo virtualMachineInfo =
122+
new ObjectMapper().readValue(jsonString, VirtualMachineInfo.class);
123+
124+
List<Map<String, Object>> vmProperties =
125+
virtualMachineInfo.getVmProperties();
126+
127+
// Check the value of the first map
128+
Map<?, ?> netInfo = (Map<String, Object>) vmProperties.get(0);
129+
Assert.assertEquals("publica", netInfo.get("provider_id"));
130+
Assert.assertEquals("yes", netInfo.get("outbound"));
131+
132+
// Check an internal map inside the map
133+
Map<?, ?> machineInfo = (Map<String, Object>) vmProperties.get(2);
134+
Assert.assertEquals("one://ramses.i3m.upv.es/95",
135+
machineInfo.get("disk.0.image.url"));
136+
}
137+
138+
private String getFileContentAsString(String fileName) throws FileException {
139+
return new Utf8File(Paths.get(JSON_FILES_FOLDER + fileName)).read();
140+
}
141+
}

src/test/java/es/upv/i3m/grycap/im/pojo/PojoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package es.upv.i3m.grycap.im.pojo;
22

3+
import es.upv.i3m.grycap.ImTestWatcher;
34
import es.upv.i3m.grycap.im.exceptions.FileException;
45
import es.upv.i3m.grycap.im.exceptions.InfrastructureUuidNotFoundException;
56

@@ -10,7 +11,7 @@
1011
import java.util.HashMap;
1112
import java.util.Map;
1213

13-
public class PojoTest {
14+
public class PojoTest extends ImTestWatcher {
1415

1516
@Test
1617
public void testInfOutputValuesEquals() {
@@ -179,5 +180,4 @@ public void testVirtualMachineInfoNotEquals() throws FileException {
179180

180181
Assert.assertNotEquals(vmInfo1, vmInfo2);
181182
}
182-
183183
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"outputs": {
3+
"galaxy_url": "http://127.0.0.1:8080",
4+
"cluster_creds": {
5+
"token": "password",
6+
"user": "username"
7+
}
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"state": {
3+
"vm_states": {
4+
"0": "configured"
5+
},
6+
"state": "configured"
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"uri": "http://127.0.0.1:8800/infrastructures/02a04d9e-0d36-11e6-a466-300000000002"
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"uri-list": [
3+
{
4+
"uri": "http://127.0.0.1:8800/infrastructures/02a04d9e-0d36-11e6-a466-300000000002"
5+
},
6+
{
7+
"uri": "http://127.0.0.1:8800/infrastructures/0f915ff0-f023-11e5-a466-300000000002"
8+
}
9+
]
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"key": "value"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "Not found: '/infrastructures/'",
3+
"code": 404
4+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"radl": [
3+
{
4+
"provider_id": "publica",
5+
"outbound": "yes",
6+
"class": "network",
7+
"id": "public_net"
8+
},
9+
{
10+
"provider_id": "privada",
11+
"outbound": "no",
12+
"class": "network",
13+
"id": "private_net"
14+
},
15+
{
16+
"net_interface.1.ip": "10.0.0.72",
17+
"cpu.arch": "x86_64",
18+
"disk.0.os.credentials.username": "ubuntu",
19+
"virtual_system_type": "qemu",
20+
"net_interface.1.connection": "private_net",
21+
"disk.0.image.name": "Ramses_Ubuntu_14_04_demo",
22+
"memory.size": 1000341504,
23+
"provider.type": "OpenNebula",
24+
"net_interface.0.connection": "public_net",
25+
"disk.0.device": "hda",
26+
"disk.0.os.name": "linux",
27+
"class": "system",
28+
"instance_id": "10030",
29+
"disk.0.image.url": "one://ramses.i3m.upv.es/95",
30+
"cpu.count": 1,
31+
"disk.0.os.version": "14.04",
32+
"launch_time": 1461843667,
33+
"id": "galaxy_server",
34+
"instance_name": "Ramses_Ubuntu_14_04_demo",
35+
"state": "configured",
36+
"provider.port": 2633,
37+
"disk.0.size": 20971520000
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)