Java Code Examples for io.restassured.path.json.JsonPath#getMap()

The following examples show how to use io.restassured.path.json.JsonPath#getMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 6 votes vote down vote up
@Test
@RunAsClient
@InSequence(11)
public void testBaseMetadataSingluarItems() {
    Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
    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());
}
 
Example 2
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 6 votes vote down vote up
@Test
@RunAsClient
@InSequence(15)
public void testBaseMetadataGarbageCollection() {
    Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
    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(".total") || name.endsWith(".time"));
            count++;
        }
    }
    assertThat(count, greaterThan(0));
}
 
Example 3
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 4
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 6 votes vote down vote up
@Test
@RunAsClient
@InSequence(31)
public void testOptionalBaseMetrics() {
    Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
    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 (MiniMeta item : names.values()) {
        if (elements.containsKey(item.toJSONName()) && names.get(item.name).optional) {
            String prefix = names.get(item.name).name;
            String type = "'"+item.toJSONName()+"'"+".type";
            String unit= "'"+item.toJSONName()+"'"+".unit";

            given().header(wantJson).options("/metrics/base/"+prefix).then().statusCode(200)
            .body(type, equalTo(names.get(item.name).type))
            .body(unit, equalTo(names.get(item.name).unit));
        }
    }

}
 
Example 5
Source File: DeploymentRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
private void verifyDeploymentWithDefinitionsValues(Deployment mockDeployment, String responseContent) {
  JsonPath path = from(responseContent);
  verifyStandardDeploymentValues(mockDeployment, path);

  Map<String, HashMap<String, Object>> deployedProcessDefinitions = path.getMap(PROPERTY_DEPLOYED_PROCESS_DEFINITIONS);
  Map<String, HashMap<String, Object>> deployedCaseDefinitions = path.getMap(PROPERTY_DEPLOYED_CASE_DEFINITIONS);
  Map<String, HashMap<String, Object>>  deployedDecisionDefinitions = path.getMap(PROPERTY_DEPLOYED_DECISION_DEFINITIONS);
  Map<String, HashMap<String, Object>>  deployedDecisionRequirementsDefinitions = path.getMap(PROPERTY_DEPLOYED_DECISION_REQUIREMENTS_DEFINITIONS);

  assertEquals(1, deployedProcessDefinitions.size());
  assertNotNull(deployedProcessDefinitions.get(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID));
  assertEquals(1, deployedCaseDefinitions.size());
  assertNotNull(deployedCaseDefinitions.get(EXAMPLE_CASE_DEFINITION_ID));
  assertEquals(1, deployedDecisionDefinitions.size());
  assertNotNull(deployedDecisionDefinitions.get(EXAMPLE_DECISION_DEFINITION_ID));
  assertEquals(1, deployedDecisionRequirementsDefinitions.size());
  assertNotNull(deployedDecisionRequirementsDefinitions.get(EXAMPLE_DECISION_REQUIREMENTS_DEFINITION_ID));
}
 
Example 6
Source File: DeploymentRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
private void verifyDrdDeploymentValues(Deployment mockDeployment, String responseContent) {
  JsonPath path = from(responseContent);
  verifyStandardDeploymentValues(mockDeployment, path);

  Map<String, HashMap<String, Object>>  deployedDecisionDefinitions =
    path.getMap(PROPERTY_DEPLOYED_DECISION_DEFINITIONS);
  Map<String, HashMap<String, Object>> deployedDecisionRequirementsDefinitions =
    path.getMap(PROPERTY_DEPLOYED_DECISION_REQUIREMENTS_DEFINITIONS);

  assertEquals(1, deployedDecisionDefinitions.size());
  HashMap decisionDefinitionDto = deployedDecisionDefinitions.get(EXAMPLE_DECISION_DEFINITION_ID);
  assertNotNull(decisionDefinitionDto);
  verifyDmnDeployment(decisionDefinitionDto);

  assertEquals(1, deployedDecisionRequirementsDefinitions.size());
  HashMap decisionRequirementsDefinitionDto = deployedDecisionRequirementsDefinitions.get(EXAMPLE_DECISION_REQUIREMENTS_DEFINITION_ID);
  assertNotNull(decisionRequirementsDefinitionDto);
  verifyDrdDeployment(decisionRequirementsDefinitionDto);

  assertNull(path.get(PROPERTY_DEPLOYED_PROCESS_DEFINITIONS));
  assertNull(path.get(PROPERTY_DEPLOYED_CASE_DEFINITIONS));
}
 
