-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from SylvainJuge/assertions-refactoring
Assertions refactoring
- Loading branch information
Showing
8 changed files
with
126 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...rationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/AttributeMatcherGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jmxscraper.assertions; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
/** Group of attribute matchers */ | ||
public class AttributeMatcherGroup { | ||
|
||
// stored as a Map for easy lookup by name | ||
private final Map<String, AttributeMatcher> matchers; | ||
|
||
/** | ||
* Constructor for a set of attribute matchers | ||
* | ||
* @param matchers collection of matchers to build a group from | ||
* @throws IllegalStateException if there is any duplicate key | ||
*/ | ||
AttributeMatcherGroup(Collection<AttributeMatcher> matchers) { | ||
this.matchers = | ||
matchers.stream().collect(Collectors.toMap(AttributeMatcher::getAttributeName, m -> m)); | ||
} | ||
|
||
/** | ||
* Checks if attributes match this attribute matcher group | ||
* | ||
* @param attributes attributes to check as map | ||
* @return {@literal true} when the attributes match all attributes from this group | ||
*/ | ||
public boolean matches(Map<String, String> attributes) { | ||
if (attributes.size() != matchers.size()) { | ||
return false; | ||
} | ||
|
||
for (Map.Entry<String, String> entry : attributes.entrySet()) { | ||
AttributeMatcher matcher = matchers.get(entry.getKey()); | ||
if (matcher == null) { | ||
// no matcher for this key: unexpected key | ||
return false; | ||
} | ||
|
||
if (!matcher.matchesValue(entry.getValue())) { | ||
// value does not match: unexpected value | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return matchers.values().toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.