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 17 package android.healthconnect.aidl; 18 19 import static android.healthconnect.cts.phr.utils.PhrDataFactory.DATA_SOURCE_ID; 20 import static android.healthconnect.cts.phr.utils.PhrDataFactory.FHIR_DATA_ALLERGY; 21 import static android.healthconnect.cts.phr.utils.PhrDataFactory.FHIR_DATA_IMMUNIZATION; 22 import static android.healthconnect.cts.phr.utils.PhrDataFactory.FHIR_VERSION_R4; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.junit.Assert.assertThrows; 27 28 import android.health.connect.UpsertMedicalResourceRequest; 29 import android.health.connect.aidl.UpsertMedicalResourceRequestsParcel; 30 import android.os.Parcel; 31 import android.platform.test.annotations.RequiresFlagsEnabled; 32 import android.platform.test.flag.junit.CheckFlagsRule; 33 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 34 35 import androidx.test.ext.junit.runners.AndroidJUnit4; 36 37 import com.android.healthfitness.flags.Flags; 38 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 import java.util.List; 44 45 @RunWith(AndroidJUnit4.class) 46 @RequiresFlagsEnabled(Flags.FLAG_PERSONAL_HEALTH_RECORD) 47 public class UpsertMedicalResourceRequestsParcelTest { 48 @Rule 49 public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); 50 51 @Test testUpsertMedicalResourceRequestsParcel_successfulCreateAndGet()52 public void testUpsertMedicalResourceRequestsParcel_successfulCreateAndGet() { 53 UpsertMedicalResourceRequest request1 = 54 new UpsertMedicalResourceRequest.Builder( 55 DATA_SOURCE_ID, FHIR_VERSION_R4, FHIR_DATA_ALLERGY) 56 .build(); 57 UpsertMedicalResourceRequest request2 = 58 new UpsertMedicalResourceRequest.Builder( 59 DATA_SOURCE_ID, FHIR_VERSION_R4, FHIR_DATA_IMMUNIZATION) 60 .build(); 61 62 UpsertMedicalResourceRequestsParcel requestsParcel = 63 new UpsertMedicalResourceRequestsParcel(List.of(request1, request2)); 64 65 assertThat(requestsParcel.getUpsertRequests()).containsExactly(request1, request2); 66 } 67 68 @Test testUpsertMedicalResourceRequestsParcel_nullRequests_throws()69 public void testUpsertMedicalResourceRequestsParcel_nullRequests_throws() { 70 assertThrows( 71 NullPointerException.class, () -> new UpsertMedicalResourceRequestsParcel(null)); 72 } 73 74 @Test 75 public void testUpsertMedicalResourceRequestsParcel_writeToParcelThenRestore_propertiesIdentical()76 testUpsertMedicalResourceRequestsParcel_writeToParcelThenRestore_propertiesIdentical() { 77 UpsertMedicalResourceRequest request1 = 78 new UpsertMedicalResourceRequest.Builder( 79 DATA_SOURCE_ID, FHIR_VERSION_R4, FHIR_DATA_ALLERGY) 80 .build(); 81 UpsertMedicalResourceRequest request2 = 82 new UpsertMedicalResourceRequest.Builder( 83 DATA_SOURCE_ID, FHIR_VERSION_R4, FHIR_DATA_IMMUNIZATION) 84 .build(); 85 UpsertMedicalResourceRequestsParcel original = 86 new UpsertMedicalResourceRequestsParcel(List.of(request1, request2)); 87 88 Parcel parcel = Parcel.obtain(); 89 original.writeToParcel(parcel, 0); 90 parcel.setDataPosition(0); 91 UpsertMedicalResourceRequestsParcel restoredParcel = 92 UpsertMedicalResourceRequestsParcel.CREATOR.createFromParcel(parcel); 93 94 assertThat(restoredParcel.getUpsertRequests()).containsExactly(request1, request2); 95 parcel.recycle(); 96 } 97 } 98