Example 7
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@Test
@RunAsClient
@InSequence(8)
public void testBaseSingularMetricsPresent() {
    Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
    Header wantJson = new Header("Accept", APPLICATION_JSON);

    JsonPath jsonPath = given().header(wantJson).get("/metrics/base").jsonPath();
    JsonPath filteredJSONPath = new JsonPath(jsonPath.prettify().replaceAll(JSON_APP_LABEL_REGEX, JSON_APP_LABEL_REGEXS_SUB));
    
    Map<String, Object> elements = filteredJSONPath.getMap(".");
    
    List<String> missing = new ArrayList<>();

    Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);       
    
    for (MiniMeta item : baseNames.values()) {
        if (item.name.startsWith("gc.")) {
            continue;
        }
            
        if (!elements.containsKey(item.toJSONName()) && !baseNames.get(item.name).optional) {
            missing.add(item.toJSONName());
        }
    }

    assertTrue("Following base items are missing: " + Arrays.toString(missing.toArray()), missing.isEmpty());
}
 
Example 8
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@Test
@RunAsClient
@InSequence(12)
public void testBaseMetadataTypeAndUnit() {
    Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
    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);

}
 
Example 9
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
@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);

}
 
Example 10
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
/**
 * Check that there is at least one metric named gc.total and that they all contain
 * expected tags (actually this is just 'name' for now).
 */
@Test
@RunAsClient
@InSequence(43)
public void testGcCountMetrics() {
    Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
    Header wantJson = new Header("Accept", APPLICATION_JSON);
    JsonPath jsonPath = given().header(wantJson).get("/metrics/base").jsonPath();

    Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
    MiniMeta gcCountMetricMeta = baseNames.get("gc.total");
    Set<String> expectedTags = gcCountMetricMeta.tags.keySet();

    // obtain list of actual base metrics from the runtime and find all named gc.total
    Map<String, Object> elements = jsonPath.getMap(".");
    boolean found = false;
    for (Map.Entry<String, Object> metricEntry : elements.entrySet()) {
        if(metricEntry.getKey().startsWith("gc.total")) {
            // We found a metric named gc.total. Now check that it contains all expected tags
            for(String expectedTag : expectedTags) {
                assertThat("The metric should contain a " + expectedTag + " tag",
                    metricEntry.getKey(), containsString(expectedTag + "="));
            }
            // check that the metric has a reasonable value - it should at least be numeric and not negative
            Assert.assertTrue("gc.total value should be numeric",
                metricEntry.getValue() instanceof Number);
            Assert.assertTrue("gc.total value should not be a negative number",
                (Integer)metricEntry.getValue() >= 0);
            found = true;
        }
    }
    Assert.assertTrue("At least one metric named gc.total is expected", found);
}
 
Example 11
Source File: MpMetricTest.java    From microprofile-metrics with Apache License 2.0 5 votes vote down vote up
/**
 * Check that there is at least one metric named gc.time and that they all contain
 * expected tags (actually this is just 'name' for now).
 */
@Test
@RunAsClient
@InSequence(44)
public void testGcTimeMetrics() {
    Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
    Header wantJson = new Header("Accept", APPLICATION_JSON);
    JsonPath jsonPath = given().header(wantJson).get("/metrics/base").jsonPath();

    Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
    MiniMeta gcTimeMetricMeta = baseNames.get("gc.time");
    Set<String> expectedTags = gcTimeMetricMeta.tags.keySet();

    // obtain list of actual base metrics from the runtime and find all named gc.time
    Map<String, Object> elements = jsonPath.getMap(".");
    boolean found = false;
    for (Map.Entry<String, Object> metricEntry : elements.entrySet()) {
        if(metricEntry.getKey().startsWith("gc.time")) {
            // We found a metric named gc.time. Now check that it contains all expected tags
            for(String expectedTag : expectedTags) {
                assertThat("The metric should contain a " + expectedTag + " tag",
                    metricEntry.getKey(), containsString(expectedTag + "="));
            }
            // check that the metric has a reasonable value - it should at least be numeric and not negative
            Assert.assertTrue("gc.time value should be numeric",
                metricEntry.getValue() instanceof Number);
            Assert.assertTrue("gc.time value should not be a negative number",
                (Integer)metricEntry.getValue() >= 0);
            found = true;
        }
    }
    Assert.assertTrue("At least one metric named gc.time is expected", found);
}
 
