/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that is equal to the specified key. * <p/> * For example: * <pre>assertThat(myMap, hasKey("bar"))</pre> * * @param key * the key that satisfying maps must contain */ public static <K> org.hamcrest.Matcher<java.util.Map<? extends K, ?>> hasKey(K key) { return org.hamcrest.collection.IsMapContaining.<K>hasKey(key); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that satisfies the specified matcher. * <p/> * For example: * <pre>assertThat(myMap, hasKey(equalTo("bar")))</pre> * * @param keyMatcher * the matcher that must be satisfied by at least one key */ public static <K> org.hamcrest.Matcher<java.util.Map<? extends K, ?>> hasKey(org.hamcrest.Matcher<? super K> keyMatcher) { return org.hamcrest.collection.IsMapContaining.<K>hasKey(keyMatcher); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that satisfies the specified matcher. * For example: * <pre>assertThat(myMap, hasKey(equalTo("bar")))</pre> * * @param keyMatcher * the matcher that must be satisfied by at least one key */ public static <K> org.hamcrest.Matcher<java.util.Map<? extends K,?>> hasKey(org.hamcrest.Matcher<? super K> keyMatcher) { return org.hamcrest.collection.IsMapContaining.hasKey(keyMatcher); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that is equal to the specified key. * For example: * <pre>assertThat(myMap, hasKey("bar"))</pre> * * @param key * the key that satisfying maps must contain */ public static <K> org.hamcrest.Matcher<java.util.Map<? extends K,?>> hasKey(K key) { return org.hamcrest.collection.IsMapContaining.hasKey(key); }
@Override protected Matcher<?> createMatcher() { return hasKey("foo"); }
public void testMatchesMapContainingKeyWithIntegerKeys() throws Exception { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "A"); map.put(2, "B"); assertThat(map, hasKey(1)); }
public void testMatchesMapContainingKeyWithNumberKeys() throws Exception { Map<Number, String> map = new HashMap<Number, String>(); map.put(1, "A"); map.put(2, "B"); assertThat(map, hasKey((Number)1)); // TODO: work out the correct sprinkling of wildcards to get this to work! // assertThat(map, hasKey(1)); }
public void testMatchesSingletonMapContainingKey() { Map<String,Integer> map = new HashMap<String, Integer>(); map.put("a", 1); assertMatches("Matches single key", hasKey("a"), map); }
public void testDoesNotMatchMapMissingKey() { Map<String,Integer> map = new TreeMap<String, Integer>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); assertMismatchDescription("map was [<a=1>, <b=2>, <c=3>]", hasKey("d"), map); } }
public void testHasReadableDescription() { assertDescription("map containing [\"a\"->ANYTHING]", hasKey("a")); }
public void testDoesNotMatchEmptyMap() { assertMismatchDescription("map was []", hasKey("Foo"), new HashMap<String,Integer>()); }
private void validateKeys(Map<String, Object> response) { List<Map<String, Object>> keys = (List<Map<String, Object>>) response.get("keys"); assertNotNull(keys); Map<String, ? extends Map<String, Object>> keysMap = keys.stream().collect(new MapCollector<>(k -> (String) k.get("kid"), k -> k)); assertThat(keysMap, hasKey(is("testKey"))); validateKey(keysMap.get("testKey")); }
@Test @Documented( "Include query statistics\n" + "\n" + "By setting `includeStats` to `true` for a statement, query statistics will be returned for it." ) public void include_query_statistics() throws JsonParseException { // Document ResponseEntity response = gen.get() .expectedStatus( 200 ) .payload( quotedJson( "{ 'statements': [ { 'statement': 'CREATE (n) RETURN id(n)', 'includeStats': true } ] }" ) ) .post( getDataUri() + "transaction/commit" ); // Then Map<String,Object> entity = jsonToMap( response.entity() ); assertNoErrors( entity ); Map<String,Object> firstResult = ((List<Map<String,Object>>) entity.get( "results" )).get( 0 ); assertThat( firstResult, hasKey( "stats" ) ); Map<String,Object> stats = (Map<String,Object>) firstResult.get( "stats" ); assertThat( stats.get( "nodes_created" ), equalTo( 1 ) ); }
@Test public void testMultipleConfigurationsFromXml() throws URISyntaxException { URL resource = XmlConfigurationTest.class.getResource("/configs/multi/multiple-configs.xml"); XmlMultiConfiguration xmlMultiConfiguration = XmlMultiConfiguration.from(resource).build(); assertThat(xmlMultiConfiguration.configuration("foo").getCacheConfigurations(), hasKey("foo")); assertThat(xmlMultiConfiguration.configuration("foo", "prod").getCacheConfigurations(), hasKey("foo")); assertThat(xmlMultiConfiguration.configuration("bar").getCacheConfigurations(), hasKey("bar")); assertThat(xmlMultiConfiguration.configuration("bar", "prod").getCacheConfigurations(), hasKey("bar")); assertThat(xmlMultiConfiguration.identities(), containsInAnyOrder("foo", "bar")); assertThat(xmlMultiConfiguration.variants("foo"), empty()); assertThat(xmlMultiConfiguration.variants("bar"), empty()); assertThat(xmlMultiConfiguration.toString(), isSimilarTo(Input.fromURI(resource.toURI())).ignoreWhitespace().ignoreComments()); }
@Test @Documented( "Handling errors in an open transaction\n" + "\n" + "Whenever there is an error in a request the server will rollback the transaction.\n" + "By inspecting the response for the presence/absence of the `transaction` key you can tell if the " + "transaction is still open" ) public void errors_in_open_transaction() throws JsonParseException { // Given String location = POST( getDataUri() + "transaction" ).location(); // Document ResponseEntity response = gen.get() .expectedStatus( 200 ) .payload( quotedJson( "{ 'statements': [ { 'statement': 'This is not a valid Cypher Statement.' } ] }" ) ) .post( location ); // Then Map<String, Object> result = jsonToMap( response.entity() ); assertThat(result, not(hasKey( "transaction" ))); }
@Test public void testMultipleVariantsFromXml() throws URISyntaxException { URL resource = XmlConfigurationTest.class.getResource("/configs/multi/multiple-variants.xml"); XmlMultiConfiguration xmlMultiConfiguration = XmlMultiConfiguration.from(resource).build(); assertThrows(() -> xmlMultiConfiguration.configuration("foo"), IllegalStateException.class); assertThat(xmlMultiConfiguration.configuration("foo", "development").getCacheConfigurations(), hasKey("foo-dev")); assertThat(xmlMultiConfiguration.configuration("foo", "production").getCacheConfigurations(), hasKey("foo-prod")); assertThat(xmlMultiConfiguration.configuration("bar", "development").getCacheConfigurations(), hasKey("bar-dev")); assertThat(xmlMultiConfiguration.configuration("bar", "production").getCacheConfigurations(), hasKey("bar-prod")); assertThat(xmlMultiConfiguration.identities(), containsInAnyOrder("foo", "bar")); assertThat(xmlMultiConfiguration.variants("foo"), containsInAnyOrder("development", "production")); assertThat(xmlMultiConfiguration.variants("bar"), containsInAnyOrder("development", "production")); assertThat(xmlMultiConfiguration.toString(), isSimilarTo(Input.fromURI(resource.toURI())).ignoreWhitespace().ignoreComments()); }
@Test @Documented( "Execute statements in an open transaction\n" + "\n" + "Given that you have an open transaction, you can make a number of requests, each of which executes additional\n" + "statements, and keeps the transaction open by resetting the transaction timeout." ) public void execute_statements_in_an_open_transaction() throws JsonParseException { // Given String location = POST( getDataUri() + "transaction" ).location(); // Document ResponseEntity response = gen.get() .expectedStatus( 200 ) .payload( quotedJson( "{ 'statements': [ { 'statement': 'CREATE (n) RETURN n' } ] }" ) ) .post( location ); // Then Map<String, Object> result = jsonToMap( response.entity() ); assertThat(result, hasKey( "transaction" )); assertNoErrors( result ); }
@Test public void testManagerRemovedFromXml() throws URISyntaxException { URL resource = XmlConfigurationTest.class.getResource("/configs/multi/multiple-configs.xml"); XmlMultiConfiguration xmlMultiConfiguration = XmlMultiConfiguration.from(resource).withoutManager("bar").build(); assertThat(xmlMultiConfiguration.configuration("foo").getCacheConfigurations(), hasKey("foo")); assertThat(xmlMultiConfiguration.configuration("foo", "prod").getCacheConfigurations(), hasKey("foo")); assertThat(xmlMultiConfiguration.configuration("bar"), nullValue()); assertThat(xmlMultiConfiguration.configuration("bar", "prod"), nullValue()); assertThat(xmlMultiConfiguration.identities(), containsInAnyOrder("foo")); assertThat(xmlMultiConfiguration.variants("foo"), empty()); assertThat(xmlMultiConfiguration.toString(), isSimilarTo( "<configurations xmlns='http://www.ehcache.org/v3/multi'>" + "<configuration identity='foo'>" + "<ehcache:config xmlns:ehcache='http://www.ehcache.org/v3' xmlns:multi='http://www.ehcache.org/v3/multi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" + "<ehcache:cache alias='foo'><ehcache:heap unit='entries'>100</ehcache:heap></ehcache:cache>" + "</ehcache:config>" + "</configuration>" + "</configurations>").ignoreWhitespace().ignoreComments()); }
@Test public void testManagerRemovedFromXmlAndReadded() throws URISyntaxException { URL resource = XmlConfigurationTest.class.getResource("/configs/multi/multiple-configs.xml"); XmlMultiConfiguration xmlMultiConfiguration = XmlMultiConfiguration.from(resource).withoutManager("bar").withManager("bar", emptyConfiguration()).build(); assertThat(xmlMultiConfiguration.configuration("foo").getCacheConfigurations(), hasKey("foo")); assertThat(xmlMultiConfiguration.configuration("foo", "prod").getCacheConfigurations(), hasKey("foo")); assertThat(xmlMultiConfiguration.configuration("bar"), notNullValue()); assertThat(xmlMultiConfiguration.configuration("bar", "prod"), notNullValue()); assertThat(xmlMultiConfiguration.identities(), containsInAnyOrder("foo", "bar")); assertThat(xmlMultiConfiguration.variants("foo"), empty()); assertThat(xmlMultiConfiguration.variants("bar"), empty()); assertThat(xmlMultiConfiguration.toString(), isSimilarTo( "<configurations xmlns='http://www.ehcache.org/v3/multi'>" + "<configuration identity='bar'>" + "<config xmlns='http://www.ehcache.org/v3'/>" + "</configuration>" + "<configuration identity='foo'>" + "<ehcache:config xmlns:ehcache='http://www.ehcache.org/v3' xmlns:multi='http://www.ehcache.org/v3/multi' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" + "<ehcache:cache alias='foo'><ehcache:heap unit='entries'>100</ehcache:heap></ehcache:cache>" + "</ehcache:config>" + "</configuration>" + "</configurations>").ignoreWhitespace().ignoreComments()); }