APISonar


org.sonarqube.ws.MediaTypes.JSON

> org > sonarqube > ws > MediaTypes > JSON
org APIs sonarqube APIs ws APIs MediaTypes APIs JSON APIs

Example 1
public static MockWsResponse createJson(String json) {
    return new MockWsResponse()
      .setContentType(MediaTypes.JSON)
      .setContentType(json);
  }
Example 2
public void json_example() {
    logInAsSystemAdministrator();

    String response = ws.newRequest()
      .setMediaType(MediaTypes.JSON)
      .setParam(PARAM_LOGIN, GRACE_HOPPER)
      .setParam(PARAM_NAME, TOKEN_NAME)
      .execute().getInput();

    assertJson(response).isSimilarTo(getClass().getResource("generate-example.json"));
  }
Example 3
public void test_project_with_zero_branches() {
    ComponentDto project = db.components().insertPrivateProject();
    userSession.logIn().addProjectPermission(USER, project);

    String json = ws.newRequest()
      .setParam("project", project.getDbKey())
      .setMediaType(MediaTypes.JSON)
      .execute()
      .getInput();
    assertJson(json).isSimilarTo("{\"branches\": []}");
  }
Example 4
public void test_json() throws Exception {
    logInAsQProfileAdministrator(dbTester.getDefaultOrganization());

    TestResponse response = wsTester.newRequest()
      .setMethod("POST")
      .setMediaType(MediaTypes.JSON)
      .setParam("language", XOO_LANGUAGE)
      .setParam("name", "Yeehaw!")
      .execute();

    JsonAssert.assertJson(response.getInput()).isSimilarTo(getClass().getResource("CreateActionTest/test_json.json"));
    assertThat(response.getMediaType()).isEqualTo(MediaTypes.JSON);
  }
Example 5
public void json_example() {
    UserDto user1 = db.users().insertUser(u -> u.setLogin("grace.hopper"));
    UserDto user2 = db.users().insertUser(u -> u.setLogin("ada.lovelace"));
    logInAsSystemAdministrator();

    String response = ws.newRequest()
      .setMediaType(MediaTypes.JSON)
      .setParam(PARAM_LOGIN, user1.getLogin())
      .setParam(PARAM_NAME, TOKEN_NAME)
      .execute().getInput();

    assertJson(response).ignoreFields("createdAt").isSimilarTo(getClass().getResource("generate-example.json"));
  }
Example 6
public void test_json_example() {
    insertDefaultGroup(db.getDefaultOrganization(), "users", 17);
    insertGroup(db.getDefaultOrganization(), "administrators", 2);
    loginAsDefaultOrgAdmin();

    String response = ws.newRequest().setMediaType(MediaTypes.JSON).execute().getInput();

    assertJson(response).ignoreFields("id").isSimilarTo(ws.getDef().responseExampleAsString());
  }
Example 7
public void should_not_fail_on_no_importers() throws Exception {
    CreateAction underTest = new CreateAction(db.getDbClient(), new QProfileFactory(deprecatedDbClient), null, LanguageTesting.newLanguages("xoo"), userSessionRule);
    WsActionTester wsTester = new WsActionTester(underTest);

    userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);

    TestResponse response = wsTester.newRequest()
      .setMethod("POST")
      .setMediaType(MediaTypes.JSON)
      .setParam("language", "xoo")
      .setParam("name", "Yeehaw!")
      .execute();
    JsonAssert.assertJson(response.getInput()).isSimilarTo(getClass().getResource("CreateActionTest/create-no-importer.json"));
    assertThat(response.getMediaType()).isEqualTo(MediaTypes.JSON);
  }
Example 8
public void add_part() throws IOException {
    PostRequest request = new PostRequest("api/issues/search");
    File reportFile = temp.newFile();
    request.setPart("report", new PostRequest.Part(MediaTypes.JSON, reportFile));

    assertThat(request.getParts()).hasSize(1);
    PostRequest.Part part = request.getParts().get("report");
    assertThat(part.getMediaType()).isEqualTo(MediaTypes.JSON);
    assertThat(part.getFile()).isSameAs(reportFile);
  }
