Skip to content

Commit

Permalink
For #753 - fixed deserialization of iconType, throw error in case of …
Browse files Browse the repository at this point in the history
…deserialization issues, added test
  • Loading branch information
vitalidze committed Jul 4, 2016
1 parent 1a24145 commit 0102a8b
Showing 6 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2016 Vitaly Litvak ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.web.server.model;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.traccar.web.shared.model.DeviceIconType;

import java.io.IOException;

public class DeviceIconTypeDeserializer extends JsonDeserializer<DeviceIconType> {
@Override
public DeviceIconType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode tree = p.readValueAsTree();
if (tree.isObject()) {
return DeviceIconType.valueOf(tree.get("type").asText());
} else {
return DeviceIconType.valueOf(tree.asText());
}
}
}
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ public void serialize(DeviceIconType deviceIcon,
JsonGenerator json,
SerializerProvider serializerProvider) throws IOException {
json.writeStartObject();
json.writeStringField("type", deviceIcon.name());
for (Position.Status status : Position.Status.values()) {
json.writeObjectFieldStart(status.name());

Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ static ObjectMapper create() {

SimpleModule module = new SimpleModule();
module.addSerializer(DeviceIconType.class, new DeviceIconTypeSerializer());
module.addDeserializer(DeviceIconType.class, new DeviceIconTypeDeserializer());
mapper.registerModule(module);
return mapper;
}
Original file line number Diff line number Diff line change
@@ -148,7 +148,10 @@ private void makeRestCall(ObjectMapper gson, HttpServletResponse response, Strin
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (Exception ex) {}
} catch (Exception ex) {
log("Merhod '" + methodName + "' failed with unexpected error", ex);
log("Method '" + methodName + "' failed with unexpected error", ex);
try {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (Exception e) {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2014 Vitaly Litvak ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.web.server.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.DeviceIconType;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class DeviceIconTypeDeSerializerTest {
ObjectMapper jackson = JacksonUtils.create();

@Test
public void testDefaultIconSerializeDeserialize() throws IOException {
Device device = new Device();
device.setIconType(DeviceIconType.DEFAULT);
String json = jackson.writeValueAsString(device);

device = jackson.readValue(json, Device.class);

assertEquals(DeviceIconType.DEFAULT, device.getIconType());
}

@Test
public void testDefaultIconFromTypeString() throws IOException {
DeviceIconType type = jackson.readValue("{\"type\": \"DEFAULT\"}", DeviceIconType.class);
assertEquals(DeviceIconType.DEFAULT, type);
}

@Test
public void testDefaultIconFromString() throws IOException {
DeviceIconType type = jackson.readValue("\"DEFAULT\"", DeviceIconType.class);
assertEquals(DeviceIconType.DEFAULT, type);
}
}
Original file line number Diff line number Diff line change
@@ -54,5 +54,7 @@ private void testDefaultPositionIconType(JsonNode jsonDeviceIconType, Position.S
Iterator<JsonNode> urls = jsonPositionIconType.get("urls").elements();
assertEquals(positionIconType.getURL(false), urls.next().asText());
assertEquals(positionIconType.getURL(true), urls.next().asText());

assertEquals(DeviceIconType.DEFAULT.name(), jsonDeviceIconType.get("type").asText());
}
}

0 comments on commit 0102a8b

Please sign in to comment.