|
| 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