Example 9
public void call_request_with_defaults() throws Exception {
    // no parameters, no media type
    WsRequest wsRequest = new GetRequest("api/issues/search");
    answer(new DumbLocalResponse(200, MediaTypes.JSON, "".getBytes(UTF_8), Collections.<String>emptyList()));

    WsResponse wsResponse = underTest.call(wsRequest);

    verifyRequested("GET", "api/issues/search", MediaTypes.JSON, Collections.<String, String>emptyMap());
    assertThat(wsResponse.code()).isEqualTo(200);
    assertThat(wsResponse.content()).isEqualTo("");
    assertThat(IOUtils.toString(wsResponse.contentReader())).isEqualTo("");
    assertThat(IOUtils.toString(wsResponse.contentStream())).isEqualTo("");
    assertThat(wsResponse.contentType()).isEqualTo(MediaTypes.JSON);
  }
Example 10
private String executeJsonRequest(@Nullable Integer page, @Nullable Integer pageSize, String... keys) {
    TestRequest request = wsTester.newRequest()
      .setMediaType(MediaTypes.JSON);
    populateRequest(request, page, pageSize, keys);
    return request.execute().getInput();
  }
Example 11
public void media_type_taken_in_url_first() throws Exception {
    ServletRequest request = new ServletRequest(source, Collections.<String, Object>emptyMap());
    when(source.getHeader(HttpHeaders.ACCEPT)).thenReturn(MediaTypes.JSON);
    when(source.getRequestURI()).thenReturn("/path/to/resource/search.protobuf");
    assertThat(request.getMediaType()).isEqualTo(MediaTypes.PROTOBUF);
  }
Example 12
public void support_json_response() {
    userSession.setGlobalPermissions(UserRole.ADMIN);
    TestResponse wsResponse = tester.newRequest()
      .setMediaType(MediaTypes.JSON)
      .execute();

    JsonAssert.assertJson(wsResponse.getInput()).isSimilarTo("{\"tasks\":[]}");
  }
Example 13
public void verify_response_example() throws URISyntaxException, IOException {
    OrganizationDto organization = db.organizations().insertForKey("my-org-1");
    userSession.addPermission(ADMINISTER, organization);
    db.components().insertComponents(
      newProjectDto(organization, "project-uuid-1").setName("Project Name 1").setKey("project-key-1"),
      newProjectDto(organization, "project-uuid-2").setName("Project Name 1").setKey("project-key-2"));

    String response = ws.newRequest()
      .setMediaType(MediaTypes.JSON)
      .setParam(PARAM_ORGANIZATION, organization.getKey())
      .execute().getInput();
    assertJson(response).isSimilarTo(ws.getDef().responseExampleAsString());
  }
Example 14
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();
  }
Example 15
public void not_fail_on_queue_task_not_linked_on_project_with_system_admin_permissions() {
    userSession.login("john").setGlobalPermissions(SYSTEM_ADMIN);

    CeQueueDto queueDto = new CeQueueDto();
    queueDto.setTaskType("fake");
    queueDto.setUuid("TASK_1");
    queueDto.setStatus(CeQueueDto.Status.PENDING);
    dbTester.getDbClient().ceQueueDao().insert(dbTester.getSession(), queueDto);
    dbTester.commit();

    ws.newRequest()
      .setMediaType(MediaTypes.JSON)
      .setParam("id", "TASK_1")
      .execute();
  }
Example 16
public AlmSettings.GetBindingWsResponse getBinding(GetBindingRequest request) {
    return call(
      new GetRequest(path("get_binding"))
        .setParam("project", request.getProject())
        .setMediaType(MediaTypes.JSON),
      AlmSettings.GetBindingWsResponse.parser());
  }
Example 17
public AlmSettings.CountBindingWsResponse countBinding(CountBindingRequest request) {
    return call(
      new GetRequest(path("count_binding"))
        .setParam("almSetting", request.getAlmSetting())
        .setMediaType(MediaTypes.JSON),
      AlmSettings.CountBindingWsResponse.parser());
  }
