1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may 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 implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.bedstead.harrier 17 18 import com.android.bedstead.enterprise.annotations.CanSetPolicyTest 19 import com.android.bedstead.enterprise.annotations.CannotSetPolicyTest 20 import com.android.bedstead.enterprise.annotations.PolicyAppliesTest 21 import com.android.bedstead.enterprise.annotations.PolicyDoesNotApplyTest 22 import com.android.bedstead.harrier.annotations.PolicyArgument 23 import java.util.stream.Stream 24 import org.junit.runners.model.FrameworkMethod 25 26 /** 27 * Allows to execute bedstead-enterprise methods from Harrier when the module is loaded 28 */ 29 interface HarrierToEnterpriseMediator { 30 31 /** 32 * Generates extra tests for test functions using 33 * [PolicyArgument] annotation in the function parameter 34 */ generatePolicyArgumentTestsnull35 fun generatePolicyArgumentTests( 36 frameworkMethod: FrameworkMethod, 37 expandedMethods: Stream<FrameworkMethod> 38 ): Stream<FrameworkMethod> 39 40 /** 41 * Parse enterprise-specific annotations in [BedsteadJUnit4], 42 * i.e. [PolicyAppliesTest], [PolicyDoesNotApplyTest], 43 * [CanSetPolicyTest] and [CannotSetPolicyTest] 44 * To be used before general annotation processing. 45 */ 46 fun parseEnterpriseAnnotations(annotations: List<Annotation>) 47 48 companion object { 49 private const val IMPLEMENTATION = 50 "com.android.bedstead.enterprise.HarrierToEnterpriseMediatorImpl" 51 52 private val mediatorInternal: HarrierToEnterpriseMediator? by lazy { 53 try { 54 Class.forName( 55 IMPLEMENTATION 56 ).getDeclaredConstructor().newInstance() as HarrierToEnterpriseMediator 57 } catch (ignored: ReflectiveOperationException) { 58 null 59 } 60 } 61 62 /** 63 * @return HarrierToEnterpriseMediator or null 64 * if the bedstead-enterprise module isn't loaded 65 */ 66 fun getMediatorOrNull(): HarrierToEnterpriseMediator? { 67 return mediatorInternal 68 } 69 70 /** 71 * @return HarrierToEnterpriseMediator or throws [IllegalStateException] when the 72 * bedstead-enterprise module isn't loaded 73 */ 74 fun getMediatorOrThrowException(attemptString: String = ""): HarrierToEnterpriseMediator { 75 return mediatorInternal ?: throw IllegalStateException( 76 "bedstead-enterprise module is not loaded - " + 77 "add it to static_libs to fix it, $attemptString" 78 ) 79 } 80 } 81 } 82