Skip to content

Commit 1953d4c

Browse files
committed
Merge pull request #24 from indigo-dc/devel
Added new unitary tests
2 parents 7899221 + cd6d309 commit 1953d4c

File tree

13 files changed

+464
-12
lines changed

13 files changed

+464
-12
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>es.upv.i3m.grycap</groupId>
55
<artifactId>im-java-api</artifactId>
6-
<version>0.4.2</version>
6+
<version>0.4.3</version>
77
<name>IM Java API</name>
88
<description>Java client for the REST API of the IM</description>
99

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (C) GRyCAP - I3M - UPV
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* <p>http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* <p>Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package es.upv.i3m.grycap.im.exceptions;
18+
19+
public class InfrastructureUuidNotFoundException extends ImClientException {
20+
21+
private static final long serialVersionUID = -8699931731538406550L;
22+
23+
}

src/main/java/es/upv/i3m/grycap/im/pojo/InfrastructureUri.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.annotation.JsonProperty;
55

6+
import es.upv.i3m.grycap.im.exceptions.InfrastructureUuidNotFoundException;
7+
68
import org.apache.commons.lang3.builder.EqualsBuilder;
79
import org.apache.commons.lang3.builder.HashCodeBuilder;
810
import org.apache.commons.lang3.builder.ToStringBuilder;
@@ -15,8 +17,8 @@ public class InfrastructureUri {
1517

1618
private final String uri;
1719
private String infrastructureId;
18-
private static final String URI_PATTERN = "([^/]+)";
19-
private static final int GROUP_WITH_ID_INFO = 4;
20+
private static final String UUID_PATTERN =
21+
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
2022

2123
public InfrastructureUri(@JsonProperty("uri") String uri) {
2224
this.uri = uri;
@@ -29,23 +31,22 @@ public String getUri() {
2931
/**
3032
* Returns the infrastructure Id extracted from the infrastructure uri.
3133
*/
32-
public String getInfrastructureId() {
34+
public String getInfrastructureId()
35+
throws InfrastructureUuidNotFoundException {
3336
if (infrastructureId == null) {
3437
infrastructureId = extractInfrastructureId();
3538
}
3639
return infrastructureId;
3740
}
3841

39-
private String extractInfrastructureId() {
40-
Pattern ptrn = Pattern.compile(URI_PATTERN);
42+
private String extractInfrastructureId()
43+
throws InfrastructureUuidNotFoundException {
44+
Pattern ptrn = Pattern.compile(UUID_PATTERN);
4145
Matcher matcher = ptrn.matcher(uri);
42-
// The fourth match has the id info
43-
// Not using reverse search because the string can have more content
44-
// afterwards
45-
for (int i = 0; i < GROUP_WITH_ID_INFO; i++) {
46-
matcher.find();
46+
if (matcher.find()) {
47+
return matcher.group(0);
4748
}
48-
return matcher.group(0);
49+
throw new InfrastructureUuidNotFoundException();
4950
}
5051

5152
@Override

src/main/java/es/upv/i3m/grycap/im/pojo/Property.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
import es.upv.i3m.grycap.im.pojo.deserializer.PropertyDeserializer;
2222

23+
import org.apache.commons.lang3.builder.EqualsBuilder;
24+
import org.apache.commons.lang3.builder.HashCodeBuilder;
25+
import org.apache.commons.lang3.builder.ToStringBuilder;
26+
2327
/**
2428
* Generic property class that stores all the different properties that can have
2529
* the infrastructure or the virtual machines.
@@ -43,4 +47,27 @@ public String getValue() {
4347
return value;
4448
}
4549

50+
@Override
51+
public String toString() {
52+
return ToStringBuilder.reflectionToString(this);
53+
}
54+
55+
@Override
56+
public int hashCode() {
57+
return new HashCodeBuilder().append(key).append(value).toHashCode();
58+
}
59+
60+
@Override
61+
public boolean equals(Object other) {
62+
if (other == this) {
63+
return true;
64+
}
65+
if (other instanceof Property) {
66+
Property prop = (Property) other;
67+
return new EqualsBuilder().append(key, prop.key).append(value, prop.value)
68+
.isEquals();
69+
}
70+
return false;
71+
}
72+
4673
}
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+
}

0 commit comments

Comments
 (0)