Example 18
public void write_json_by_default() throws Exception {
    TestRequest request = new TestRequest();
    DumbResponse response = new DumbResponse();

    Issues.Issue msg = Issues.Issue.newBuilder().setKey("I1").build();
    WsUtils.writeProtobuf(msg, request, response);

    assertThat(response.stream().mediaType()).isEqualTo(MediaTypes.JSON);
    assertThat(response.outputAsString())
      .startsWith("{")
      .contains("\"key\":\"I1\"")
      .endsWith("}");
  }
Example 19
private void executeAndVerify(@Nullable String componentKey, @Nullable String key, String expectedFile) {
    TestRequest request = ws.newRequest().setMediaType(MediaTypes.JSON);
    if (key != null) {
      request.setParam("id", key);
    }
    if (componentKey != null) {
      request.setParam("resource", componentKey);
    }
    String result = request.execute().getInput();
    assertJson(result).isSimilarTo(resource(expectedFile));
  }
Example 20
public void test_example_json_response() {
    when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), Matchers.isNull(String.class), eq("My Project"),
      anyMapOf(String.class, String.class), any(InputStream.class))).thenReturn(A_CE_TASK);

    TestResponse wsResponse = tester.newRequest()
      .setParam("projectKey", "my_project")
      .setParam("projectName", "My Project")
      .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar")
      .setMediaType(MediaTypes.JSON)
      .setMethod("POST")
      .execute();

    JsonAssert.assertJson(tester.getDef().responseExampleAsString()).isSimilarTo(wsResponse.getInput());
  }
Example 21
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    boolean isAuthenticated = authenticate(request, response);
    response.setContentType(MediaTypes.JSON);

    try (JsonWriter jsonWriter = JsonWriter.of(response.getWriter())) {
      jsonWriter.beginObject();
      jsonWriter.prop("valid", isAuthenticated);
      jsonWriter.endObject();
    }
  }
Example 22
public void test_json_example() {
    UserDto user = insertUser();
    GroupDto usersGroup = insertGroup("sonar-users", "Sonar Users");
    insertGroup("sonar-admins", "Sonar Admins");
    addUserToGroup(user, usersGroup);

    String response = ws.newRequest()
      .setMediaType(MediaTypes.JSON)
      .setParam("login", USER_LOGIN)
      .setParam(Param.SELECTED, ALL.value())
      .setParam(Param.PAGE_SIZE, "25")
      .setParam(Param.PAGE, "1")
      .execute().getInput();
    assertJson(response).ignoreFields("id").isSimilarTo(ws.getDef().responseExampleAsString());
  }
Example 23
private String executeJsonRequest(@Nullable String name, @Nullable String key, @Nullable String description, @Nullable String url, @Nullable String avatar) {
    TestRequest request = wsTester.newRequest()
      .setMediaType(MediaTypes.JSON);
    populateRequest(name, key, description, url, avatar, request);
    return request.execute().getInput();
  }
Example 24
public void addUserToTemplate(AddUserToTemplateRequest request) {
    call(
      new PostRequest(path("add_user_to_template"))
        .setParam("login", request.getLogin())
        .setParam("organization", request.getOrganization())
        .setParam("permission", request.getPermission())
        .setParam("templateId", request.getTemplateId())
        .setParam("templateName", request.getTemplateName())
        .setMediaType(MediaTypes.JSON)
      ).content();
  }
Example 25
public void test_response_example() {
    logInAsRoot();
    UserDto user = UserTesting.newUserDto().setLogin("daniel").setName("Daniel").setEmail("daniel@corp.com");
    UserDto rootDto = userDao.insert(dbSession, user);
    userDao.setRoot(dbSession, rootDto.getLogin(), true);
    dbSession.commit();

    TestResponse response = wsTester.newRequest().setMediaType(MediaTypes.JSON).execute();
    assertJson(response.getInput()).isSimilarTo(wsTester.getDef().responseExampleAsString());
  }