1 /* 2 * Copyright (C) 2023 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.federatedcompute.common; 18 19 import static android.federatedcompute.common.TrainingInterval.SCHEDULING_MODE_ONE_TIME; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.junit.Assert.assertThrows; 24 25 import android.os.Parcel; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 /** Tests for {@link TrainingOptions} */ 32 @RunWith(JUnit4.class) 33 public final class TrainingOptionsTest { 34 private static final String POPULATION_NAME = "population"; 35 private static final String SERVER_ADDRESS = "https://adtech.uri/"; 36 private static final TrainingInterval TRAINING_INTERVAL = 37 new TrainingInterval.Builder().setSchedulingMode(SCHEDULING_MODE_ONE_TIME).build(); 38 39 @Test testFederatedTask()40 public void testFederatedTask() { 41 TrainingOptions options = 42 new TrainingOptions.Builder() 43 .setPopulationName(POPULATION_NAME) 44 .setServerAddress(SERVER_ADDRESS) 45 .setTrainingInterval(TRAINING_INTERVAL) 46 .build(); 47 assertThat(options.getPopulationName()).isEqualTo(POPULATION_NAME); 48 assertThat(options.getTrainingInterval()).isEqualTo(TRAINING_INTERVAL); 49 } 50 51 @Test testNullPopulation()52 public void testNullPopulation() { 53 assertThrows( 54 IllegalArgumentException.class, 55 () -> 56 new TrainingOptions.Builder() 57 .setPopulationName(null) 58 .setTrainingInterval(TRAINING_INTERVAL) 59 .build()); 60 } 61 62 @Test testEmptyPopulation()63 public void testEmptyPopulation() { 64 assertThrows( 65 IllegalArgumentException.class, 66 () -> 67 new TrainingOptions.Builder() 68 .setPopulationName("") 69 .setTrainingInterval(TRAINING_INTERVAL) 70 .build()); 71 } 72 73 @Test testNullServerAddressIsNotAllowed()74 public void testNullServerAddressIsNotAllowed() { 75 assertThrows( 76 IllegalArgumentException.class, 77 () -> 78 new TrainingOptions.Builder() 79 .setPopulationName(POPULATION_NAME) 80 .setServerAddress(null) 81 .build()); 82 } 83 84 @Test testEmptyServerAddressIsNotAllowed()85 public void testEmptyServerAddressIsNotAllowed() { 86 assertThrows( 87 IllegalArgumentException.class, 88 () -> 89 new TrainingOptions.Builder() 90 .setPopulationName(POPULATION_NAME) 91 .setServerAddress("") 92 .build()); 93 } 94 95 @Test testNullTrainingIntervalIsAllowed()96 public void testNullTrainingIntervalIsAllowed() { 97 TrainingOptions options = 98 new TrainingOptions.Builder() 99 .setPopulationName(POPULATION_NAME) 100 .setServerAddress(SERVER_ADDRESS) 101 .setTrainingInterval(null) 102 .build(); 103 assertThat(options.getTrainingInterval()).isNull(); 104 } 105 106 @Test testNullContextDataIsAllowed()107 public void testNullContextDataIsAllowed() { 108 TrainingOptions options = 109 new TrainingOptions.Builder() 110 .setPopulationName(POPULATION_NAME) 111 .setServerAddress(SERVER_ADDRESS) 112 .setTrainingInterval(null) 113 .setContextData(null) 114 .build(); 115 116 assertThat(options.getContextData()).isNull(); 117 } 118 119 @Test testParcelValidInterval()120 public void testParcelValidInterval() { 121 TrainingOptions options = 122 new TrainingOptions.Builder() 123 .setPopulationName(POPULATION_NAME) 124 .setServerAddress(SERVER_ADDRESS) 125 .setTrainingInterval(null) 126 .build(); 127 128 Parcel p = Parcel.obtain(); 129 options.writeToParcel(p, 0); 130 p.setDataPosition(0); 131 TrainingOptions fromParcel = TrainingOptions.CREATOR.createFromParcel(p); 132 133 assertThat(options).isEqualTo(fromParcel); 134 } 135 } 136