Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/unit tests #460

Merged
merged 23 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8e6d775
JUnit update and dependencies addition to allow tests to run through …
payneBrandon Nov 30, 2021
0c873ff
Adding settings.json file for executig tests locally
payneBrandon Nov 30, 2021
1628bf3
Finish migrating to Jackson from Gson
payneBrandon Nov 30, 2021
f0f3701
Unit test updates
payneBrandon Nov 30, 2021
dda0b92
Bug fix with pivotOffset text to numeric
payneBrandon Nov 30, 2021
2af3812
Removing invalid test. We now specify localhost if missing docker host
payneBrandon Dec 1, 2021
0c9853d
JSON property order update
payneBrandon Dec 1, 2021
8a27638
Setting private property visibility on object mapper
payneBrandon Dec 1, 2021
eaeb021
Metadata JSON property orderings
payneBrandon Dec 1, 2021
a0ee99e
JSON property order update
payneBrandon Dec 1, 2021
215bd4c
JsonProperty additions to alleviate naming convention
payneBrandon Dec 1, 2021
2047917
JsonProperty updates
payneBrandon Dec 1, 2021
0a76808
Property name updates for JSON
payneBrandon Dec 1, 2021
aec8652
Adding empty string enum handling
payneBrandon Dec 1, 2021
95d0359
Error adjust for Jackson
payneBrandon Dec 1, 2021
d5b23de
Adding proper RSU dummyy object to test with
payneBrandon Dec 1, 2021
3359adf
Fixing expected times for submit call after updates
payneBrandon Dec 6, 2021
9d8b616
JsonProperty updates
payneBrandon Dec 6, 2021
05fd533
Adding default ttl value, as it is now set.
payneBrandon Dec 6, 2021
eb25828
Removing dead code, unit test updates
payneBrandon Dec 7, 2021
7a4a5b1
Static mocking updatge
payneBrandon Dec 7, 2021
861282d
Testing authentication protocal addition
payneBrandon Dec 8, 2021
5152714
Merge pull request #6 from CDOT-CV/bug/snmp
payneBrandon Dec 8, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we need this update. It does the same thing as the existing code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This swaps over to using the Jackson library. There was an odd mixture using both the Gson and Jackson libraries, and they did not always mix. When I got the unit tests to run, large numbers of them were failing and many were due to this mismatch of serializations. You are correct that the new code performs the same as previous in this instance and that was intentional - this update ensures compatibility and chooses a single serializer (Jackson) over the current duality.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

} 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