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 com.google.appengine.api.datastore.Entity; 20 import java.util.logging.Level; 21 import java.util.logging.Logger; 22 23 /** Entity describing test coverage status. */ 24 public class TestCoverageStatusEntity implements DashboardEntity { 25 protected static final Logger logger = 26 Logger.getLogger(TestCoverageStatusEntity.class.getName()); 27 28 public static final String KIND = "TestCoverageStatus"; 29 30 // Property keys 31 public static final String TOTAL_LINE_COUNT = "totalLineCount"; 32 public static final String COVERED_LINE_COUNT = "coveredLineCount"; 33 public static final String UPDATED_TIMESTAMP = "updatedTimestamp"; 34 35 public final String testName; 36 public final long coveredLineCount; 37 public final long totalLineCount; 38 public final long timestamp; 39 40 /** 41 * Create a TestCoverageStatusEntity object with status metadata. 42 * 43 * @param testName The name of the test. 44 * @param timestamp The timestamp indicating the most recent test run event in the test state. 45 * @param coveredLineCount The number of lines covered. 46 * @param totalLineCount The total number of lines. 47 */ 48 public TestCoverageStatusEntity( 49 String testName, long timestamp, long coveredLineCount, long totalLineCount) { 50 this.testName = testName; 51 this.timestamp = timestamp; 52 this.coveredLineCount = coveredLineCount; 53 this.totalLineCount = totalLineCount; 54 } 55 56 @Override 57 public Entity toEntity() { 58 Entity testEntity = new Entity(KIND, this.testName); 59 testEntity.setProperty(UPDATED_TIMESTAMP, this.timestamp); 60 testEntity.setProperty(COVERED_LINE_COUNT, this.coveredLineCount); 61 testEntity.setProperty(TOTAL_LINE_COUNT, this.totalLineCount); 62 return testEntity; 63 } 64 65 /** 66 * Convert an Entity object to a TestCoverageStatusEntity. 67 * 68 * @param e The entity to process. 69 * @return TestCoverageStatusEntity object with the properties from e processed, or null if 70 * incompatible. 71 */ 72 @SuppressWarnings("unchecked") 73 public static TestCoverageStatusEntity fromEntity(Entity e) { 74 if (!e.getKind().equals(KIND) 75 || e.getKey().getName() == null 76 || !e.hasProperty(UPDATED_TIMESTAMP) 77 || !e.hasProperty(COVERED_LINE_COUNT) 78 || !e.hasProperty(TOTAL_LINE_COUNT)) { 79 logger.log(Level.WARNING, "Missing test attributes in entity: " + e.toString()); 80 return null; 81 } 82 String testName = e.getKey().getName(); 83 long timestamp = 0; 84 long coveredLineCount = -1; 85 long totalLineCount = -1; 86 try { 87 timestamp = (long) e.getProperty(UPDATED_TIMESTAMP); 88 coveredLineCount = (Long) e.getProperty(COVERED_LINE_COUNT); 89 totalLineCount = (Long) e.getProperty(TOTAL_LINE_COUNT); 90 } catch (ClassCastException exception) { 91 // Invalid contents or null values 92 logger.log(Level.WARNING, "Error parsing test entity.", exception); 93 return null; 94 } 95 return new TestCoverageStatusEntity(testName, timestamp, coveredLineCount, totalLineCount); 96 } 97 } 98