-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #753 - fixed deserialization of iconType, throw error in case of …
…deserialization issues, added test
- Loading branch information
Showing
6 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/org/traccar/web/server/model/DeviceIconTypeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/org/traccar/web/server/model/DeviceIconTypeDeSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters