/** * @param response HTTP response * @return response body * @throws IOException in case of any issue */ public static String checkResponse(Response response) throws IOException { String res = ""; if (response.body() != null) { res = response.body().asString(); } int code = response.getStatusCode(); if (code < 200 || code >= 300) { LOG.warn("{}", response.getStatusLine()); throw new RestException(code, res); } return res; }
@Test public void testDeleteStudentPhoneNumber() throws NoSuchFieldException { PhoneNumber phoneNumber = new PhoneNumber(null, 1l, Boolean.FALSE, "(123) 12 234 5678"); Response response = given() .headers(getAdminAuthHeaders()) .contentType("application/json") .body(phoneNumber) .post("/students/students/{STUDENTID}/phoneNumbers", TEST_STUDENT_ID); Long id = new Long(response.body().jsonPath().getInt("id")); response = given() .headers(getAuthHeaders()) .delete("/students/students/{STUDENTID}/phoneNumbers/{ID}", TEST_STUDENT_ID, id); assertOk(response, studentPermissions, StudentPermissions.DELETE_STUDENTPHONENUMBER, 204); if (response.getStatusCode() != 204) { given() .headers(getAdminAuthHeaders()) .delete("/students/students/{STUDENTID}/phoneNumbers/{ID}", TEST_STUDENT_ID, id); } }
@Test public void dataverseCategory() { Response enableNonPublicSearch = enableSetting(SettingsServiceBean.Key.SearchApiNonPublicAllowed); assertEquals(200, enableNonPublicSearch.getStatusCode()); /** * Unfortunately, it appears that the ability to specify the category of a dataverse when * creating it is a GUI-only feature. It can't currently be done via the API, to our knowledge. * You also can't tell from the API which category was persisted but it always seems to be * "UNCATEGORIZED" */ TestDataverse dataverseToCreate = new TestDataverse(dv1, "dv1", Dataverse.DataverseType.ORGANIZATIONS_INSTITUTIONS); Response createDvResponse = createDataverse(dataverseToCreate, homer); assertEquals(201, createDvResponse.getStatusCode()); TestSearchQuery query = new TestSearchQuery("dv1"); Response searchResponse = search(query, homer); JsonPath jsonPath = JsonPath.from(searchResponse.body().asString()); String dv1Category = jsonPath.get("data.facets." + SearchFields.DATAVERSE_CATEGORY).toString(); String msg = "dv1Category: " + dv1Category; assertEquals("dv1Category: [null]", msg); Response disableNonPublicSearch = deleteSetting(SettingsServiceBean.Key.SearchApiNonPublicAllowed); assertEquals(200, disableNonPublicSearch.getStatusCode()); }
private void assertMetacardsIngested(int expectedumberOfMetacards) throws Exception { String queryUrl = OPENSEARCH_PATH.getUrl() + "?q=*&format=xml&src=local"; Response response = when().get(queryUrl); String bodyXml = response.body().asString(); NodeChildren metacards = new XmlPath(bodyXml).get("metacards.metacard"); assertThat(metacards.size(), is(expectedumberOfMetacards)); }
private static JsonObject createUser(String jsonStr) { JsonObjectBuilder createdUser = Json.createObjectBuilder(); Response response = createUserViaApi(jsonStr, getPassword(jsonStr)); // System.out.println(response.prettyPrint()); Assert.assertEquals(200, response.getStatusCode()); JsonPath jsonPath = JsonPath.from(response.body().asString()); createdUser.add(idKey, jsonPath.getInt("data.user." + idKey)); createdUser.add(usernameKey, jsonPath.get("data.user." + usernameKey).toString()); createdUser.add(apiTokenKey, jsonPath.get("data." + apiTokenKey).toString()); return createdUser.build(); }
private List getFilesFromDatasetEndpoint(Integer datasetId, String apiToken) { List<Integer> fileList = new ArrayList<>(); Response getDatasetFilesResponse = given().get("api/datasets/" + datasetId + "/versions/:latest/files?key=" + apiToken); JsonPath jsonPath = JsonPath.from(getDatasetFilesResponse.body().asString()); // Integer fileId = jsonPath.get("data[0].datafile.id"); Map dataFiles = jsonPath.get("data[0]"); if (dataFiles != null) { Map datafile = (Map) dataFiles.get("datafile"); if (datafile != null) { Integer fileId = (Integer) datafile.get("id"); if (fileId != null) { fileList.add(fileId); } } } return fileList; }
/** * Assumes you have turned on experimental non-public search * https://github.com/IQSS/dataverse/issues/1299 * * <p>curl -X PUT -d true http://localhost:8080/api/admin/settings/:SearchApiNonPublicAllowed * * @return The Integer found or null. */ private static Integer findDatasetIdFromGlobalId(String globalId, String apiToken) { Response searchForGlobalId = given() .get( "api/search?key=" + apiToken + "&q=dsPersistentId:\"" + globalId.replace(":", "\\:") + "\"&show_entity_ids=true"); JsonPath jsonPath = JsonPath.from(searchForGlobalId.body().asString()); int id; try { id = jsonPath.get("data.items[0].entity_id"); } catch (IllegalArgumentException ex) { return null; } return id; }
@Test public void testCreateStudentPhoneNumberOwner() throws NoSuchFieldException { if (Role.STUDENT.name().equals(this.role)) { PhoneNumber phoneNumber = new PhoneNumber(null, 1l, Boolean.FALSE, "(123) 12 234 5678"); Response response = given() .headers(getAuthHeaders()) .contentType("application/json") .body(phoneNumber) .post("/students/students/{ID}/phoneNumbers", getUserIdForRole(this.getRole())); response.then().assertThat().statusCode(200); int id = response.body().jsonPath().getInt("id"); given() .headers(getAdminAuthHeaders()) .delete( "/students/students/{STUDENTID}/phoneNumbers/{ID}", getUserIdForRole(this.getRole()), id); } }
private long getRoleAssignmentId(Response response) { JsonPath jsonPath = JsonPath.from(response.body().asString()); return jsonPath.getInt("data.id"); }
private String getGlobalId(Response createDatasetResponse) { String xml = createDatasetResponse.body().asString(); String datasetSwordIdUrl = from(xml).get("entry.id"); /** @todo stop assuming the last 22 characters are the doi/globalId */ return datasetSwordIdUrl.substring(datasetSwordIdUrl.length() - 22); }
private static String getDataverseAlias(long dataverseId, String apiToken) { Response getDataverse = given().get("api/dataverses/" + dataverseId + "?key=" + apiToken); JsonPath jsonPath = JsonPath.from(getDataverse.body().asString()); String dataverseAlias = jsonPath.get("data.alias"); return dataverseAlias; }