• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 
23 import android.os.Parcel;
24 
25 import androidx.test.ext.junit.runners.AndroidJUnit4;
26 
27 import com.google.protobuf.ByteString;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import javax.annotation.Nullable;
33 
34 @RunWith(AndroidJUnit4.class)
35 public final class ExampleConsumptionTest {
36 
37     @Test
testBuilder_emptyCollectionName()38     public void testBuilder_emptyCollectionName() {
39         assertThrows(
40                 IllegalArgumentException.class,
41                 () ->
42                         new ExampleConsumption.Builder()
43                                 .setCollectionName("")
44                                 .setExampleCount(10)
45                                 .setSelectionCriteria(new byte[] {10, 0, 1})
46                                 .build());
47     }
48 
49     @Test
testBuilder_nullCollectionName()50     public void testBuilder_nullCollectionName() {
51         assertThrows(
52                 IllegalArgumentException.class,
53                 () ->
54                         new ExampleConsumption.Builder()
55                                 .setCollectionName(null)
56                                 .setExampleCount(10)
57                                 .setSelectionCriteria(new byte[] {10, 0, 1})
58                                 .build());
59     }
60 
61     @Test
testBuilder_nullSelectionCriteria()62     public void testBuilder_nullSelectionCriteria() {
63         assertThrows(
64                 NullPointerException.class,
65                 () ->
66                         new ExampleConsumption.Builder()
67                                 .setCollectionName("collection_name")
68                                 .setExampleCount(10)
69                                 .setSelectionCriteria(null)
70                                 .build());
71     }
72 
73     @Test
testBuilder_normalCaseWithoutResumptionToken()74     public void testBuilder_normalCaseWithoutResumptionToken() {
75         String collectionName = "my_collection";
76         byte[] selectionCriteria = new byte[] {10, 0, 1};
77         int exampleCount = 10;
78         ExampleConsumption consumption =
79                 createExampleConsumption(collectionName, selectionCriteria, exampleCount, null);
80         assertThat(consumption.getCollectionName()).isEqualTo(collectionName);
81         assertThat(consumption.getExampleCount()).isEqualTo(exampleCount);
82         assertThat(ByteString.copyFrom(consumption.getSelectionCriteria()))
83                 .isEqualTo(ByteString.copyFrom(selectionCriteria));
84         assertThat(consumption.getResumptionToken()).isNull();
85     }
86 
87     @Test
testBuilder_normalCaseWithResumptionToken()88     public void testBuilder_normalCaseWithResumptionToken() {
89         String collectionName = "my_collection";
90         byte[] selectionCriteria = new byte[] {10, 0, 1};
91         int exampleCount = 10;
92         byte[] resumptionToken = new byte[] {25, 10, 4, 56};
93         ExampleConsumption consumption =
94                 createExampleConsumption(
95                         collectionName, selectionCriteria, exampleCount, resumptionToken);
96         assertThat(consumption.getCollectionName()).isEqualTo(collectionName);
97         assertThat(consumption.getExampleCount()).isEqualTo(exampleCount);
98         assertThat(ByteString.copyFrom(consumption.getSelectionCriteria()))
99                 .isEqualTo(ByteString.copyFrom(selectionCriteria));
100         assertThat(ByteString.copyFrom(consumption.getResumptionToken()))
101                 .isEqualTo(ByteString.copyFrom(resumptionToken));
102     }
103 
104     @Test
testWriteToParcel()105     public void testWriteToParcel() {
106         String collectionName = "my_collection";
107         byte[] selectionCriteria = new byte[] {10, 0, 1};
108         int exampleCount = 10;
109         byte[] resumptionToken = new byte[] {25, 10, 4, 56};
110         ExampleConsumption consumption =
111                 createExampleConsumption(
112                         collectionName, selectionCriteria, exampleCount, resumptionToken);
113 
114         Parcel parcel = Parcel.obtain();
115         consumption.writeToParcel(parcel, 0);
116 
117         // Reset data position before recreating the parcelable.
118         parcel.setDataPosition(0);
119         ExampleConsumption recoveredConsumption =
120                 ExampleConsumption.CREATOR.createFromParcel(parcel);
121         assertThat(recoveredConsumption.getCollectionName()).isEqualTo(collectionName);
122         assertThat(recoveredConsumption.getExampleCount()).isEqualTo(exampleCount);
123         assertThat(ByteString.copyFrom(recoveredConsumption.getSelectionCriteria()))
124                 .isEqualTo(ByteString.copyFrom(selectionCriteria));
125         assertThat(ByteString.copyFrom(recoveredConsumption.getResumptionToken()))
126                 .isEqualTo(ByteString.copyFrom(resumptionToken));
127     }
128 
createExampleConsumption( String collectionName, byte[] selectionCriteria, int exampleCount, @Nullable byte[] resumptionToken)129     private static ExampleConsumption createExampleConsumption(
130             String collectionName,
131             byte[] selectionCriteria,
132             int exampleCount,
133             @Nullable byte[] resumptionToken) {
134         ExampleConsumption.Builder builder =
135                 new ExampleConsumption.Builder()
136                         .setCollectionName(collectionName)
137                         .setSelectionCriteria(selectionCriteria)
138                         .setExampleCount(exampleCount);
139         if (resumptionToken != null) {
140             builder.setResumptionToken(resumptionToken);
141         }
142         return builder.build();
143     }
144 }
145