public static MockWsResponse createJson(String json) { return new MockWsResponse() .setContentType(MediaTypes.JSON) .setContentType(json); } }
public MockWsResponse setContent(InputStream is) { try { return setContent(IOUtils.toByteArray(is)); } catch (IOException e) { throw Throwables.propagate(e); } }
private MockWsResponse newResponse() { return new MockWsResponse().setRequestUrl("https://local/api/issues/search"); }
public void test() { GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC"); when(wsConnector.call(get)).thenReturn(new MockWsResponse().setCode(403).setRequestUrl("https://local/foo").setContent("error")); try { call(get, Testing.Fake.parser()); fail(); } catch (HttpException e) { assertThat(e.code()).isEqualTo(403); } }
private static WsResponse newProtobufFakeResponse() { Testing.Fake message = Testing.Fake.newBuilder().setLabel("ok").build(); return new MockWsResponse().setContent(message.toByteArray()); } }
@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); }
@Test public void test_call() throws Exception { new BaseService(wsConnector, "api/issues") { public void test() throws IOException { GetRequest get = new GetRequest(path("issue")).setMediaType(MediaTypes.JSON); when(wsConnector.call(get)).thenReturn(new MockWsResponse().setContent("ok")); WsResponse response = call(get); assertThat(response.content()).isEqualTo("ok"); } }.test(); }
@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); }
public void test() { GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC"); when(wsConnector.call(get)).thenReturn(MockWsResponse.createJson("{}").setRequestUrl("http://local/api/issues/issue?key=ABC")); try { call(get, Testing.Fake.parser()); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Fail to parse protobuf response of http://local/api/issues/issue?key=ABC"); } } }.test();
@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="); }
public void test() throws IOException { GetRequest get = new GetRequest(path("issue")).setMediaType(MediaTypes.JSON); when(wsConnector.call(get)).thenReturn(new MockWsResponse().setContent("ok")); WsResponse response = call(get); assertThat(response.content()).isEqualTo("ok"); }
@Test public void fail_if_http_error() { new BaseService(wsConnector, "api/issues") { public void test() { GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC"); when(wsConnector.call(get)).thenReturn(new MockWsResponse().setCode(403).setRequestUrl("https://local/foo").setContent("error")); try { call(get, Testing.Fake.parser()); fail(); } catch (HttpException e) { assertThat(e.code()).isEqualTo(403); } } }.test(); }
@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); }
public static MockWsResponse createJson(String json) { return new MockWsResponse() .setContentType(MediaTypes.JSON) .setContentType(json); } }
public MockWsResponse setContent(InputStream is) { try { return setContent(IOUtils.toByteArray(is)); } catch (IOException e) { throw Throwables.propagate(e); } }
@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); }