private static <T> T[] nullSafeAny() { return ArgumentMatchers.argThat( argument -> true ); }
private void expectWritten(List<ScannerReport.ContextProperty> expected) { verify(writer).writeContextProperties(argThat(props -> { List<ScannerReport.ContextProperty> copy = Lists.newArrayList(props); copy.removeAll(expected); return copy.isEmpty(); })); }
private void enqueueNotFoundDownload(String pluginKey, String pluginHash) { doReturn(Optional.empty()).when(pluginFiles).get(argThat(p -> pluginKey.equals(p.key) && pluginHash.equals(p.hash))); } }
private void enqueueDownload(String pluginKey, String pluginHash) throws IOException { File jar = temp.newFile(); Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); manifest.getMainAttributes().putValue("Plugin-Key", pluginKey); try (JarOutputStream output = new JarOutputStream(FileUtils.openOutputStream(jar), manifest)) { } doReturn(Optional.of(jar)).when(pluginFiles).get(argThat(p -> pluginKey.equals(p.key) && pluginHash.equals(p.hash))); }
public static void mockReader(ScannerWsClient mock, String path, Reader reader, Reader... others) { WsResponse response = mock(WsResponse.class); when(response.contentReader()).thenReturn(reader); WsResponse[] otherResponses = new WsResponse[others.length]; for (int i = 0; i < others.length; i++) { WsResponse otherResponse = mock(WsResponse.class); when(otherResponse.contentReader()).thenReturn(others[i]); otherResponses[i] = otherResponse; } when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response, otherResponses); }
public static void mockStream(ScannerWsClient mock, String path, InputStream is) { WsResponse response = mock(WsResponse.class); when(response.contentStream()).thenReturn(is); when(mock.call(argThat(new RequestMatcher(path)))).thenReturn(response); }
private HandlerMapping createHandlerMapping(MockHttpServletRequest request, Method requestMappingMethod) throws Exception { System.out.println("createHandlerMapping" + request); HandlerMapping requestMappingHandlerMapping = mock(HandlerMapping.class); HandlerExecutionChain handlerExecutionChain = mock(HandlerExecutionChain.class); HandlerMethod handlerMethod = mock(HandlerMethod.class); when(handlerMethod.getMethod()).thenReturn(requestMappingMethod); doReturn(TestController.class).when(handlerMethod).getBeanType(); when(handlerExecutionChain.getHandler()).thenReturn(handlerMethod); when(requestMappingHandlerMapping.getHandler(ArgumentMatchers.argThat(item -> item.getRequestURI().equals("/test/requestName")))).thenReturn(handlerExecutionChain); return requestMappingHandlerMapping; }
public static void mockException(ScannerWsClient mock, String path, Exception e) { when(mock.call(argThat(new RequestMatcher(path)))).thenThrow(e); }
public static void verifyCall(ScannerWsClient mock, String path) { verify(mock).call(argThat(new RequestMatcher(path))); }
@Test public void transferFailure_Throwable() { @SuppressWarnings("unchecked") final AsyncMethodCallback<String> callback = mock(AsyncMethodCallback.class); AsyncMethodCallbacks.transfer(exceptionallyCompletedFuture(new Throwable("foo")), callback); verify(callback, only()).onError(argThat(argument -> { return argument instanceof CompletionException && "foo".equals(argument.getCause().getMessage()); })); }
@Test public void testRegionserverNoRegions() throws Exception { runRegionserverCanary(); verify(mockAppender).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() { @Override public boolean matches(LoggingEvent argument) { return argument.getRenderedMessage().contains("Regionserver not serving any regions"); } })); }
@Test public void bind_to_specific_address() { Properties p = new Properties(); p.setProperty("sonar.web.port", "9000"); p.setProperty("sonar.web.host", "1.2.3.4"); TomcatConnectors.configure(tomcat, new Props(p)); verify(tomcat.getService()) .addConnector(argThat(c -> c.getScheme().equals("http") && c.getPort() == 9000 && ((InetAddress) c.getProperty("address")).getHostAddress().equals("1.2.3.4"))); }
@Test public void onGlobalPropertyChange() { GlobalPropertyChangeHandler handler = mock(GlobalPropertyChangeHandler.class); SettingsChangeNotifier notifier = new SettingsChangeNotifier(new GlobalPropertyChangeHandler[] {handler}); notifier.onGlobalPropertyChange("foo", "bar"); verify(handler).onChange(argThat(change -> change.getKey().equals("foo") && change.getNewValue().equals("bar"))); }
@Test public void bind_to_all_addresses_by_default() { Properties p = new Properties(); p.setProperty("sonar.web.port", "9000"); TomcatConnectors.configure(tomcat, new Props(p)); verify(tomcat.getService()).addConnector(argThat(c -> c.getScheme().equals("http") && c.getPort() == 9000 && ((InetAddress) c.getProperty("address")).getHostAddress().equals("0.0.0.0"))); }
@Test public void testRegionserverWithRegions() throws Exception { final TableName tableName = TableName.valueOf(name.getMethodName()); testingUtility.createTable(tableName, new byte[][] { FAMILY }); runRegionserverCanary(); verify(mockAppender, never()).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() { @Override public boolean matches(LoggingEvent argument) { return argument.getRenderedMessage().contains("Regionserver not serving any regions"); } })); }
private void verifyQueueSubmit(ComponentDto project, ComponentDto branch, UserDto user, Map<String, String> characteristics, String taskUuid) { verify(queue).submit(argThat(submit -> submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponent().filter(cpt -> cpt.getUuid().equals(branch.uuid()) && cpt.getMainComponentUuid().equals(project.uuid())).isPresent() && submit.getSubmitterUuid().equals(user.getUuid()) && submit.getCharacteristics().equals(characteristics) && submit.getUuid().equals(taskUuid))); }
@Test public void httpNotOk() { reader.onSubscribe(subscription); verifyZeroInteractions(subscription); reader.onNext(HttpHeaders.of(HttpStatus.UNAUTHORIZED)); verifyZeroInteractions(subscription); verifyZeroInteractions(deframer); verify(transportStatusListener).transportReportStatus( argThat(s -> s.getCode() == Status.UNAUTHENTICATED.getCode())); } }
@Test public void test_max_post_size_for_http_connection() { Properties properties = new Properties(); Props props = new Props(properties); TomcatConnectors.configure(tomcat, props); verify(tomcat.getService()).addConnector(argThat(c -> c.getMaxPostSize() == -1)); }
@Test public void download_from_url() { Plugin test = Plugin.factory("test"); Release test10 = new Release(test, "1.0").setDownloadUrl("http://server/test-1.0.jar"); test.addRelease(test10); when(updateCenter.findInstallablePlugins("foo", create("1.0"))).thenReturn(newArrayList(test10)); pluginDownloader.start(); pluginDownloader.download("foo", create("1.0")); // SONAR-4523: do not corrupt JAR files when restarting the server while a plugin is being downloaded. // The JAR file is downloaded in a temp file verify(httpDownloader).download(any(URI.class), argThat(new HasFileName("test-1.0.jar.tmp"))); assertThat(new File(downloadDir, "test-1.0.jar")).exists(); assertThat(new File(downloadDir, "test-1.0.jar.tmp")).doesNotExist(); }
@Test public void submit_a_report_on_existing_project() { ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization()); UserDto user = db.users().insertUser(); userSession.logIn(user).addProjectPermission(SCAN_EXECUTION, project); mockSuccessfulPrepareSubmitCall(); underTest.submit(defaultOrganizationKey, project.getDbKey(), null, project.name(), emptyMap(), IOUtils.toInputStream("{binary}", StandardCharsets.UTF_8)); verifyReportIsPersisted(TASK_UUID); verifyZeroInteractions(permissionTemplateService); verifyZeroInteractions(favoriteUpdater); verify(queue).submit(argThat(submit -> submit.getType().equals(CeTaskTypes.REPORT) && submit.getComponent().filter(cpt -> cpt.getUuid().equals(project.uuid()) && cpt.getMainComponentUuid().equals(project.uuid())).isPresent() && submit.getSubmitterUuid().equals(user.getUuid()) && submit.getUuid().equals(TASK_UUID))); }