1 /*
2  * Copyright (c) 2017 Google Inc. All Rights Reserved.
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 com.googlecode.objectify.ObjectifyService.ofy;
20 
21 import com.google.appengine.api.datastore.Key;
22 import com.google.appengine.api.datastore.KeyFactory;
23 import com.googlecode.objectify.annotation.Cache;
24 import com.googlecode.objectify.annotation.Entity;
25 import com.googlecode.objectify.annotation.Id;
26 import com.googlecode.objectify.annotation.Index;
27 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import java.util.stream.Collectors;
31 import lombok.Data;
32 import lombok.Getter;
33 import lombok.NoArgsConstructor;
34 import lombok.Setter;
35 
36 @Entity(name = "Test")
37 @Cache
38 @Data
39 @NoArgsConstructor
40 /** Entity describing test metadata. */
41 public class TestEntity implements DashboardEntity {
42     protected static final Logger logger = Logger.getLogger(TestEntity.class.getName());
43 
44     public static final String KIND = "Test";
45     public static final String HAS_PROFILING_DATA = "hasProfilingData";
46 
47     @Id
48     @Getter
49     @Setter
50     private String testName;
51 
52     @Index
53     @Getter
54     @Setter
55     private boolean hasProfilingData;
56 
57     /**
58      * Create a TestEntity object.
59      *
60      * @param testName The name of the test.
61      * @param hasProfilingData True if the test includes profiling data.
62      */
TestEntity(String testName, boolean hasProfilingData)63     public TestEntity(String testName, boolean hasProfilingData) {
64         this.testName = testName;
65         this.hasProfilingData = hasProfilingData;
66     }
67 
68     /**
69      * Create a TestEntity object.
70      *
71      * @param testName The name of the test.
72      */
TestEntity(String testName)73     public TestEntity(String testName) {
74         this(testName, false);
75     }
76 
77     /** Saving function for the instance of this class */
78     @Override
save()79     public com.googlecode.objectify.Key<TestEntity> save() {
80         return ofy().save().entity(this).now();
81     }
82 
toEntity()83     public com.google.appengine.api.datastore.Entity toEntity() {
84         com.google.appengine.api.datastore.Entity testEntity = new com.google.appengine.api.datastore.Entity(this.getOldKey());
85         testEntity.setProperty(HAS_PROFILING_DATA, this.hasProfilingData);
86         return testEntity;
87     }
88 
89     /**
90      * Get objectify TestRun Entity's key.
91      *
92      * @param startTimestamp test start timestamp
93      */
getTestRunKey(long startTimestamp)94     public com.googlecode.objectify.Key getTestRunKey(long startTimestamp) {
95         com.googlecode.objectify.Key testKey =
96                 com.googlecode.objectify.Key.create(TestEntity.class, this.getTestName());
97         com.googlecode.objectify.Key testRunKey =
98                 com.googlecode.objectify.Key.create(testKey, TestRunEntity.class, startTimestamp);
99         return testRunKey;
100     }
101 
102     /**
103      * Get key info from appengine based library.
104      */
getOldKey()105     public Key getOldKey() {
106         return KeyFactory.createKey(KIND, testName);
107     }
108 
getAllTestNames()109     public static List<String> getAllTestNames() {
110         List<TestEntity> testEntityList = getAllTest();
111 
112         List<String> allTestNames = testEntityList.stream()
113             .map(te -> te.getTestName()).collect(Collectors.toList());
114         return allTestNames;
115     }
116 
getAllTest()117     public static List<TestEntity> getAllTest() {
118         return ofy().load().type(TestEntity.class).list();
119     }
120 
121     @Override
equals(Object obj)122     public boolean equals(Object obj) {
123         if (obj == null || !TestEntity.class.isAssignableFrom(obj.getClass())) {
124             return false;
125         }
126         TestEntity test2 = (TestEntity) obj;
127         return (
128             this.testName.equals(test2.testName) &&
129                 this.hasProfilingData == test2.hasProfilingData);
130     }
131 
132     /**
133      * Convert an Entity object to a TestEntity.
134      *
135      * @param e The entity to process.
136      * @return TestEntity object with the properties from e processed, or null if incompatible.
137      */
138     @SuppressWarnings("unchecked")
fromEntity(com.google.appengine.api.datastore.Entity e)139     public static TestEntity fromEntity(com.google.appengine.api.datastore.Entity e) {
140         if (!e.getKind().equals(KIND) || e.getKey().getName() == null) {
141             logger.log(Level.WARNING, "Missing test attributes in entity: " + e.toString());
142             return null;
143         }
144         String testName = e.getKey().getName();
145         boolean hasProfilingData = false;
146         if (e.hasProperty(HAS_PROFILING_DATA)) {
147             hasProfilingData = (boolean) e.getProperty(HAS_PROFILING_DATA);
148         }
149         return new TestEntity(testName, hasProfilingData);
150     }
151 }
152