Skip to content

Commit

Permalink
Merge pull request #460 from CDOT-CV/bug/unit-tests
Browse files Browse the repository at this point in the history
Bug/unit tests
  • Loading branch information
dan-du-car authored Dec 14, 2021
2 parents f1231e1 + 5152714 commit dd666ab
Show file tree
Hide file tree
Showing 35 changed files with 1,156 additions and 1,070 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.test.config": {
"name": "testConfig",
"vmArgs": ["-javaagent:/root/.m2/repository/org/jmockit/jmockit/1.49/jmockit-1.49.jar"]
},
"java.test.defaultConfig": "testConfig"
}
59 changes: 38 additions & 21 deletions jpo-ode-common/src/main/java/us/dot/its/jpo/ode/util/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.fasterxml.jackson.databind.type.LogicalType;

public class JsonUtils {

public static class JsonUtilsException extends Exception {

private static final long serialVersionUID = 1L;
Expand All @@ -45,32 +49,45 @@ public JsonUtilsException(String string, Exception e) {

}

private static Gson gsonCompact;
private static Gson gsonVerbose;
private static ObjectMapper mapper;
private static ObjectMapper mapper_noNulls;
private static Logger logger;

private JsonUtils() {
logger = LoggerFactory.getLogger(JsonUtils.class);
}

static {
gsonCompact = new GsonBuilder().create();
gsonVerbose = new GsonBuilder().serializeNulls().create();
mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.coercionConfigFor(LogicalType.Enum)
.setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsNull);

mapper_noNulls = new ObjectMapper();
mapper_noNulls.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper_noNulls.setSerializationInclusion(Include.NON_NULL);
}

public static String toJson(Object o, boolean verbose) {

// convert java object to JSON format,
// and returned as JSON formatted string
return verbose ? gsonVerbose.toJson(o) : gsonCompact.toJson(o);
try {
return verbose ? mapper.writeValueAsString(o) : mapper_noNulls.writeValueAsString(o);
} catch (JsonProcessingException e) {
e.printStackTrace();
return "";
}
}

public static Object fromJson(String s, Class<?> clazz) {
return gsonCompact.fromJson(s, clazz);
try {
return jacksonFromJson(s, clazz);
} catch (JsonUtilsException e) {
e.printStackTrace();
return null;
}
}

public static Object jacksonFromJson(String s, Class<?> clazz) throws JsonUtilsException {
try {
return mapper.readValue(s, clazz);
Expand All @@ -86,7 +103,7 @@ public static String newJson(String key, Object value) {
public static ObjectNode cloneObjectNode(ObjectNode src) {
return src.deepCopy();
}

public static ObjectNode newObjectNode(String key, Object value) {
ObjectNode json = mapper.createObjectNode();
json.putPOJO(key, value);
Expand Down Expand Up @@ -155,7 +172,7 @@ public static HashMap<String, JsonNode> jsonNodeToHashMap(JsonNode jsonNode) {
}
return nodeProps;
}

/**
* Takes in a key, value pair and returns a valid JSON string such as
* {"error":"message"}
Expand All @@ -167,14 +184,14 @@ public static HashMap<String, JsonNode> jsonNodeToHashMap(JsonNode jsonNode) {
public static String jsonKeyValue(String key, String value) {
return "{\"" + key + "\":\"" + value + "\"}";
}

public static BigDecimal decimalValue(JsonNode v) {
BigDecimal result;
if (v.isTextual()) {
result = new BigDecimal(v.textValue());
} else {
result = v.decimalValue();
}
return result;
BigDecimal result;
if (v.isTextual()) {
result = new BigDecimal(v.textValue());
} else {
result = v.decimalValue();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@
package us.dot.its.jpo.ode.inet;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.junit.Before;
import org.junit.Test;

import mockit.Capturing;
import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
Expand All @@ -41,14 +38,13 @@ public class InetPacketTest {
DatagramPacket mockDatagramPacket;
byte[] mockPayload;

@Mocked InetAddress address;
@Mocked InetAddress mockAddress;

@Before
public void setup() {
new MockUp<InetAddress>() {
@Mock
public InetAddress getByName(String host) {
return address;
@Mock InetAddress getByName(String host) {
return mockAddress;
}
};
}
Expand Down Expand Up @@ -87,7 +83,7 @@ public void testByteConstructor() {
*/

@Test
public void parseBundleNulll() {
public void parseBundleNull() {
InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 });
byte[] bundle = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ public void testToJson() {
@Test
public void testNewJson() {
String j = JsonUtils.newJson("key", "value");
assertEquals("{\"key\":value}", j);
assertEquals("{\"key\":\"value\"}", j);
}

@Test
public void testNewObjectNode() {
ObjectNode j = JsonUtils.newObjectNode("key", "value");
assertEquals("{\"key\":value}", j.toString());
assertEquals("{\"key\":\"value\"}", j.toString());
}

@Test
public void testAddNode() {
ObjectNode j = JsonUtils.newObjectNode("key", "value");
ObjectNode j2 = JsonUtils.addNode(j, "key2", "value2");
assertEquals("{\"key\":value,\"key2\":value2}", j.toString());
assertEquals("{\"key\":value,\"key2\":value2}", j2.toString());
assertEquals("{\"key\":\"value\",\"key2\":\"value2\"}", j.toString());
assertEquals("{\"key\":\"value\",\"key2\":\"value2\"}", j2.toString());
}

@Test
Expand All @@ -157,7 +157,7 @@ public void testToObjectNode() throws JsonUtilsException {
assertEquals(expectedOvdf, ovdf.toString());
JsonUtils.addNode(ovdf, "avgSpeed", "2.22");
assertEquals(
"{\"className\":\"com.bah.ode.model.OdeVehicleDataFlat\",\"serialId\":\"10817812-036b-4d7b-867b-ae0bc62a2b3e.0\",\"receivedAt\":\"2015-07-22T19:21:16.413+0000\",\"groupId\":\"4130008F\",\"accelLong\":0.34,\"accelVert\":0.0,\"accellYaw\":8.42,\"heading\":65.95,\"speed\":8.12,\"sizeLength\":500,\"sizeWidth\":200,\"latitude\":42.3296667,\"longitude\":-83.044539,\"elevation\":156.9,\"tempId\":\"C4290123\",\"year\":2015,\"month\":5,\"day\":13,\"hour\":15,\"minute\":52,\"second\":45.5,\"dateTime\":\"2015-06-13T19:52:45.500+0000\",\"avgSpeed\":2.22}",
"{\"className\":\"com.bah.ode.model.OdeVehicleDataFlat\",\"serialId\":\"10817812-036b-4d7b-867b-ae0bc62a2b3e.0\",\"receivedAt\":\"2015-07-22T19:21:16.413+0000\",\"groupId\":\"4130008F\",\"accelLong\":0.34,\"accelVert\":0.0,\"accellYaw\":8.42,\"heading\":65.95,\"speed\":8.12,\"sizeLength\":500,\"sizeWidth\":200,\"latitude\":42.3296667,\"longitude\":-83.044539,\"elevation\":156.9,\"tempId\":\"C4290123\",\"year\":2015,\"month\":5,\"day\":13,\"hour\":15,\"minute\":52,\"second\":45.5,\"dateTime\":\"2015-06-13T19:52:45.500+0000\",\"avgSpeed\":\"2.22\"}",
ovdf.toString());
}

Expand Down Expand Up @@ -201,7 +201,7 @@ public void testPutObject() {
ObjectNode dm = JsonUtils.newNode();
dm.putObject("metadata");
dm.putObject("payload").setAll(JsonUtils.newObjectNode("key1", "value1"));
assertEquals("{\"metadata\":{},\"payload\":{\"key1\":value1}}", dm.toString());
assertEquals("{\"metadata\":{},\"payload\":{\"key1\":\"value1\"}}", dm.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
******************************************************************************/
package us.dot.its.jpo.ode.dds;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

/**
* "DEPOSIT: { \"systemDepositName\": \"%s\", \"encodeType\": \"%s\", \"encodedMsg\": \"%s\" }"
* "DEPOSIT: { \"systemDepositName\": \"%s\", \"encodeType\": \"%s\",
* \"encodedMsg\": \"%s\" }"
*
*/
@JsonPropertyOrder({ "systemDepositName", "encodeType", "encodedMsg" })
public class DdsDepRequest extends DdsRequest {

private static final long serialVersionUID = 6066887685895268828L;
Expand All @@ -28,7 +31,6 @@ public class DdsDepRequest extends DdsRequest {
private String encodeType;
private String encodedMsg;


public String getSystemDepositName() {
return systemDepositName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,14 @@
import us.dot.its.jpo.ode.plugin.j2735.DdsAdvisorySituationData;

public class OdeAsdPayload extends OdeMsgPayload {

private static final long serialVersionUID = 7061315628111448390L;

public OdeAsdPayload() {
this(new DdsAdvisorySituationData());
}
private static final long serialVersionUID = 7061315628111448390L;

public OdeAsdPayload(DdsAdvisorySituationData asd) {
super(asd);
this.setData(asd);
}

public DdsAdvisorySituationData getAsd() {
return (DdsAdvisorySituationData) getData();
}

public void setAsd(DdsAdvisorySituationData asd) {
setData(asd);
}
public OdeAsdPayload() {
this(new DdsAdvisorySituationData());
}

public OdeAsdPayload(DdsAdvisorySituationData asd) {
super(asd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,48 @@
******************************************************************************/
package us.dot.its.jpo.ode.model;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({ "bsmSource", "logFileName", "recordType", "securityResultCode", "receivedMessageDetails",
"encodings", "payloadType", "serialId", "odeReceivedAt", "schemaVersion", "maxDurationTime", "recordGeneratedAt",
"recordGeneratedBy", "sanitized" })
public class OdeBsmMetadata extends OdeLogMetadata {

private static final long serialVersionUID = -8601265839394150140L;
private static final long serialVersionUID = -8601265839394150140L;

private String originIp;

public enum BsmSource {
EV, RV, unknown
}

private String originIp;
private BsmSource bsmSource;

public enum BsmSource {
EV, RV, unknown
}
public OdeBsmMetadata() {
super();
}

private BsmSource bsmSource;

public OdeBsmMetadata() {
super();
}
public OdeBsmMetadata(OdeMsgPayload payload) {
super(payload);
}

public OdeBsmMetadata(OdeMsgPayload payload) {
super(payload);
}
public OdeBsmMetadata(OdeMsgPayload payload, SerialId serialId, String receivedAt) {

public OdeBsmMetadata(OdeMsgPayload payload, SerialId serialId, String receivedAt) {

}
}

public BsmSource getBsmSource() {
return bsmSource;
}
public BsmSource getBsmSource() {
return bsmSource;
}

public void setBsmSource(BsmSource bsmSource) {
this.bsmSource = bsmSource;
}
public void setBsmSource(BsmSource bsmSource) {
this.bsmSource = bsmSource;
}

public String getOriginIp() {
return originIp;
}
public String getOriginIp() {
return originIp;
}

public void setOriginIp(String originIp) {
this.originIp = originIp;
}
public void setOriginIp(String originIp) {
this.originIp = originIp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
******************************************************************************/
package us.dot.its.jpo.ode.model;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({ "metadata", "payload" })
public class OdeData extends OdeObject implements OdeFilterable {
private static final long serialVersionUID = -7711340868799607662L;

Expand Down
Loading

0 comments on commit dd666ab

Please sign in to comment.