@Test public void testIsAvailableSubjectExperation() throws Exception { when(webclient.invoke(eq("HEAD"), isNull())).thenReturn(response); when(mockSecurity.getExpires(subject)) .thenReturn(new Date(System.currentTimeMillis() + 600000L)) .thenReturn(new Date(System.currentTimeMillis() + 600000L)) .thenReturn(new Date()); sendEvent.setSubject(subject); boolean available = false; while (!sendEvent.ping()) {} long lastPing = sendEvent.getLastPing(); // sleep incase the test runs too fast we want to make sure their is a time difference Thread.sleep(1); // run within the expiration period of the assertion available = sendEvent.ping(); assertTrue(available); assertEquals(lastPing, sendEvent.getLastPing()); // sleep incase the test runs too fast we want to make sure their is a time difference Thread.sleep(1); // run with expired assertion available = sendEvent.ping(); assertTrue(available); assertNotEquals(lastPing, sendEvent.getLastPing()); }
@Before public void setUp() throws Exception { System.setProperty("ddf.home", "."); callbackURI = new URL("https://localhost:12345/services/csw/subscription/event"); ObjectFactory objectFactory = new ObjectFactory(); request = new GetRecordsType(); request.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA); request.setResultType(ResultType.RESULTS); request.getResponseHandler().add(callbackURI.toString()); queryType = new QueryType(); elementSetNameType = new ElementSetNameType(); elementSetNameType.setValue(ElementSetType.BRIEF); queryType.setElementSetName(elementSetNameType); request.setAbstractQuery(objectFactory.createAbstractQuery(queryType)); transformerManager = mock(TransformerManager.class); transformer = mock(QueryResponseTransformer.class); binaryContent = mock(BinaryContent.class); when(transformerManager.getTransformerBySchema( Matchers.contains(CswConstants.CSW_OUTPUT_SCHEMA))) .thenReturn(transformer); when(transformer.transform(any(SourceResponse.class), anyMap())).thenReturn(binaryContent); when(binaryContent.getByteArray()).thenReturn("byte array with message contents".getBytes()); query = mock(QueryRequest.class); metacard = mock(Metacard.class); webclient = mock(WebClient.class); mockCxfClientFactory = mock(SecureCxfClientFactory.class); response = mock(Response.class); subject = mock(Subject.class); mockSecurity = mock(Security.class); headers.put(Subject.class.toString(), Arrays.asList(new Subject[] {subject})); AccessPlugin accessPlugin = mock(AccessPlugin.class); accessPlugins.add(accessPlugin); when(mockCxfClientFactory.getWebClient()).thenReturn(webclient); // when(webclient.head()).thenReturn(response); when(webclient.invoke(anyString(), any(QueryResponse.class))).thenReturn(response); when(response.getHeaders()).thenReturn(headers); when(accessPlugin.processPostQuery(any(QueryResponse.class))) .thenAnswer( new Answer<QueryResponse>() { @Override public QueryResponse answer(InvocationOnMock invocationOnMock) throws Throwable { return (QueryResponse) invocationOnMock.getArguments()[0]; } }); sendEvent = new SendEventExtension(transformerManager, request, query, mockCxfClientFactory); sendEvent.setSubject(subject); }
@Test public void testIsAvailableRetryBackoff() throws Exception { when(webclient.invoke(eq("HEAD"), isNull())).thenThrow(new RuntimeException("test")); sendEvent.setSubject(subject); long lastPing = sendEvent.getLastPing(); boolean available = true; // loop until we get to a backoff that will be long enough not to cause intermitent test // failures while (sendEvent.getRetryCount() < 7) { available = sendEvent.ping(); } assertFalse(available); assertNotEquals(lastPing, sendEvent.getLastPing()); lastPing = sendEvent.getLastPing(); Thread.sleep(1); // run again this time within a backoff period and verify that it doesn't retry this is available = sendEvent.ping(); assertFalse(available); assertEquals(7, sendEvent.getRetryCount()); assertEquals(lastPing, sendEvent.getLastPing()); }
@Test public void testIsAvailableNoExperation() throws Exception { long lastPing = sendEvent.getLastPing(); when(webclient.invoke(eq("HEAD"), isNull())).thenReturn(response); boolean available = false; while (!sendEvent.ping()) {} assertNotEquals(lastPing, sendEvent.getLastPing()); lastPing = sendEvent.getLastPing(); Thread.sleep(1); // run within the expiration period of the assertion available = sendEvent.ping(); assertTrue(available); assertEquals(lastPing, sendEvent.getLastPing()); }
@Test public void testDeleted() throws Exception { sendEvent.deleted(metacard); verifyResults(); }
@Test public void testUpdatedMiss() throws Exception { sendEvent.updatedMiss(metacard, metacard); verifyResults(); }