private GlobalAnalysisMode createMode(String key, String value) { Map<String, String> map = new HashMap<>(); if (key != null) { map.put(key, value); } ScannerProperties props = new ScannerProperties(map); return new GlobalAnalysisMode(props); } }
@Override protected void doBeforeStart() { ScannerProperties scannerProps = new ScannerProperties(scannerProperties); GlobalAnalysisMode globalMode = new GlobalAnalysisMode(scannerProps); add(scannerProps); add(globalMode); addBootstrapComponents(); }
@Test public void build_singleton() { System2 system = mock(System2.class); ScannerProperties settings = new ScannerProperties(new HashMap<>()); ScannerWsClient first = underTest.provide(settings, env, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), system); ScannerWsClient second = underTest.provide(settings, env, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), system); assertThat(first).isSameAs(second); } }
@Test public void provide_client_with_custom_settings() { Map<String, String> props = new HashMap<>(); props.put("sonar.host.url", "https://here/sonarqube"); props.put("sonar.login", "theLogin"); props.put("sonar.password", "thePassword"); props.put("sonar.ws.timeout", "42"); ScannerProperties settings = new ScannerProperties(props); ScannerWsClient client = underTest.provide(settings, env, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), mock(System2.class)); assertThat(client).isNotNull(); HttpConnector httpConnector = (HttpConnector) client.wsConnector(); assertThat(httpConnector.baseUrl()).isEqualTo("https://here/sonarqube/"); assertThat(httpConnector.okHttpClient().proxy()).isNull(); }
@Test public void log_and_profile_request_if_debug_level() { WsRequest request = newRequest(); WsResponse response = newResponse().setRequestUrl("https://local/api/issues/search"); when(wsClient.wsConnector().call(request)).thenReturn(response); logTester.setLevel(LoggerLevel.DEBUG); ScannerWsClient underTest = new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))); WsResponse result = underTest.call(request); // do not fail the execution -> interceptor returns the response assertThat(result).isSameAs(response); // check logs List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG); assertThat(debugLogs).hasSize(1); assertThat(debugLogs.get(0)).contains("GET 200 https://local/api/issues/search | time="); }
@Test public void fail_if_requires_credentials() { expectedException.expect(MessageException.class); expectedException .expectMessage("Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties sonar.login and sonar.password."); WsRequest request = newRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_credentials_are_not_valid() { expectedException.expect(MessageException.class); expectedException.expectMessage("Not authorized. Please check the properties sonar.login and sonar.password."); WsRequest request = newRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, /* credentials are configured */true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_requires_permission() { expectedException.expect(MessageException.class); expectedException.expectMessage("You're not authorized to run analysis. Please contact the project administrator."); WsRequest request = newRequest(); WsResponse response = newResponse() .setCode(403); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_bad_request() { expectedException.expect(MessageException.class); expectedException.expectMessage("Boo! bad request! bad!"); WsRequest request = newRequest(); WsResponse response = newResponse() .setCode(400) .setContent("{\"errors\":[{\"msg\":\"Boo! bad request! bad!\"}]}"); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Before public void setUp() throws Exception { HttpConnector connector = HttpConnector.newBuilder().url(server.url("/").toString()).build(); GlobalAnalysisMode analysisMode = new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())); ScannerWsClient wsClient = new ScannerWsClient(WsClientFactories.getDefault().newClient(connector), false, analysisMode); userHome = temp.newFolder(); MapSettings settings = new MapSettings(); settings.setProperty("sonar.userHome", userHome.getAbsolutePath()); underTest = new PluginFiles(wsClient, settings.asConfig()); }
@Override protected void doBeforeStart() { ScannerProperties scannerProps = new ScannerProperties(scannerProperties); GlobalAnalysisMode globalMode = new GlobalAnalysisMode(scannerProps); add(scannerProps); add(globalMode); addBootstrapComponents(); }