Example 12
Source File: DeploymentRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void verifyBpmnDeploymentValues(Deployment mockDeployment, String responseContent) {
  JsonPath path = from(responseContent);
  verifyStandardDeploymentValues(mockDeployment, path);

  Map<String, HashMap<String, Object>> deployedProcessDefinitionDtos = path.getMap(PROPERTY_DEPLOYED_PROCESS_DEFINITIONS);

  assertEquals(1, deployedProcessDefinitionDtos.size());
  HashMap processDefinitionDto = deployedProcessDefinitionDtos.get(MockProvider.EXAMPLE_PROCESS_DEFINITION_ID);
  assertNotNull(processDefinitionDto);
  verifyBpmnDeployment(processDefinitionDto);

  assertNull(path.get(PROPERTY_DEPLOYED_CASE_DEFINITIONS));
  assertNull(path.get(PROPERTY_DEPLOYED_DECISION_DEFINITIONS));
  assertNull(path.get(PROPERTY_DEPLOYED_DECISION_REQUIREMENTS_DEFINITIONS));
}
 
Example 13
Source File: DeploymentRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void verifyCmmnDeploymentValues(Deployment mockDeployment, String responseContent) {
  JsonPath path = from(responseContent);
  verifyStandardDeploymentValues(mockDeployment, path);

  Map<String, HashMap<String, Object>> deployedCaseDefinitions = path.getMap(PROPERTY_DEPLOYED_CASE_DEFINITIONS);

  assertEquals(1, deployedCaseDefinitions.size());
  HashMap caseDefinitionDto = deployedCaseDefinitions.get(EXAMPLE_CASE_DEFINITION_ID);
  assertNotNull(caseDefinitionDto);
  verifyCmnDeployment(caseDefinitionDto);

  assertNull(path.get(PROPERTY_DEPLOYED_PROCESS_DEFINITIONS));
  assertNull(path.get(PROPERTY_DEPLOYED_DECISION_DEFINITIONS));
  assertNull(path.get(PROPERTY_DEPLOYED_DECISION_REQUIREMENTS_DEFINITIONS));
}
 
Example 14
Source File: DeploymentRestServiceInteractionTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
private void verifyDmnDeploymentValues(Deployment mockDeployment, String responseContent) {
  JsonPath path = from(responseContent);
  verifyStandardDeploymentValues(mockDeployment, path);

  Map<String, HashMap<String, Object>> deployedDecisionDefinitions = path.getMap(PROPERTY_DEPLOYED_DECISION_DEFINITIONS);

  assertEquals(1, deployedDecisionDefinitions.size());
  HashMap decisionDefinitionDto = deployedDecisionDefinitions.get(EXAMPLE_DECISION_DEFINITION_ID);
  assertNotNull(decisionDefinitionDto);
  verifyDmnDeployment(decisionDefinitionDto);

  assertNull(path.get(PROPERTY_DEPLOYED_DECISION_REQUIREMENTS_DEFINITIONS));
  assertNull(path.get(PROPERTY_DEPLOYED_PROCESS_DEFINITIONS));
  assertNull(path.get(PROPERTY_DEPLOYED_CASE_DEFINITIONS));
}
 
