aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/tests/JsonTests.java
blob: 8378d7f1fd849e31716fc68cc2597a36d25bb476 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.juick.tests;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.Message;
import com.juick.Tag;
import com.juick.User;
import com.juick.json.MessageSerializer;
import com.juick.json.UserSerializer;
import org.json.JSONObject;
import org.junit.Test;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;

public class JsonTests {
    @Test
    public void DateTest() {
        Message msg = new Message();
        SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            msg.setDate(df.parse("2015-01-01 05:02:10"));
            MessageSerializer serializer = new MessageSerializer();
            String json = serializer.serialize(msg).toString();
            assertEquals("{\"timestamp\":\"2015-01-01 05:02:10\"}", json);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void serializersTest() throws IOException {
        User user = new User();
        user.setUName("ugnich");
        user.setUID(1);
        user.setFullName("Anton Ugnich");
        UserSerializer userSerializer = new UserSerializer();
        JSONObject handmadeJson = userSerializer.serialize(user);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        User jacksonUser = mapper.readValue(handmadeJson.toString(), User.class);
        assertEquals("jackson should deserialize ugnich json", user, jacksonUser);
        String jacksonString = mapper.writeValueAsString(user);
        JSONObject jacksonJson = new JSONObject(jacksonString);
        assertEquals("jackson should serialize as ugnich", handmadeJson.length(), jacksonJson.length());
        Message msg = new Message();
        msg.setMID(1);
        msg.setUser(user);
        msg.setText("yo");
        msg.setDate(new Date());
        msg.Tags.add(new Tag("test"));
        msg.Tags.add(new Tag("json"));
        MessageSerializer messageSerializer = new MessageSerializer();
        JSONObject handmadeJsonMessage = messageSerializer.serialize(msg);
        Message jacksonMessage = mapper.readValue(handmadeJsonMessage.toString(), Message.class);
        assertEquals("jackson should deserialize ugnich json", msg, jacksonMessage);
        String jacksonStringMessage = mapper.writeValueAsString(jacksonMessage);
        JSONObject jacksonJsonMessage = new JSONObject(jacksonStringMessage);
        assertEquals("jackson should serialize as ugnich", handmadeJsonMessage.length(), jacksonJsonMessage.length());
    }
}