@Test public void enable_strict_order_of_arrays() { expectedException.expect(AssertionError.class); assertJson("[1,2]").withStrictArrayOrder().isSimilarTo("[2, 1]"); }
public static JsonAssert assertJson(URL actualJson) { return new JsonAssert(urlToString(actualJson)); }
@Test public void ignore_fields() { assertJson("{\"foo\": \"bar\"}") .ignoreFields("ignore-me") .isSimilarTo("{\"foo\": \"bar\", \"ignore-me\": \"value\"}"); } }
public JsonAssert isSimilarTo(URL expected) { return isSimilarTo(urlToString(expected)); }
@Test public void return_false_when_jwt_throws_unauthorized_exception() throws Exception { doThrow(AuthenticationException.class).when(jwtHttpHandler).validateToken(request, response); when(basicAuthentication.authenticate(request)).thenReturn(Optional.empty()); underTest.doFilter(request, response, chain); verify(response).setContentType(MediaTypes.JSON); JsonAssert.assertJson(stringWriter.toString()).isSimilarTo("{\"valid\":false}"); }
@Test public void empty_result_without_views() { db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "AU-Tpxb--iU5OvuD2FLy", "AU-TpxcA-iU5OvuD2FLz", "AU-TpxcA-iU5OvuD2FLx"); String result = newRequest(underTestWithoutViews).execute().getInput(); assertJson(result) .withStrictArrayOrder() .ignoreFields("permissions") .isSimilarTo("{" + " \"permissionTemplates\": []," + " \"defaultTemplates\": [" + " {" + " \"templateId\": \"AU-Tpxb--iU5OvuD2FLy\"," + " \"qualifier\": \"TRK\"" + " }" + " ]" + "}"); }
public static JsonAssert assertJson(String actualJson) { return new JsonAssert(actualJson); }
@Test public void return_true_when_no_jwt_nor_basic_auth_and_no_force_authentication() throws Exception { settings.setProperty("sonar.forceAuthentication", "false"); when(jwtHttpHandler.validateToken(request, response)).thenReturn(Optional.empty()); when(basicAuthentication.authenticate(request)).thenReturn(Optional.empty()); underTest.doFilter(request, response, chain); verify(response).setContentType(MediaTypes.JSON); JsonAssert.assertJson(stringWriter.toString()).isSimilarTo("{\"valid\":true}"); }
@Test public void empty_result() { db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "AU-Tpxb--iU5OvuD2FLy", "AU-Tpxb--iU5OvuD2FLz", "AU-TpxcA-iU5OvuD2FLx"); String result = newRequest(wsTester).execute().getInput(); assertJson(result) .withStrictArrayOrder() .ignoreFields("permissions") .isSimilarTo("{" + " \"permissionTemplates\": []," + " \"defaultTemplates\": [" + " {" + " \"templateId\": \"AU-Tpxb--iU5OvuD2FLy\"," + " \"qualifier\": \"TRK\"" + " }," + " {" + " \"templateId\": \"AU-Tpxb--iU5OvuD2FLz\"," + " \"qualifier\": \"APP\"" + " }," + " {" + " \"templateId\": \"AU-TpxcA-iU5OvuD2FLx\"," + " \"qualifier\": \"VW\"" + " }" + " ]" + "}"); }
public JsonAssert isSimilarTo(URL expected) { return isSimilarTo(urlToString(expected)); }
public static JsonAssert assertJson(String actualJson) { return new JsonAssert(actualJson); }
@Test public void empty_array_is_returned_when_there_is_no_upgrade_available() throws Exception { underTest.handle(request, response); assertJson(response.outputAsString()).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_UPGRADE_LIST); }
@Test public void branch_support() { init(); branchFeature.setEnabled(true); assertJson(call()).isSimilarTo("{\"branchesEnabled\":true}"); branchFeature.setEnabled(false); assertJson(call()).isSimilarTo("{\"branchesEnabled\":false}"); }
private void verifyResult(String json, String expectedJsonFile) { assertJson(json).ignoreFields("id").isSimilarTo(getClass().getResource(getClass().getSimpleName() + "/" + expectedJsonFile)); } }
@Test public void template_groups_of_json_example() { GroupDto adminGroup = insertGroupOnDefaultOrganization("sonar-administrators", "System administrators"); GroupDto userGroup = insertGroupOnDefaultOrganization("sonar-users", "Any new users created will automatically join this group"); PermissionTemplateDto template = addTemplateToDefaultOrganization(); addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), adminGroup.getId())); addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), userGroup.getId())); // Anyone group addGroupToTemplate(newPermissionTemplateGroup(USER, template.getId(), null)); addGroupToTemplate(newPermissionTemplateGroup(ISSUE_ADMIN, template.getId(), null)); commit(); loginAsAdmin(db.getDefaultOrganization()); String response = newRequest() .setParam(PARAM_PERMISSION, ISSUE_ADMIN) .setParam(PARAM_TEMPLATE_ID, template.getUuid()) .execute() .getInput(); assertJson(response) .ignoreFields("id") .withStrictArrayOrder() .isSimilarTo(getClass().getResource("template_groups-example.json")); }
public static JsonAssert assertJson(URL actualJson) { return new JsonAssert(urlToString(actualJson)); }
@Test public void empty_array_is_returned_when_there_is_not_plugin_installed() { String response = tester.newRequest().execute().getInput(); assertJson(response).withStrictArrayOrder().isSimilarTo(JSON_EMPTY_PLUGIN_LIST); }
private void test(List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest, String expected) { StringWriter output = new StringWriter(); JsonWriter jsonWriter = JsonWriter.of(output); ProtobufJsonFormat.write(underTest.build(db.getSession(), blocks, branch, pullRequest), jsonWriter); JsonAssert.assertJson(output.toString()).isSimilarTo(expected); }