- setter.accept(originalValue);
- }
-
- @Test
- public void testStartPreprocessorAsync() {
- assertNotNull(oper.startPreprocessorAsync());
- }
-
- @Test
- public void testObtainVfCount_testGetVfCount_testSetVfCount() throws Exception {
- // insert CQ data so it's there for the check
- context.setProperty(AaiCqResponse.CONTEXT_KEY, makeCqResponse());
-
- // shouldn't actually need to do anything
- assertNull(oper.obtainVfCount());
-
- // verify that the count was stored
- Integer vfcount = context.getProperty(VF_COUNT_KEY);
- assertEquals(VF_COUNT, vfcount);
- assertEquals(VF_COUNT.intValue(), oper.getVfCount());
-
- // change the count and then verify that it isn't overwritten by another call
- oper.setVfCount(VF_COUNT + 1);
-
- assertNull(oper.obtainVfCount());
- vfcount = context.getProperty(VF_COUNT_KEY);
- assertEquals(VF_COUNT + 1, vfcount.intValue());
- assertEquals(VF_COUNT + 1, oper.getVfCount());
- }
-
- /**
- * Tests obtainVfCount() when it actually has to query.
- */
- @Test
- public void testObtainVfCountQuery() throws Exception {
- CompletableFuture<OperationOutcome> future2 = oper.obtainVfCount();
- assertNotNull(future2);
- assertTrue(executor.runAll(100));
-
- // not done yet
- assertFalse(future2.isDone());
-
- provideCqResponse(makeCqResponse());
-
- assertTrue(executor.runAll(100));
- assertTrue(future2.isDone());
- assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
-
- // verify that the count was stored
- Integer vfcount = context.getProperty(VF_COUNT_KEY);
- assertEquals(VF_COUNT, vfcount);
-
- // repeat - shouldn't need to do anything now
- assertNull(oper.obtainVfCount());
- }
-
- @Test
- public void testPostProcess() throws Exception {
- // completed
- outcome.setSubRequestId(null);
- CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
- assertTrue(future2.isDone());
- assertSame(outcome, future2.get());
- assertEquals(PolicyResult.SUCCESS, outcome.getResult());
- assertNotNull(outcome.getSubRequestId());
-
- // failed
- outcome.setSubRequestId(null);
- response.getRequest().getRequestStatus().setRequestState(SoOperation.FAILED);
- future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
- assertTrue(future2.isDone());
- assertSame(outcome, future2.get());
- assertEquals(PolicyResult.FAILURE, outcome.getResult());
- assertNotNull(outcome.getSubRequestId());
-
- // no request id in the response
- response.getRequestReferences().setRequestId(null);
- response.getRequest().getRequestStatus().setRequestState("unknown");
- assertThatIllegalArgumentException()
- .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
- .withMessage("missing request ID in response");
- response.getRequestReferences().setRequestId(REQ_ID.toString());
-
- // status = 500
- when(rawResponse.getStatus()).thenReturn(500);
-
- // null request reference
- SoRequestReferences ref = response.getRequestReferences();
- response.setRequestReferences(null);
- assertThatIllegalArgumentException()
- .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
- .withMessage("missing request ID in response");
- response.setRequestReferences(ref);
- }
-
- /**
- * Tests postProcess() when the "get" is repeated a couple of times.
- */
- @Test
- public void testPostProcessRepeated_testResetGetCount() throws Exception {
- /*
- * Two failures and then a success - should result in two "get" calls.
- *
- * Note: getStatus() is invoked twice during each call, so have to double up the
- * return values.
- */
- when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
-
- when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
-
- // use a real executor
- params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
-
- oper = new SoOperation(params, config) {
- @Override
- public long getWaitMsGet() {
- return 1;
- }
- };
-
- CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-
- assertSame(outcome, future2.get(500, TimeUnit.SECONDS));
- assertEquals(PolicyResult.SUCCESS, outcome.getResult());
- assertEquals(2, oper.getGetCount());
-
- /*
- * repeat - this time, the "get" operations will be exhausted, so it should fail
- */
- when(rawResponse.getStatus()).thenReturn(500);
-
- future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-
- assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
- assertEquals(PolicyResult.FAILURE_TIMEOUT, outcome.getResult());
- assertEquals(MAX_GETS + 1, oper.getGetCount());
-
- oper.resetGetCount();
- assertEquals(0, oper.getGetCount());