aboutsummaryrefslogtreecommitdiffstats
path: root/gson/src/test/java/org/onap/policy/common/gson/internal/AdapterTest.java
blob: 9d80c8600afb9f838e47b27ecde2f3ff1b978a9b (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.gson.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.gson.JacksonExclusionStrategy;
import org.onap.policy.common.gson.annotation.GsonJsonProperty;
import org.onap.policy.common.gson.internal.Adapter.Factory;
import org.onap.policy.common.gson.internal.DataAdapterFactory.Data;
import org.onap.policy.common.gson.internal.DataAdapterFactory.DerivedData;
import org.powermock.reflect.Whitebox;

public class AdapterTest {
    private static final String GET_INVALID_NAME = "get$InvalidName";
    private static final String SET_INVALID_NAME = "set$InvalidName";
    private static final String EMPTY_ALIAS = "emptyAlias";
    private static final String GET_VALUE = ".getValue";
    private static final String GET_VALUE_NAME = "getValue";
    private static final String SET_VALUE_NAME = "setValue";
    private static final String VALUE_NAME = "value";
    private static final String MY_NAME = AdapterTest.class.getName();
    private static final String FACTORY_FIELD = "factory";

    private static DataAdapterFactory dataAdapter = new DataAdapterFactory();

    private static Gson gson = new GsonBuilder().registerTypeAdapterFactory(dataAdapter)
                    .setExclusionStrategies(new JacksonExclusionStrategy()).create();

    private static Factory saveFactory;

    /*
     * The remaining fields are just used within the tests.
     */

    private String value;

    // empty alias - should use field name
    @GsonJsonProperty("")
    protected String emptyAlias;

    @GsonJsonProperty("name-with-alias")
    protected String nameWithAlias;

    protected String unaliased;

    private List<Data> listField;

    private Data dataField;

    @BeforeClass
    public static void setUpBeforeClass() {
        saveFactory = Whitebox.getInternalState(Adapter.class, FACTORY_FIELD);
    }

    @After
    public void tearDown() {
        Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, saveFactory);
    }

    @Test
    public void testIsManagedField() {
        assertTrue(Adapter.isManaged(field(VALUE_NAME)));

        // return an invalid field name
        Factory factory = mock(Factory.class);
        when(factory.getName(any(Field.class))).thenReturn("$invalidFieldName");
        Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);
        assertFalse(Adapter.isManaged(field(VALUE_NAME)));
    }

    @Test
    public void testIsManagedMethod() {
        assertTrue(Adapter.isManaged(mget(GET_VALUE_NAME)));

        // return an invalid method name
        Factory factory = mock(Factory.class);
        Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);

        when(factory.getName(any(Method.class))).thenReturn(GET_INVALID_NAME);
        assertFalse(Adapter.isManaged(mget(GET_VALUE_NAME)));

        when(factory.getName(any(Method.class))).thenReturn(SET_INVALID_NAME);
        assertFalse(Adapter.isManaged(mset(SET_VALUE_NAME)));
    }

    @Test
    public void testAdapterField_Converter() {
        Adapter adapter = new Adapter(gson, field("dataField"));

        // first, write something of type Data
        dataAdapter.reset();
        dataField = new Data(300);
        JsonElement tree = adapter.toJsonTree(dataField);
        assertEquals("{'id':300}".replace('\'', '"'), tree.toString());

        // now try a subclass
        dataAdapter.reset();
        dataField = new DerivedData(300, "three");
        tree = adapter.toJsonTree(dataField);
        assertEquals("{'id':300,'text':'three'}".replace('\'', '"'), tree.toString());
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testAdapterField_Converter_List() {
        listField = DataAdapterFactory.makeList();

        Adapter adapter = new Adapter(gson, field("listField"));

        dataAdapter.reset();
        JsonElement tree = adapter.toJsonTree(listField);
        assertTrue(dataAdapter.isDataWritten());
        assertEquals(DataAdapterFactory.ENCODED_LIST, tree.toString());

        // encode it twice so it uses the cached converter
        dataAdapter.reset();
        tree = adapter.toJsonTree(listField);
        assertTrue(dataAdapter.isDataWritten());
        assertEquals(DataAdapterFactory.ENCODED_LIST, tree.toString());

        dataAdapter.reset();
        List<Data> lst2 = (List<Data>) adapter.fromJsonTree(tree);
        assertTrue(dataAdapter.isDataRead());

        assertEquals(listField.toString(), lst2.toString());

        // decode it twice so it uses the cached converter
        dataAdapter.reset();
        lst2 = (List<Data>) adapter.fromJsonTree(tree);
        assertTrue(dataAdapter.isDataRead());

        assertEquals(listField.toString(), lst2.toString());
    }

    @Test
    public void testAdapterMethod_Converter() throws Exception {
        listField = DataAdapterFactory.makeList();

        Method getter = mget("getMyList");

        Adapter aget = new Adapter(gson, getter, getter.getReturnType());

        dataAdapter.reset();
        JsonElement tree = aget.toJsonTree(listField);
        assertTrue(dataAdapter.isDataWritten());
        assertEquals(DataAdapterFactory.ENCODED_LIST, tree.toString());

        Method setter = AdapterTest.class.getDeclaredMethod("setMyList", List.class);
        Adapter aset = new Adapter(gson, setter, setter.getGenericParameterTypes()[0]);

        dataAdapter.reset();
        @SuppressWarnings("unchecked")
        List<Data> lst2 = (List<Data>) aset.fromJsonTree(tree);
        assertTrue(dataAdapter.isDataRead());

        assertEquals(listField.toString(), lst2.toString());
    }

    @Test
    public void testGetPropName_testGetFullName_testMakeError() {
        // test field
        Adapter adapter = new Adapter(gson, field(VALUE_NAME));

        assertEquals(VALUE_NAME, adapter.getPropName());
        assertEquals(MY_NAME + ".value", adapter.getFullName());


        // test getter
        adapter = new Adapter(gson, mget(GET_VALUE_NAME), String.class);

        assertEquals(VALUE_NAME, adapter.getPropName());
        assertEquals(MY_NAME + GET_VALUE, adapter.getFullName());

        assertEquals("hello: " + MY_NAME + GET_VALUE, adapter.makeError("hello: "));


        // test setter
        adapter = new Adapter(gson, mset(SET_VALUE_NAME), String.class);

        assertEquals(VALUE_NAME, adapter.getPropName());
        assertEquals(MY_NAME + ".setValue", adapter.getFullName());
    }

    @Test
    public void testToJsonTree() {
        Adapter adapter = new Adapter(gson, field(VALUE_NAME));

        JsonElement tree = adapter.toJsonTree("hello");
        assertTrue(tree.isJsonPrimitive());
        assertEquals("hello", tree.getAsString());
    }

    @Test
    public void testFromJsonTree() {
        Adapter adapter = new Adapter(gson, field(VALUE_NAME));

        assertEquals("world", adapter.fromJsonTree(new JsonPrimitive("world")));
    }

    @Test
    public void testDetmPropName() {
        assertEquals(EMPTY_ALIAS, Adapter.detmPropName(field(EMPTY_ALIAS)));
        assertEquals("name-with-alias", Adapter.detmPropName(field("nameWithAlias")));
        assertEquals("unaliased", Adapter.detmPropName(field("unaliased")));

        // return an invalid field name
        Factory factory = mock(Factory.class);
        when(factory.getName(any(Field.class))).thenReturn("$invalidFieldName");
        Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);
        assertEquals(null, Adapter.detmPropName(field(VALUE_NAME)));
    }

    @Test
    public void testDetmGetterPropName() {
        assertEquals(EMPTY_ALIAS, Adapter.detmGetterPropName(mget("getEmptyAlias")));
        assertEquals("get-with-alias", Adapter.detmGetterPropName(mget("getWithAlias")));
        assertEquals("plain", Adapter.detmGetterPropName(mget("getPlain")));
        assertEquals("primBool", Adapter.detmGetterPropName(mget("isPrimBool")));
        assertEquals("boxedBool", Adapter.detmGetterPropName(mget("isBoxedBool")));
        assertEquals(null, Adapter.detmGetterPropName(mget("isString")));
        assertEquals(null, Adapter.detmGetterPropName(mget("noGet")));
        assertEquals(null, Adapter.detmGetterPropName(mget("get")));

        // return an invalid method name
        Factory factory = mock(Factory.class);
        Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);

        when(factory.getName(any(Method.class))).thenReturn(GET_INVALID_NAME);
        assertEquals(null, Adapter.detmGetterPropName(mget(GET_VALUE_NAME)));
    }

    @Test
    public void testDetmSetterPropName() {
        assertEquals(EMPTY_ALIAS, Adapter.detmSetterPropName(mset("setEmptyAlias")));
        assertEquals("set-with-alias", Adapter.detmSetterPropName(mset("setWithAlias")));
        assertEquals("plain", Adapter.detmSetterPropName(mset("setPlain")));
        assertEquals(null, Adapter.detmSetterPropName(mset("noSet")));
        assertEquals(null, Adapter.detmSetterPropName(mset("set")));

        // return an invalid method name
        Factory factory = mock(Factory.class);
        Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);

        when(factory.getName(any(Method.class))).thenReturn(SET_INVALID_NAME);
        assertEquals(null, Adapter.detmSetterPropName(mset(SET_VALUE_NAME)));
    }

    @Test
    public void testGetQualifiedNameField() throws Exception {
        assertEquals(MY_NAME + ".value", Adapter.getQualifiedName(AdapterTest.class.getDeclaredField(VALUE_NAME)));
    }

    @Test
    public void testGetQualifiedNameMethod() {
        assertEquals(MY_NAME + GET_VALUE, Adapter.getQualifiedName(mget(GET_VALUE_NAME)));
    }

    /**
     * Gets a field from this class, by name.
     *
     * @param name name of the field to get
     * @return the field
     */
    private Field field(String name) {
        try {
            return AdapterTest.class.getDeclaredField(name);

        } catch (SecurityException | NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Gets a "getter" method from this class, by name.
     *
     * @param name name of the method to get
     * @return the method
     */
    private Method mget(String name) {
        try {
            return AdapterTest.class.getDeclaredMethod(name);

        } catch (NoSuchMethodException | SecurityException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Gets a "setter" method from this class, by name.
     *
     * @param name name of the method to get
     * @return the method
     */
    private Method mset(String name) {
        try {
            return AdapterTest.class.getDeclaredMethod(name, String.class);

        } catch (NoSuchMethodException | SecurityException e) {
            throw new RuntimeException(e);
        }
    }

    /*
     * The remaining methods are just used within the tests.
     */

    protected String getValue() {
        return value;
    }

    // empty alias - should use method name
    @GsonJsonProperty("")
    protected String getEmptyAlias() {
        return "";
    }

    @GsonJsonProperty("get-with-alias")
    protected String getWithAlias() {
        return "";
    }

    // no alias, begins with "get"
    protected String getPlain() {
        return "";
    }

    // begins with "is", returns primitive boolean
    protected boolean isPrimBool() {
        return true;
    }

    // begins with "is", returns boxed Boolean
    protected Boolean isBoxedBool() {
        return true;
    }

    // begins with "is", but doesn't return a boolean
    protected String isString() {
        return "";
    }

    // doesn't begin with "get"
    protected String noGet() {
        return "";
    }

    // nothing after "get"
    protected String get() {
        return "";
    }


    protected void setValue(String text) {
        // do nothing
    }

    // empty alias - should use method name
    @GsonJsonProperty("")
    protected void setEmptyAlias(String text) {
        // do nothing
    }

    @GsonJsonProperty("set-with-alias")
    protected void setWithAlias(String text) {
        // do nothing
    }

    // no alias, begins with "set"
    protected void setPlain(String text) {
        // do nothing
    }

    // doesn't begin with "set"
    protected void noSet(String text) {
        // do nothing
    }

    // nothing after "get"
    protected void set(String text) {
        // do nothing
    }

    // returns a list
    protected List<Data> getMyList() {
        return listField;
    }

    // accepts a list
    protected void setMyList(List<Data> newList) {
        listField = newList;
    }
}