-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
241 additions
and
253 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
87 changes: 87 additions & 0 deletions
87
src/main/java/br/unifor/ppgia/resiliencebench/resources/PatternConfig.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,87 @@ | ||
package br.unifor.ppgia.resiliencebench.resources; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
import static io.vertx.core.impl.ConversionHelper.toObject; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public class PatternConfig extends ArrayList<PatternConfig.Attribute> { | ||
public PatternConfig() { | ||
} | ||
|
||
public PatternConfig(Attribute... params) { | ||
this.addAll(Arrays.asList(params)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
var serialized = this.stream() | ||
.map(Attribute::toString) | ||
.collect(joining(".")); | ||
if (serialized.isEmpty()) { | ||
serialized = "none"; | ||
} | ||
return serialized; | ||
} | ||
|
||
public static class Attribute { | ||
@JsonIgnore | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
private String name; | ||
private JsonNode value; | ||
|
||
public Attribute() { | ||
} | ||
|
||
public Attribute(String name, JsonNode value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public Attribute(String name, Object value) { | ||
this(name, mapper.valueToTree(value)); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public JsonNode getValue() { | ||
return value; | ||
} | ||
|
||
@JsonIgnore | ||
public Object getValueAsObject() { | ||
if (getValue().isTextual()) { | ||
return getValue().asText(); | ||
} else if (getValue().isNumber()) { | ||
if (getValue().isDouble() || getValue().isFloatingPointNumber()) { | ||
return getValue().doubleValue(); | ||
} else if (getValue().isLong()) { | ||
return getValue().longValue(); | ||
} else { | ||
return getValue().intValue(); | ||
} | ||
} else if (getValue().isBoolean()) { | ||
return getValue().asBoolean(); | ||
} else if (getValue().isArray()) { | ||
var list = new ArrayList<>(); | ||
getValue().elements().forEachRemaining(element -> list.add(toObject(element))); | ||
return list; | ||
} else { | ||
return getValue(); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getName() + "-" + getValueAsObject(); | ||
} | ||
} | ||
} |
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
57 changes: 38 additions & 19 deletions
57
src/test/java/br/unifor/ppgia/resiliencebench/resources/ListExpansionTest.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 |
---|---|---|
@@ -1,42 +1,61 @@ | ||
package br.unifor.ppgia.resiliencebench.resources; | ||
|
||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ListExpansionTest { | ||
|
||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
@Test | ||
public void should_expand_template() { | ||
Map<String, Object> configTemplate = new HashMap<>(); | ||
configTemplate.put("slowCallRateThreshold", "100"); | ||
configTemplate.put("slowCallDurationThreshold", "1000"); | ||
configTemplate.put("waitDurationInOpenState", List.of("50", "100", "200")); | ||
public void should_expand_template_as_patternConfig() { | ||
PatternConfig configTemplate = new PatternConfig(); | ||
|
||
configTemplate.add(new PatternConfig.Attribute("slowCallRateThreshold", objectMapper.valueToTree(100))); | ||
configTemplate.add(new PatternConfig.Attribute("slowCallDurationThreshold", objectMapper.valueToTree(1000))); | ||
configTemplate.add(new PatternConfig.Attribute("waitDurationInOpenState", objectMapper.valueToTree(List.of(50, 100, 200)))); | ||
|
||
var expandedConfigs = ListExpansion.expandConfigTemplate(configTemplate); | ||
|
||
Assertions.assertEquals(3, expandedConfigs.size()); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("waitDurationInOpenState"), "50"); | ||
Assertions.assertEquals(expandedConfigs.get(1).get("waitDurationInOpenState"), "100"); | ||
Assertions.assertEquals(expandedConfigs.get(2).get("waitDurationInOpenState"), "200"); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("waitDurationInOpenState"), 50); | ||
Assertions.assertEquals(expandedConfigs.get(1).get("waitDurationInOpenState"), 100); | ||
Assertions.assertEquals(expandedConfigs.get(2).get("waitDurationInOpenState"), 200); | ||
} | ||
|
||
@Test | ||
public void should_expand_simple_template_as_patternConfig() { | ||
PatternConfig configTemplate = new PatternConfig(); | ||
|
||
configTemplate.add(new PatternConfig.Attribute("slowCallRateThreshold",100)); | ||
configTemplate.add(new PatternConfig.Attribute("slowCallDurationThreshold",1000)); | ||
configTemplate.add(new PatternConfig.Attribute("waitDurationInOpenState", 200)); | ||
|
||
var expandedConfigs = ListExpansion.expandConfigTemplate(configTemplate); | ||
|
||
Assertions.assertEquals(1, expandedConfigs.size()); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("slowCallRateThreshold"), 100); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("slowCallDurationThreshold"), 1000); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("waitDurationInOpenState"), 200); | ||
} | ||
|
||
@Test | ||
public void should_expand_multiple_templates() { | ||
Map<String, Object> configTemplate = new HashMap<>(); | ||
configTemplate.put("slowCallRateThreshold", "100"); | ||
configTemplate.put("slowCallDurationThreshold", List.of("1000", "2000")); | ||
configTemplate.put("waitDurationInOpenState", List.of("50", "100", "200")); | ||
public void should_expand_multiple_templates_as_patternConfig() { | ||
PatternConfig configTemplate = new PatternConfig(); | ||
configTemplate.add(new PatternConfig.Attribute("slowCallRateThreshold", 100)); | ||
configTemplate.add(new PatternConfig.Attribute("slowCallDurationThreshold", List.of(1000, 2000))); | ||
configTemplate.add(new PatternConfig.Attribute("waitDurationInOpenState", List.of(50, 100, 200))); | ||
var expandedConfigs = ListExpansion.expandConfigTemplate(configTemplate); | ||
|
||
Assertions.assertEquals(6, expandedConfigs.size()); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("waitDurationInOpenState"), "50"); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("slowCallDurationThreshold"), "1000"); | ||
Assertions.assertEquals(expandedConfigs.get(1).get("waitDurationInOpenState"), "100"); | ||
Assertions.assertEquals(expandedConfigs.get(1).get("slowCallDurationThreshold"), "1000"); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("waitDurationInOpenState"), 50); | ||
Assertions.assertEquals(expandedConfigs.get(0).get("slowCallDurationThreshold"), 1000); | ||
Assertions.assertEquals(expandedConfigs.get(1).get("waitDurationInOpenState"), 100); | ||
Assertions.assertEquals(expandedConfigs.get(1).get("slowCallDurationThreshold"), 1000); | ||
} | ||
} | ||
|
Oops, something went wrong.