Home | History | Annotate | Download | only in entity
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you
      5  * may not use this file except in compliance with the License. You may
      6  * obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
     13  * implied. See the License for the specific language governing
     14  * permissions and limitations under the License.
     15  */
     16 
     17 package com.android.vts.entity;
     18 
     19 import static org.junit.Assert.*;
     20 
     21 import com.google.appengine.api.datastore.Entity;
     22 import com.google.appengine.api.datastore.Key;
     23 import com.google.appengine.api.datastore.KeyFactory;
     24 import com.google.appengine.api.datastore.Text;
     25 import com.google.appengine.api.users.User;
     26 import com.google.appengine.api.users.UserServiceFactory;
     27 import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
     28 import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;
     29 import com.google.gson.JsonObject;
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 import org.junit.After;
     33 import org.junit.Assert;
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 
     37 public class TestAcknowledgmentEntityTest {
     38     private final LocalServiceTestHelper helper =
     39             new LocalServiceTestHelper(new LocalUserServiceTestConfig())
     40                     .setEnvIsAdmin(true)
     41                     .setEnvIsLoggedIn(true)
     42                     .setEnvEmail("testemail (at) domain.com")
     43                     .setEnvAuthDomain("test");
     44 
     45     @Before
     46     public void setUp() {
     47         helper.setUp();
     48     }
     49 
     50     @After
     51     public void tearDown() {
     52         helper.tearDown();
     53     }
     54 
     55     /** Test serialization to/from Entity objects. */
     56     @Test
     57     public void testEntitySerialization() {
     58         Key key = KeyFactory.createKey(TestEntity.KIND, "test");
     59         User user = UserServiceFactory.getUserService().getCurrentUser();
     60         List<String> branches = new ArrayList<>();
     61         branches.add("branch1");
     62         List<String> devices = new ArrayList<>();
     63         devices.add("device1");
     64         List<String> testCaseNames = new ArrayList<>();
     65         testCaseNames.add("testCase1");
     66         Text note = new Text("note");
     67         TestAcknowledgmentEntity ack =
     68                 new TestAcknowledgmentEntity(key, user, branches, devices, testCaseNames, note);
     69         Entity e = ack.toEntity();
     70 
     71         Assert.assertNotNull(e);
     72         Assert.assertEquals(key, e.getProperty(TestAcknowledgmentEntity.TEST_KEY));
     73         Assert.assertEquals(user, e.getProperty(TestAcknowledgmentEntity.USER));
     74         Assert.assertTrue(
     75                 ((List<String>) e.getProperty(TestAcknowledgmentEntity.BRANCHES))
     76                         .containsAll(branches));
     77         Assert.assertTrue(
     78                 ((List<String>) e.getProperty(TestAcknowledgmentEntity.DEVICES))
     79                         .containsAll(devices));
     80         Assert.assertTrue(
     81                 ((List<String>) e.getProperty(TestAcknowledgmentEntity.TEST_CASE_NAMES))
     82                         .containsAll(testCaseNames));
     83         Assert.assertEquals(note, e.getProperty(TestAcknowledgmentEntity.NOTE));
     84 
     85         TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromEntity(e);
     86         Assert.assertNotNull(deserialized);
     87         Assert.assertEquals(key, deserialized.test);
     88         Assert.assertEquals(user, deserialized.user);
     89         Assert.assertTrue(deserialized.branches.containsAll(branches));
     90         Assert.assertTrue(deserialized.devices.containsAll(devices));
     91         Assert.assertTrue(deserialized.testCaseNames.containsAll(testCaseNames));
     92         Assert.assertEquals(note.getValue(), deserialized.note);
     93     }
     94 
     95     /** Test serialization to/from Entity objects when optional parameters are null. */
     96     @Test
     97     public void testEntitySerializationWithNulls() {
     98         Key key = KeyFactory.createKey(TestEntity.KIND, "test");
     99         User user = UserServiceFactory.getUserService().getCurrentUser();
    100         TestAcknowledgmentEntity ack =
    101                 new TestAcknowledgmentEntity(key, user, null, null, null, null);
    102         Entity e = ack.toEntity();
    103 
    104         Assert.assertNotNull(e);
    105         Assert.assertEquals(key, e.getProperty(TestAcknowledgmentEntity.TEST_KEY));
    106         Assert.assertEquals(user, e.getProperty(TestAcknowledgmentEntity.USER));
    107         Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.BRANCHES));
    108         Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.DEVICES));
    109         Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.TEST_CASE_NAMES));
    110         Assert.assertFalse(e.hasProperty(TestAcknowledgmentEntity.NOTE));
    111 
    112         TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromEntity(e);
    113         Assert.assertNotNull(deserialized);
    114         Assert.assertEquals(key, deserialized.test);
    115         Assert.assertEquals(user, deserialized.user);
    116         Assert.assertEquals(0, deserialized.branches.size());
    117         Assert.assertEquals(0, deserialized.devices.size());
    118         Assert.assertEquals(0, deserialized.testCaseNames.size());
    119         Assert.assertNull(deserialized.note);
    120     }
    121 
    122     /** Test serialization to/from Json objects. */
    123     @Test
    124     public void testJsonSerialization() {
    125         Key key = KeyFactory.createKey(TestEntity.KIND, "test");
    126         User user = UserServiceFactory.getUserService().getCurrentUser();
    127         List<String> branches = new ArrayList<>();
    128         branches.add("branch1");
    129         List<String> devices = new ArrayList<>();
    130         devices.add("device1");
    131         List<String> testCaseNames = new ArrayList<>();
    132         testCaseNames.add("testCase1");
    133         Text note = new Text("note");
    134         TestAcknowledgmentEntity ack =
    135                 new TestAcknowledgmentEntity(key, user, branches, devices, testCaseNames, note);
    136         Entity e = new Entity(KeyFactory.createKey(TestAcknowledgmentEntity.KIND, "fakekey"));
    137         e.setPropertiesFrom(ack.toEntity());
    138         JsonObject json = TestAcknowledgmentEntity.fromEntity(e).toJson();
    139 
    140         TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromJson(user, json);
    141         Assert.assertNotNull(deserialized);
    142         Assert.assertEquals(key, deserialized.test);
    143         Assert.assertEquals(user, deserialized.user);
    144         Assert.assertTrue(deserialized.branches.containsAll(branches));
    145         Assert.assertTrue(deserialized.devices.containsAll(devices));
    146         Assert.assertTrue(deserialized.testCaseNames.containsAll(testCaseNames));
    147         Assert.assertEquals(note.getValue(), deserialized.note);
    148     }
    149 
    150     /** Test serialization to/from Json objects when optional properties are null. */
    151     @Test
    152     public void testJsonSerializationWithNulls() {
    153         Key key = KeyFactory.createKey(TestEntity.KIND, "test");
    154         User user = UserServiceFactory.getUserService().getCurrentUser();
    155         TestAcknowledgmentEntity ack =
    156                 new TestAcknowledgmentEntity(key, user, null, null, null, null);
    157         Entity e = new Entity(KeyFactory.createKey(TestAcknowledgmentEntity.KIND, "fakekey"));
    158         e.setPropertiesFrom(ack.toEntity());
    159         JsonObject json = TestAcknowledgmentEntity.fromEntity(e).toJson();
    160 
    161         TestAcknowledgmentEntity deserialized = TestAcknowledgmentEntity.fromJson(user, json);
    162         Assert.assertNotNull(deserialized);
    163         Assert.assertEquals(key, deserialized.test);
    164         Assert.assertEquals(user, deserialized.user);
    165         Assert.assertEquals(0, deserialized.branches.size());
    166         Assert.assertEquals(0, deserialized.devices.size());
    167         Assert.assertEquals(0, deserialized.testCaseNames.size());
    168         Assert.assertEquals("", deserialized.note);
    169     }
    170 }
    171