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 com.fasterxml.jackson.annotation.JsonAutoDetect;
20 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
21 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22 import com.google.appengine.api.datastore.KeyFactory;
23 import com.googlecode.objectify.Key;
24 
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 lombok.EqualsAndHashCode;
31 import lombok.Getter;
32 import lombok.NoArgsConstructor;
33 import lombok.Setter;
34 
35 import java.util.Date;
36 import java.util.logging.Logger;
37 
38 import static com.googlecode.objectify.ObjectifyService.ofy;
39 
40 /** Entity Class for CodeCoverageEntity */
41 @Cache
42 @Entity(name = "CodeCoverage")
43 @EqualsAndHashCode(of = "id")
44 @NoArgsConstructor
45 @JsonAutoDetect(fieldVisibility = Visibility.ANY)
46 @JsonIgnoreProperties({"id", "parent"})
47 public class CodeCoverageEntity implements DashboardEntity {
48     protected static final Logger logger = Logger.getLogger(CodeCoverageEntity.class.getName());
49 
50     public static final String KIND = "CodeCoverage";
51 
52     public static final String COVERED_LINE_COUNT = "coveredLineCount";
53     public static final String TOTAL_LINE_COUNT = "totalLineCount";
54 
55     /** CodeCoverageEntity id field */
56     @Id @Getter @Setter Long id;
57 
58     @Parent @Getter Key<?> parent;
59 
60     @Index @Getter @Setter private long coveredLineCount;
61 
62     @Index @Getter @Setter private long totalLineCount;
63 
64     /** When this record was created or updated */
65     @Index Date updated;
66 
67     /** Constructor function for ApiCoverageEntity Class */
CodeCoverageEntity( com.google.appengine.api.datastore.Key testRunKey, long coveredLineCount, long totalLineCount)68     public CodeCoverageEntity(
69             com.google.appengine.api.datastore.Key testRunKey,
70             long coveredLineCount,
71             long totalLineCount) {
72 
73         this.parent = getParentKey(testRunKey);
74 
75         this.coveredLineCount = coveredLineCount;
76         this.totalLineCount = totalLineCount;
77     }
78 
79     /** Constructor function for ApiCoverageEntity Class */
CodeCoverageEntity( long id, com.google.appengine.api.datastore.Key testRunKey, long coveredLineCount, long totalLineCount)80     public CodeCoverageEntity(
81             long id,
82             com.google.appengine.api.datastore.Key testRunKey,
83             long coveredLineCount,
84             long totalLineCount) {
85         this.id = id;
86 
87         this.parent = getParentKey(testRunKey);
88 
89         this.coveredLineCount = coveredLineCount;
90         this.totalLineCount = totalLineCount;
91     }
92 
93     /** Constructor function for ApiCoverageEntity Class with objectify key*/
CodeCoverageEntity(Key testRunKey, long coveredLineCount, long totalLineCount)94     public CodeCoverageEntity(Key testRunKey, long coveredLineCount, long totalLineCount) {
95         this.parent = testRunKey;
96         this.coveredLineCount = coveredLineCount;
97         this.totalLineCount = totalLineCount;
98     }
99 
100     /** Get objectify Key from datastore Key type */
getParentKey(com.google.appengine.api.datastore.Key testRunKey)101     private Key getParentKey(com.google.appengine.api.datastore.Key testRunKey) {
102         Key testParentKey = Key.create(TestEntity.class, testRunKey.getParent().getName());
103         return Key.create(testParentKey, TestRunEntity.class, testRunKey.getId());
104     }
105 
106     /** Get UrlSafeKey from ApiCoverageEntity Information */
getUrlSafeKey()107     public String getUrlSafeKey() {
108         Key uuidKey = Key.create(this.parent, CodeCoverageEntity.class, this.id);
109         return uuidKey.toUrlSafe();
110     }
111 
112     /** Saving function for the instance of this class */
113     @Override
save()114     public Key<CodeCoverageEntity> save() {
115         this.id = this.getParent().getId();
116         this.updated = new Date();
117         return ofy().save().entity(this).now();
118     }
119 
toEntity()120     public com.google.appengine.api.datastore.Entity toEntity() {
121         com.google.appengine.api.datastore.Key testKey =
122                 KeyFactory.createKey(TestEntity.KIND, this.getParent().getParent().getName());
123         com.google.appengine.api.datastore.Key testRunKey =
124                 KeyFactory.createKey(testKey, TestRunEntity.KIND, this.getParent().getId());
125         com.google.appengine.api.datastore.Key codeCoverageKey =
126                 KeyFactory.createKey(testRunKey, KIND, this.getParent().getId());
127 
128         com.google.appengine.api.datastore.Entity codeCoverageEntity =
129                 new com.google.appengine.api.datastore.Entity(codeCoverageKey);
130         codeCoverageEntity.setProperty(COVERED_LINE_COUNT, this.coveredLineCount);
131         codeCoverageEntity.setProperty(TOTAL_LINE_COUNT, this.totalLineCount);
132         codeCoverageEntity.setIndexedProperty("updated", new Date());
133         return codeCoverageEntity;
134     }
135 }
136