Example 15
Source File: CoreMainTest.java    From camel-quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void testMainInstance() {
    JsonPath p = RestAssured.given()
            .accept(MediaType.APPLICATION_JSON)
            .get("/test/main/describe")
            .then()
            .statusCode(200)
            .extract()
            .body()
            .jsonPath();

    assertThat(p.getString("xml-loader")).isEqualTo(DisabledXMLRoutesDefinitionLoader.class.getName());
    assertThat(p.getString("xml-model-dumper")).isEqualTo(DisabledModelToXMLDumper.class.getName());

    assertThat(p.getString("routes-collector.type")).isEqualTo(CamelMainRoutesCollector.class.getName());
    assertThat(p.getString("routes-collector.type-registry")).isEqualTo(RegistryRoutesLoaders.Default.class.getName());
    assertThat(p.getString("routes-collector.type-xml")).isEqualTo(DisabledXMLRoutesDefinitionLoader.class.getName());

    assertThat(p.getList("listeners", String.class))
            .containsAnyOf(CamelMainEventBridge.class.getName(), CustomMainListener.class.getName());
    assertThat(p.getList("routeBuilders", String.class))
            .contains(CamelRoute.class.getName())
            .doesNotContain(CamelRouteFiltered.class.getName());
    assertThat(p.getList("routes", String.class))
            .contains("keep-alive", "configure", "beforeStart", "produced", "endpointdsl")
            .doesNotContain("filtered");

    assertThat(p.getString("lru-cache-factory")).isEqualTo(DefaultLRUCacheFactory.class.getName());
    assertThat(p.getBoolean("autoConfigurationLogSummary")).isFalse();

    assertThat(p.getMap("registry.components", String.class, String.class)).isNotEmpty();
    assertThat(p.getMap("registry.dataformats", String.class, String.class)).isEmpty();
    assertThat(p.getMap("registry.languages", String.class, String.class)).containsExactlyInAnyOrderEntriesOf(mapOf(
            "constant", "org.apache.camel.language.constant.ConstantLanguage",
            "file", "org.apache.camel.language.simple.FileLanguage",
            "header", "org.apache.camel.language.header.HeaderLanguage",
            "simple", "org.apache.camel.language.simple.SimpleLanguage",
            "ref", "org.apache.camel.language.ref.RefLanguage"));

    Map<String, String> factoryFinderMap = p.getMap("factory-finder.class-map", String.class, String.class);

    // dataformats
    assertThat(factoryFinderMap)
            .hasKeySatisfying(startsWith("META-INF/services/org/apache/camel/dataformat/"))
            .hasEntrySatisfying(entry(
                    "META-INF/services/org/apache/camel/dataformat/my-dataformat",
                    "org.apache.camel.quarkus.it.support.dataformat.MyDataformat"));

    // languages
    assertThat(factoryFinderMap)
            .hasKeySatisfying(startsWith("META-INF/services/org/apache/camel/language/"))
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/language/constant"))
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/language/file"))
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/language/header"))
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/language/ref"))
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/language/simple"));

    // components
    assertThat(factoryFinderMap)
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/component/"));

    // misc
    assertThat(factoryFinderMap)
            .hasKeySatisfying(startsWith("META-INF/services/org/apache/camel/configurer/"));

    // core
    assertThat(factoryFinderMap)
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/properties-component-factory"))
            .hasKeySatisfying(doesNotStartWith("META-INF/services/org/apache/camel/reactive-executor"));

    // rest properties are configured through CamelContext, this test is to spot changes
    // done to underlying camel-main implementation that would prevent to configure it
    // consistently across runtimes using camel-main properties.
    assertThat(p.getString("config.rest-port")).isEqualTo("9876");

    // resilience4j properties are configured through ModelCamelContext, this test is
    // to ensure that FastCamelContext won't change in a way it cannot be configured
    // consistently across runtimes using camel-main properties.
    assertThat(p.getString("config.resilience4j-sliding-window-size")).isEqualTo("1234");
}
 
Example 16
Source File: RestAssuredJSONExamplesTest.java    From tracksrestcasestudy with MIT License 2 votes vote down vote up
@Test
public void aSetOfJsonPathExamples(){

    /*
        REST Assured documentation https://github.com/rest-assured/rest-assured/wiki/Usage
        
        JSON parsing https://github.com/rest-assured/rest-assured/wiki/Usage#json-example
        JsonPath - https://github.com/rest-assured/rest-assured/wiki/Usage#json-using-jsonpath

        JsonPath blog post with useful examples:
        https://blog.jayway.com/2013/04/12/whats-new-in-rest-assured-1-8/

        Deserialisation
        https://github.com/rest-assured/rest-assured/wiki/Usage#deserialization

     */
    File jsonExample = new File(System.getProperty("user.dir"),
            "src/test/resources/jsonxml/jsonexample.json");

    JsonPath jsonPath = new JsonPath(jsonExample);

    // multiple matches returned in an ArrayList
    ArrayList ret = jsonPath.get("projects.project");
    Assert.assertEquals(6, ret.size());

    // can index on multiple matches with array indexing notation [1]
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project[1].name"));
    Assert.assertEquals(3,jsonPath.getInt("projects.project[1].id"));

    // can count backwards so -1 is last, -2 is second to last
    Assert.assertEquals(10,jsonPath.getInt("projects.project[-1].id"));

    // filters and finding stuff
    // find the project with id == '3' and return the name
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project.find {it.id == 3}.name"));

    // use `findAll` to find all the items that match a condition
    // conditions can use Groovy so convert the id to an integer in the filter if it was a string with  `{it.id.toInteger() <= 6}`

    // use `it` in the filter condition to refer to currently matched item
    ArrayList projectsWithIdLessThanSix = jsonPath.get("projects.project.findAll {it.id <= 6}");
    Assert.assertEquals(4,projectsWithIdLessThanSix.size());



    // get an 'object' as a Map from the list
    Map<String, Object> project1map = jsonPath.getMap("projects.project[1]");
    Assert.assertEquals("active", project1map.get("state"));


    // set root to an element in the Json to make accessing it easier
    JsonPath project2 = new JsonPath(jsonExample).setRoot("projects.project[2]");
    Assert.assertEquals(5 ,project2.getInt("id"));
    Assert.assertEquals("active" ,project2.get("state"));




}