public static JsonPath jsonResponse(Response response) { return response.jsonPath(); } }
public static JsonPath jsonResponse(Response response) { return response.jsonPath(); } }
@Test @RunAsClient @InSequence(19) public void testApplicationMetadataItems() { Header wantJson = new Header("Accept", APPLICATION_JSON); JsonPath jsonPath = given().header(wantJson).options("/metrics/application").jsonPath(); Map<String, Object> elements = jsonPath.getMap("."); List<String> missing = new ArrayList<>(); Map<String, MiniMeta> names = getExpectedMetadataFromXmlFile(MetricRegistry.Type.APPLICATION); for (String item : names.keySet()) { if (!elements.containsKey(item)) { missing.add(item); } } assertTrue("Following application items are missing: " + Arrays.toString(missing.toArray()), missing.isEmpty()); }
@Test @RunAsClient @InSequence(8) public void testBaseSingularMetricsPresent() { Header wantJson = new Header("Accept", APPLICATION_JSON); JsonPath jsonPath = given().header(wantJson).get("/metrics/base").jsonPath(); Map<String, Object> elements = jsonPath.getMap("."); List<String> missing = new ArrayList<>(); Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE); for (String item : baseNames.keySet()) { if (item.startsWith("gc.")) { continue; } if (!elements.containsKey(item) && !baseNames.get(item).optional) { missing.add(item); } } assertTrue("Following base items are missing: " + Arrays.toString(missing.toArray()), missing.isEmpty()); }
@Test @RunAsClient @InSequence(11) public void testBaseMetadataSingluarItems() { Header wantJson = new Header("Accept", APPLICATION_JSON); JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath(); Map<String, Object> elements = jsonPath.getMap("."); List<String> missing = new ArrayList<>(); Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE); for (String item : baseNames.keySet()) { if (item.startsWith("gc.") || baseNames.get(item).optional) { continue; } if (!elements.containsKey(item)) { missing.add(item); } } assertTrue("Following base items are missing: " + Arrays.toString(missing.toArray()), missing.isEmpty()); }
@Test @RunAsClient @InSequence(15) public void testBaseMetadataGarbageCollection() throws Exception { Header wantJson = new Header("Accept", APPLICATION_JSON); JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath(); int count = 0; Map<String, Object> elements = jsonPath.getMap("."); for (String name : elements.keySet()) { if (name.startsWith("gc.")) { assertTrue(name.endsWith(".count") || name.endsWith(".time")); count++; } } assertThat(count, greaterThan(0)); }
/** * 获取本次请求的响应信息 * * @param response */ private String getResponseInfo(Response response) { // TODO - 此处容易抛异常 if (response.contentType().contains("json")) { return "[" + response.statusCode() + "]" + response.jsonPath().get(); } else { return "[" + response.statusCode() + "]" + response.htmlPath().get(); } }
@Test @RunAsClient @InSequence(31) public void testOptionalBaseMetrics() { Header wantJson = new Header("Accept", APPLICATION_JSON); JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath(); Map<String, Object> elements = jsonPath.getMap("."); Map<String, MiniMeta> names = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE); for (String item : names.keySet()) { if (elements.containsKey(item) && names.get(item).optional) { String prefix = names.get(item).name; String type = "\""+prefix+"\""+".type"; String unit= "\""+prefix+"\""+".unit"; given().header(wantJson).options("/metrics/base/"+prefix).then().statusCode(200) .body(type, equalTo(names.get(item).type)) .body(unit, equalTo(names.get(item).unit)); } } }
@Test @RunAsClient @InSequence(21) public void testApplicationTagJson() { JsonPath jsonPath = given().header("Accept", APPLICATION_JSON) .when() .options("/metrics/application/purple").jsonPath(); String tags = jsonPath.getString("purple.tags"); assertNotNull(tags); assertTrue(tags.contains("app=myShop")); assertTrue(tags.contains("tier=integration")); }
/** * 请求 * * @param testStep */ public Response request(TestStep testStep) { RestAssured.baseURI = baseURL; if (!testStep.getParams().isEmpty()) { response = request(testStep.getType(), testStep.getPath(), testStep.getParams()); } else { response = request(testStep.getType(), testStep.getPath()); } logger.info(getResponseInfo(response)); // TODO - 此处容易抛异常 if (response.contentType().contains("json")) { listenerUtils.testCaseResult.setDescription(response.jsonPath().get().toString()); } else { listenerUtils.testCaseResult.setDescription(response.htmlPath().get().toString()); } return response; } }
@Test @RunAsClient @InSequence(12) public void testBaseMetadataTypeAndUnit() { Header wantJson = new Header("Accept", APPLICATION_JSON); JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath(); Map<String, Map<String, Object>> elements = jsonPath.getMap("."); Map<String, MiniMeta> expectedMetadata = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE); checkMetadataPresent(elements, expectedMetadata); }
@Test @RunAsClient @InSequence(20) public void testApplicationMetadataTypeAndUnit() { Header wantJson = new Header("Accept", APPLICATION_JSON); JsonPath jsonPath = given().header(wantJson).options("/metrics/application").jsonPath(); Map<String, Map<String, Object>> elements = jsonPath.getMap("."); Map<String, MiniMeta> expectedMetadata = getExpectedMetadataFromXmlFile(MetricRegistry.Type.APPLICATION); checkMetadataPresent(elements, expectedMetadata); }
@Test public void testGetVersion() throws Exception { Response r = given().accept(APPLICATION_JSON_UTF8_VALUE) // .expect() // .statusCode(200).log().ifError() // .with().port(localServerPort) // .get(getVersionPrefix() + "/version"); r.then() // .statusCode(HttpStatus.OK.value()).log().ifError() // .contentType(APPLICATION_JSON_UTF8_VALUE); // We don't care what values are returned as long as they are filled. JsonPath jsonPathEvaluator = r.jsonPath(); assertThat(jsonPathEvaluator.get("version"), allOf(notNullValue(), not(equalTo(VersionDto.N_A)))); assertThat(jsonPathEvaluator.get("commit"), allOf(notNullValue(), not(equalTo(VersionDto.N_A)))); assertThat(jsonPathEvaluator.get("time"), allOf(notNullValue(), not(equalTo(VersionDto.N_A)))); } }