22
33import com .uid2 .optout .Const ;
44import com .uid2 .optout .TestUtils ;
5- import com .uid2 .optout .web .QuorumWebClient ;
6- import com .uid2 .shared .audit .UidInstanceIdProvider ;
7- import com .uid2 .shared .optout .OptOutEntry ;
85import com .uid2 .shared .optout .OptOutUtils ;
96import com .uid2 .shared .vertx .VertxUtils ;
10- import io .vertx .core .CompositeFuture ;
117import io .vertx .core .Future ;
128import io .vertx .core .Promise ;
139import io .vertx .core .Vertx ;
2218import org .junit .runner .RunWith ;
2319
2420import java .util .ArrayList ;
25- import java .util .List ;
2621
2722@ RunWith (VertxUnitRunner .class )
2823public class OptOutServiceVerticleTest {
2924 private static final String INTERNAL_TEST_KEY = "test-operator-key" ;
30- private static final String INTERNAL_OPTOUT_KEY = "test-optout-operator-key" ;
3125 private static Vertx vertx ;
32- private static final UidInstanceIdProvider uidInstanceIdProvider = new UidInstanceIdProvider ("test-instance" , "id" );
3326
3427 @ BeforeClass
3528 public static void suiteSetup (TestContext context ) throws Exception {
3629 vertx = Vertx .vertx ();
3730 JsonObject config = VertxUtils .getJsonConfig (vertx );
38- deployLogProducer (context , config )
39- .compose (v -> {
40- try {
41- return deployService (context , config );
42- } catch (Exception e ) {
43- return Future .failedFuture (e );
44- }
45- })
31+ deployService (context , config )
4632 .onComplete (context .asyncAssertSuccess ());
4733 }
4834
@@ -51,24 +37,14 @@ public static void suiteTearDown(TestContext context) {
5137 vertx .close (context .asyncAssertSuccess ());
5238 }
5339
54-
55- private static Future <String > deployLogProducer (TestContext context , JsonObject config ) throws Exception {
40+ private static Future <String > deployService (TestContext context , JsonObject config ) throws Exception {
5641 Promise <String > promise = Promise .promise ();
57-
58- // set data_dir option to use tmpDir during test
42+
5943 config
6044 .put (Const .Config .OptOutDataDirProp , OptOutUtils .tmpDir )
61- .put (Const .Config .OptOutInternalApiTokenProp , INTERNAL_TEST_KEY )
62- .put (Const .Config .OptOutReplicaUris , "http://127.0.0.1:8081/optout/write,http://127.0.0.1:8081/optout/write,http://127.0.0.1:8081/optout/write" );
45+ .put (Const .Config .OptOutInternalApiTokenProp , INTERNAL_TEST_KEY );
6346
64- OptOutLogProducer producer = TestUtils .createOptOutLogProducer (vertx , config );
65- vertx .deployVerticle (producer , ar -> promise .handle (ar ));
66- return promise .future ();
67- }
68-
69- private static Future <String > deployService (TestContext context , JsonObject config ) throws Exception {
70- Promise <String > promise = Promise .promise ();
71- OptOutServiceVerticle svc = TestUtils .createOptOutService (vertx , config , uidInstanceIdProvider );
47+ OptOutServiceVerticle svc = TestUtils .createOptOutService (vertx , config );
7248 vertx .deployVerticle (svc , ar -> {
7349 // set an empty cloud paths
7450 svc .setCloudPaths (new ArrayList <>());
@@ -77,142 +53,20 @@ private static Future<String> deployService(TestContext context, JsonObject conf
7753 return promise .future ();
7854 }
7955
80- @ Test
81- public void writeNull_expect400 (TestContext context ) {
82- verifyStatus (context , writeQuery (OptOutUtils .nullHashBytes ), 400 );
83- }
84-
85- @ Test
86- public void writeOnes_expect400 (TestContext context ) {
87- verifyStatus (context , writeQuery (OptOutUtils .onesHashBytes ), 400 );
88- }
89-
90- @ Test
91- public void writeId_expect200 (TestContext context ) {
92- verifyStatus (context , writeQuery (100 ), 200 );
93- }
94-
9556 @ Test
9657 public void getHealthCheck_expectOK (TestContext context ) {
9758 verifyStatus (context , Endpoints .OPS_HEALTHCHECK .toString (), 200 );
9859 }
9960
10061 @ Test
101- public void writeMultiple_expect200 (TestContext context ) {
102- Future <Void > f = Future .succeededFuture ();
103- for (int i = 0 ; i < 3 ; ++i ) {
104- final long id = 1 + i * 100 ;
105- f .compose (v -> verifyStatus (context , writeQuery (id ), 200 ));
106- }
107- f .onComplete (context .asyncAssertSuccess ());
108- }
109-
110- @ Test
111- public void writeIdsSerial_expect200 (TestContext context ) {
112- Future <Void > f = Future .succeededFuture ();
113- for (int i = 0 ; i < 100 ; ++i ) {
114- final long id = 100 + i ;
115- f .compose (v -> verifyStatus (context , writeQuery (id ), 200 ));
116- }
117- f .onComplete (context .asyncAssertSuccess ());
118- }
119-
120- @ Test
121- public void writeIdsParallel_expect200 (TestContext context ) {
122- List <Future > fs = new ArrayList <Future >();
123- for (int i = 0 ; i < 100 ; ++i ) {
124- final long id = 100 + i ;
125- fs .add (verifyStatus (context , writeQuery (id ), 200 ));
126- }
127- CompositeFuture .all (fs ).onComplete (context .asyncAssertSuccess ());
128- }
129-
130- // optout/add forwards request to remote optout/write api endpoints
131- @ Test
132- public void replicateWithoutOptoutRole_expect401 (TestContext context ) {
133- verifyStatus (context , replicateQuery (234 ), 401 );
134- }
135- @ Test
136- public void replicate_expect200 (TestContext context ) {
137- verifyStatus (context , replicateQuery (234 ), 200 , INTERNAL_OPTOUT_KEY );
138- }
139-
140- @ Test
141- public void testQuorumClient_expectSuccess (TestContext context ) {
142- String [] uris = new String [3 ];
143- for (int i = 0 ; i < 3 ; ++i ) {
144- uris [i ] = String .format ("http://127.0.0.1:%d%s" , Const .Port .ServicePortForOptOut , Endpoints .OPTOUT_WRITE );
145- }
146-
147- QuorumWebClient quorumClient = new QuorumWebClient (vertx , uris , uidInstanceIdProvider );
148- quorumClient .get (req -> {
149- req .addQueryParam (OptOutServiceVerticle .IDENTITY_HASH , OptOutEntry .idHashB64FromLong (123 ));
150- req .addQueryParam (OptOutServiceVerticle .ADVERTISING_ID , OptOutEntry .idHashB64FromLong (456 ));
151- req .bearerTokenAuthentication (INTERNAL_TEST_KEY );
152- return req ;
153- }).onComplete (context .asyncAssertSuccess ());
154- }
155-
156- @ Test
157- public void testQuorumClient1Failure_expectSuccess (TestContext context ) {
158- String [] uris = new String [3 ];
159- for (int i = 0 ; i < 2 ; ++i ) {
160- uris [i ] = String .format ("http://127.0.0.1:%d%s" , Const .Port .ServicePortForOptOut , Endpoints .OPTOUT_WRITE );
161- }
162- uris [2 ] = "http://httpstat.us/404" ;
163-
164- QuorumWebClient quorumClient = new QuorumWebClient (vertx , uris , uidInstanceIdProvider );
165- quorumClient .get (req -> {
166- req .addQueryParam (OptOutServiceVerticle .IDENTITY_HASH , OptOutEntry .idHashB64FromLong (123 ));
167- req .addQueryParam (OptOutServiceVerticle .ADVERTISING_ID , OptOutEntry .idHashB64FromLong (456 ));
168- req .bearerTokenAuthentication (INTERNAL_TEST_KEY );
169- return req ;
170- }).onComplete (context .asyncAssertSuccess ());
171- }
172-
173- @ Test
174- public void testQuorumClientAllFailures_expectSuccess (TestContext context ) {
175- String [] uris = new String [3 ];
176- for (int i = 0 ; i < 3 ; ++i ) {
177- uris [i ] = "http://httpstat.us/404" ;
178- }
179-
180- QuorumWebClient quorumClient = new QuorumWebClient (vertx , uris , uidInstanceIdProvider );
181- quorumClient .get (req -> {
182- req .addQueryParam (OptOutServiceVerticle .IDENTITY_HASH , OptOutEntry .idHashB64FromLong (123 ));
183- req .addQueryParam (OptOutServiceVerticle .ADVERTISING_ID , OptOutEntry .idHashB64FromLong (456 ));
184- return req ;
185- }).onComplete (context .asyncAssertFailure ());
186- }
187-
188- private String writeQuery (long id ) {
189- return this .writeQuery (OptOutEntry .idHashB64FromLong (id ));
190- }
191-
192- private String writeQuery (String identityHashB64 ) {
193- return this .writeQuery (identityHashB64 , identityHashB64 );
194- }
195-
196- private String writeQuery (byte [] identityHash ) {
197- return this .writeQuery (identityHash , identityHash );
198- }
199-
200- private String writeQuery (byte [] identityHash , byte [] advertisingId ) {
201- return this .writeQuery (OptOutUtils .byteArrayToBase64String (identityHash ),
202- OptOutUtils .byteArrayToBase64String (advertisingId ));
203- }
204-
205- private String writeQuery (String identityHashB64 , String advertisingIdB64 ) {
206- return String .format ("%s?%s=%s&%s=%s" , Endpoints .OPTOUT_WRITE ,
207- OptOutServiceVerticle .IDENTITY_HASH ,
208- identityHashB64 ,
209- OptOutServiceVerticle .ADVERTISING_ID ,
210- advertisingIdB64 );
62+ public void replicateWithoutAuth_expect401 (TestContext context ) {
63+ verifyStatus (context , replicateQuery (234 ), 401 , null );
21164 }
21265
21366 private String replicateQuery (long id ) {
214- return this .replicateQuery (OptOutEntry .idHashB64FromLong (id ),
215- OptOutEntry .idHashB64FromLong (id ));
67+ return this .replicateQuery (
68+ com .uid2 .shared .optout .OptOutEntry .idHashB64FromLong (id ),
69+ com .uid2 .shared .optout .OptOutEntry .idHashB64FromLong (id ));
21670 }
21771
21872 private String replicateQuery (String identityHashB64 , String advertisingIdB64 ) {
@@ -222,6 +76,7 @@ private String replicateQuery(String identityHashB64, String advertisingIdB64) {
22276 OptOutServiceVerticle .ADVERTISING_ID ,
22377 advertisingIdB64 );
22478 }
79+
22580 private Future <Void > verifyStatus (TestContext context , String pq , int status ) {
22681 return verifyStatus (context , pq , status , INTERNAL_TEST_KEY );
22782 }
@@ -232,15 +87,18 @@ private Future<Void> verifyStatus(TestContext context, String pq, int status, St
23287 int port = Const .Port .ServicePortForOptOut ;
23388 vertx .createHttpClient ()
23489 .request (HttpMethod .GET , port , "127.0.0.1" , pq )
235- .compose (req -> req
236- .putHeader ("Authorization" , "Bearer " + token )
237- .send ()
238- .compose (resp -> {
239- context .assertEquals (status , resp .statusCode ());
240- async .complete ();
241- promise .complete ();
242- return resp .body ();
243- }));
90+ .compose (req -> {
91+ if (token != null ) {
92+ req .putHeader ("Authorization" , "Bearer " + token );
93+ }
94+ return req .send ()
95+ .compose (resp -> {
96+ context .assertEquals (status , resp .statusCode ());
97+ async .complete ();
98+ promise .complete ();
99+ return resp .body ();
100+ });
101+ });
244102 return promise .future ();
245103 }
246104}
0 commit comments