summaryrefslogtreecommitdiff
path: root/src/test/java/com/android/vts/entity/TestAcknowledgmentEntityTest.java
blob: 8ed68b671b860f3e03952f430efc52bc2873f552 (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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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 com.android.vts.entity;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TestAcknowledgmentEntityTest {
    private final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalUserServiceTestConfig())
                    .setEnvIsAdmin(true)
                    .setEnvIsLoggedIn(true)
                    .setEnvEmail("testemail@domain.com")
                    .setEnvAuthDomain("test");

    @Before
    public void setUp() {
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }

    /** Test serialization to/from Entity objects. */
    @Test
    public void testEntitySerialization() {
        Key key = KeyFactory.createKey(TestEntity.KIND, "test");
        User user = UserServiceFactory.getUserService().getCurrentUser();
        List<String> branches = new ArrayList<>();
        branches.add("branch1");
        List<String> devices = new ArrayList<>();
        devices.add("device1");
        List<String> testCaseNames = new ArrayList<>();
        testCaseNames.add("testCase1");
        Text note = new Text("note");
        TestAcknowledgmentEntity ack =
                new TestAcknowledgmentEntity(key, user, branches, devices, testCaseNames, note);
        Entity e = ack.toEntity();

        Assert.assertNotNull(e);
        Assert.assertEquals(key, e.getProperty(TestAcknowledgmentEntity.TEST_KEY));
        Assert.assertEquals(user, e.getProperty(TestAcknowledgmentEntity.USER_OBJ));
        Assert.assertTrue(
                ((List<String>) e.getProperty(TestAcknowledgmentEntity.BRANCHES))
                        .containsAll(branches));
        Assert.assertTrue(
                ((List<String>) e.getProperty(TestAcknowledgmentEntity.DEVICES))
                        .containsAll(devices));
        Assert.assertTrue(
                ((List<String>) e.getProperty(TestAcknowledgmentEntity.TEST_CASE_NAMES))
                        .containsAll(testCaseNames));
        Assert.assertEquals(note, e.getProperty(TestAcknowledgmentEntity.NOTE));

        TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromEntity(e);
        Assert.assertNotNull(deserialized);
        Assert.assertEquals(key, deserialized.test);
        Assert.assertEquals(user, deserialized.getUserObj());
        Assert.assertTrue(deserialized.getBranches().containsAll(branches));
        Assert.assertTrue(deserialized.getDevices().containsAll(devices));
        Assert.assertTrue(deserialized.getTestCaseNames().containsAll(testCaseNames));
        Assert.assertEquals(note.getValue(), deserialized.getNote());
    }

    /** Test serialization to/from Entity objects when optional parameters are null. */
    @Test
    public void testEntitySerializationWithNulls() {
        Key key = KeyFactory.createKey(TestEntity.KIND, "test");
        User user = UserServiceFactory.getUserService().getCurrentUser();
        TestAcknowledgmentEntity ack =
                new TestAcknowledgmentEntity(key, user, null, null, null, null);
        Entity e = ack.toEntity();

        Assert.assertNotNull(e);
        Assert.assertEquals(key, e.getProperty(TestAcknowledgmentEntity.TEST_KEY));
        Assert.assertEquals(user, e.getProperty(TestAcknowledgmentEntity.USER_OBJ));
        Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.BRANCHES));
        Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.DEVICES));
        Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.TEST_CASE_NAMES));
        Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.NOTE));

        TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromEntity(e);
        Assert.assertNotNull(deserialized);
        Assert.assertEquals(key, deserialized.test);
        Assert.assertEquals(user, deserialized.getUserObj());
        Assert.assertEquals(0, deserialized.getBranches().size());
        Assert.assertEquals(0, deserialized.getDevices().size());
        Assert.assertEquals(0, deserialized.getTestCaseNames().size());
        Assert.assertNull(deserialized.getNote());
    }

    /** Test serialization to/from Json objects. */
    @Test
    public void testJsonSerialization() {
        Key key = KeyFactory.createKey(TestEntity.KIND, "test");
        User user = UserServiceFactory.getUserService().getCurrentUser();
        List<String> branches = new ArrayList<>();
        branches.add("branch1");
        List<String> devices = new ArrayList<>();
        devices.add("device1");
        List<String> testCaseNames = new ArrayList<>();
        testCaseNames.add("testCase1");
        Text note = new Text("note");
        TestAcknowledgmentEntity ack =
                new TestAcknowledgmentEntity(key, user, branches, devices, testCaseNames, note);
        Entity e = new Entity(KeyFactory.createKey(TestAcknowledgmentEntity.KIND, "fakekey"));
        e.setPropertiesFrom(ack.toEntity());
        JsonObject json = TestAcknowledgmentEntity.fromEntity(e).toJson();

        TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromJson(user, json);
        Assert.assertNotNull(deserialized);
        Assert.assertEquals(key, deserialized.test);
        Assert.assertEquals(user, deserialized.getUserObj());
        Assert.assertTrue(deserialized.getBranches().containsAll(branches));
        Assert.assertTrue(deserialized.getDevices().containsAll(devices));
        Assert.assertTrue(deserialized.getTestCaseNames().containsAll(testCaseNames));
        Assert.assertEquals(note.getValue(), deserialized.getNote());
    }

    /** Test serialization to/from Json objects when optional properties are null. */
    @Test
    public void testJsonSerializationWithNulls() {
        Key key = KeyFactory.createKey(TestEntity.KIND, "test");
        User user = UserServiceFactory.getUserService().getCurrentUser();
        TestAcknowledgmentEntity ack =
                new TestAcknowledgmentEntity(key, user, null, null, null, null);
        Entity e = new Entity(KeyFactory.createKey(TestAcknowledgmentEntity.KIND, "fakekey"));
        e.setPropertiesFrom(ack.toEntity());
        JsonObject json = TestAcknowledgmentEntity.fromEntity(e).toJson();

        TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromJson(user, json);
        Assert.assertNotNull(deserialized);
        Assert.assertEquals(key, deserialized.test);
        Assert.assertEquals(user, deserialized.getUserObj());
        Assert.assertEquals(0, deserialized.getBranches().size());
        Assert.assertEquals(0, deserialized.getDevices().size());
        Assert.assertEquals(0, deserialized.getTestCaseNames().size());
        Assert.assertEquals("", deserialized.getNote());
    }
}