summaryrefslogtreecommitdiff
path: root/java/perftests/src/test/java/org/apache/qpid/disttest/message/JsonHandlerTest.java
blob: 2e0c2e1ecd6450721492f3e70073c5c8b23ae477 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.qpid.disttest.message;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.qpid.disttest.client.property.ListPropertyValue;
import org.apache.qpid.disttest.client.property.PropertyValue;
import org.apache.qpid.disttest.json.JsonHandler;
import org.apache.qpid.test.utils.QpidTestCase;

public class JsonHandlerTest extends QpidTestCase
{
    private JsonHandler _jsonHandler = null;
    private SendChristmasCards _testCommand = null;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();

        _jsonHandler = new JsonHandler();

        _testCommand = new SendChristmasCards(CommandType.START_TEST, Collections.singletonMap(SendChristmasCards.CardType.FUNNY, 5));
        _testCommand.persons = Arrays.asList(new Person("Phil"), new Person("Andrew"));
    }

    public void testMarshallUnmarshall() throws Exception
    {
        final String jsonString = _jsonHandler.marshall(_testCommand);

        final SendChristmasCards unmarshalledCommand = _jsonHandler.unmarshall(jsonString, SendChristmasCards.class);

        assertEquals("Unmarshalled command should be equal to the original object", _testCommand, unmarshalledCommand);
    }

    public void testGeneratorDesrialization()
    {
        String json = "{_messageProperties: {test: 1; generator: {'@def': 'list'; _cyclic: false; _items: ['first', " +
                "{'@def': 'range'; _upper:10; '_type':'int'}]}}}";
        final TestCommand unmarshalledCommand = _jsonHandler.unmarshall(json, TestCommand.class);
        Map<String, PropertyValue> properties = unmarshalledCommand.getMessageProperties();
        assertNotNull("Properties should not be null", properties);
        assertFalse("Properties should not be empty", properties.isEmpty());
        assertEquals("Unexpected properties size", 2, properties.size());
        PropertyValue testProperty = properties.get("test");
        assertNotNull("Unexpected property test", testProperty);
        assertTrue("Unexpected property test", testProperty.getValue() instanceof Number);
        assertEquals("Unexpected property value", 1, ((Number)testProperty.getValue()).intValue());
        Object generatorObject = properties.get("generator");
        assertTrue("Unexpected generator object", generatorObject instanceof ListPropertyValue);
        PropertyValue generator = (PropertyValue)generatorObject;
        assertEquals("Unexpected generator value", "first", generator.getValue());
        for (int i = 0; i < 10; i++)
        {
            assertEquals("Unexpected generator value", new Integer(i), generator.getValue());
        }
        String newJson =_jsonHandler.marshall(unmarshalledCommand);
        final TestCommand newUnmarshalledCommand = _jsonHandler.unmarshall(newJson, TestCommand.class);
        assertEquals("Unmarshalled command should be equal to the original object", unmarshalledCommand, newUnmarshalledCommand);
    }

    /**
     * A {@link Command} designed to exercise {@link JsonHandler}, e.g does it handle a map of enums?.
     *
     * This class is non-private to avoid auto-deletion of "unused" fields/methods
     */
    static class SendChristmasCards extends Command
    {
        enum CardType {FUNNY, TRADITIONAL}

        private Map<CardType, Integer> _cardTypes;
        private List<Person> persons;

        public SendChristmasCards(final CommandType type, Map<CardType, Integer> cardTypes)
        {
            super(type);
            _cardTypes = cardTypes;
        }

        public Map<CardType, Integer> getCardTypes()
        {
            return _cardTypes;
        }

        public List<Person> getPersons()
        {
            return persons;
        }

        @Override
        public boolean equals(final Object obj)
        {
            return EqualsBuilder.reflectionEquals(this, obj);
        }
    }

    /**
     * This class is non-private to avoid auto-deletion of "unused" fields/methods
     */
    static class Person
    {
        private String _firstName;

        public Person(final String firstName)
        {
            _firstName = firstName;
        }

        public String getFirstName()
        {
            return _firstName;
        }

        @Override
        public boolean equals(final Object obj)
        {
            return EqualsBuilder.reflectionEquals(this, obj);
        }

    }

    /**
     * Yet another test class
     */
    static class TestCommand extends Command
    {

        private Map<String, PropertyValue> _messageProperties;

        public TestCommand(CommandType type)
        {
            super(type);
        }

        public Map<String, PropertyValue> getMessageProperties()
        {
            return _messageProperties;
        }

        public void setMessageProperties(Map<String, PropertyValue> _messageProperties)
        {
            this._messageProperties = _messageProperties;
        }

        @Override
        public boolean equals(final Object obj)
        {
            if (obj == null || !(obj instanceof TestCommand))
            {
                return false;
            }
            TestCommand other = (TestCommand)obj;
            if (_messageProperties == null && other._messageProperties != null )
            {
                return false;
            }
            return _messageProperties.equals(other._messageProperties);
        }
    }
}