1 /*
2  * Copyright (C) 2018 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 com.googlecode.objectify.ObjectifyService.ofy;
20 
21 import com.fasterxml.jackson.annotation.JsonAutoDetect;
22 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
23 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
24 import com.googlecode.objectify.Key;
25 import com.googlecode.objectify.annotation.Cache;
26 import com.googlecode.objectify.annotation.Entity;
27 import com.googlecode.objectify.annotation.Id;
28 import com.googlecode.objectify.annotation.Index;
29 import com.googlecode.objectify.annotation.Parent;
30 import java.util.Date;
31 import java.util.List;
32 import java.util.UUID;
33 import lombok.EqualsAndHashCode;
34 import lombok.Getter;
35 import lombok.NoArgsConstructor;
36 import lombok.Setter;
37 
38 /** Entity Class for ApiCoverageEntity */
39 @Cache
40 @Entity(name = "ApiCoverage")
41 @EqualsAndHashCode(of = "id")
42 @NoArgsConstructor
43 @JsonAutoDetect(fieldVisibility = Visibility.ANY)
44 @JsonIgnoreProperties({"id", "parent"})
45 public class ApiCoverageEntity implements DashboardEntity {
46 
47     /** ApiCoverageEntity id field */
48     @Id @Getter @Setter String id;
49 
50     @Parent @Getter Key<?> parent;
51 
52     /** HAL package name. e.g. android.hardware.foo. */
53     @Index @Getter @Setter String halPackageName;
54 
55     /** HAL (major) version. e.g. 1. */
56     @Index @Getter @Setter int halMajorVersion;
57 
58     /** HAL (minor) version. e.g. 0. */
59     @Index @Getter @Setter int halMinorVersion;
60 
61     /** HAL interface name. e.g. IFoo. */
62     @Index @Getter @Setter String halInterfaceName;
63 
64     /** List of HAL API */
65     @Getter @Setter List<String> halApi;
66 
67     /** List of HAL covered API */
68     @Getter @Setter List<String> coveredHalApi;
69 
70     /** When this record was created or updated */
71     @Index Date updated;
72 
73     /** Constructor function for ApiCoverageEntity Class */
ApiCoverageEntity( com.google.appengine.api.datastore.Key testRunKey, String halPackageName, int halVersionMajor, int halVersionMinor, String halInterfaceName, List<String> halApi, List<String> coveredHalApi)74     public ApiCoverageEntity(
75             com.google.appengine.api.datastore.Key testRunKey,
76             String halPackageName,
77             int halVersionMajor,
78             int halVersionMinor,
79             String halInterfaceName,
80             List<String> halApi,
81             List<String> coveredHalApi) {
82         this.id = UUID.randomUUID().toString();
83         this.parent = getParentKey(testRunKey);
84 
85         this.halPackageName = halPackageName;
86         this.halMajorVersion = halVersionMajor;
87         this.halMinorVersion = halVersionMinor;
88         this.halInterfaceName = halInterfaceName;
89         this.halApi = halApi;
90         this.coveredHalApi = coveredHalApi;
91         this.updated = new Date();
92     }
93 
94     /** Constructor function for ApiCoverageEntity Class with objectify Key. */
ApiCoverageEntity( Key testRunKey, String halPackageName, int halVersionMajor, int halVersionMinor, String halInterfaceName, List<String> halApi, List<String> coveredHalApi)95     public ApiCoverageEntity(
96             Key testRunKey,
97             String halPackageName,
98             int halVersionMajor,
99             int halVersionMinor,
100             String halInterfaceName,
101             List<String> halApi,
102             List<String> coveredHalApi) {
103         this.id = UUID.randomUUID().toString();
104         this.parent = testRunKey;
105 
106         this.halPackageName = halPackageName;
107         this.halMajorVersion = halVersionMajor;
108         this.halMinorVersion = halVersionMinor;
109         this.halInterfaceName = halInterfaceName;
110         this.halApi = halApi;
111         this.coveredHalApi = coveredHalApi;
112         this.updated = new Date();
113     }
114 
115     /** Get objectify Key from datastore Key type */
getParentKey(com.google.appengine.api.datastore.Key testRunKey)116     private Key getParentKey(com.google.appengine.api.datastore.Key testRunKey) {
117         Key testParentKey = Key.create(TestEntity.class, testRunKey.getParent().getName());
118         return Key.create(testParentKey, TestRunEntity.class, testRunKey.getId());
119     }
120 
121     /** Get UrlSafeKey from ApiCoverageEntity Information */
getUrlSafeKey()122     public String getUrlSafeKey() {
123         Key uuidKey = Key.create(this.parent, ApiCoverageEntity.class, this.id);
124         return uuidKey.toUrlSafe();
125     }
126 
127     /** Saving function for the instance of this class */
128     @Override
save()129     public Key<ApiCoverageEntity> save() {
130         this.id = UUID.randomUUID().toString();
131         this.updated = new Date();
132         return ofy().save().entity(this).now();
133     }
134 
135     /** Get List of ApiCoverageEntity by HAL interface name */
getByUrlSafeKey(String urlSafeKey)136     public static ApiCoverageEntity getByUrlSafeKey(String urlSafeKey) {
137         return ofy().load()
138                 .type(ApiCoverageEntity.class)
139                 .filterKey(com.google.cloud.datastore.Key.fromUrlSafe(urlSafeKey))
140                 .first()
141                 .now();
142     }
143 
144     /** Get List of ApiCoverageEntity by HAL interface name */
getByInterfaceNameList(String halInterfaceName)145     public static List<ApiCoverageEntity> getByInterfaceNameList(String halInterfaceName) {
146         return ofy().load()
147                 .type(ApiCoverageEntity.class)
148                 .filter("halInterfaceName", halInterfaceName)
149                 .list();
150     }
151 
152     /** Get List of ApiCoverageEntity by HAL package name */
getByPackageNameList(String packageName)153     public static List<ApiCoverageEntity> getByPackageNameList(String packageName) {
154         return ofy().load()
155                 .type(ApiCoverageEntity.class)
156                 .filter("halPackageName", packageName)
157                 .list();
158     }
159 }
160