Merge pull request #53 from fiammara/master
topic about petstore added
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
index 883f48a..f27c6a3 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodExecStats.java
@@ -28,7 +28,6 @@
public class MethodExecStats implements Comparable<MethodExecStats> {
private String method;
- private WeightedAverage avgTime = new WeightedAverage();
private volatile int minTime = -1, maxTime;
private AtomicInteger
@@ -76,7 +75,6 @@
finishes.incrementAndGet();
int milliTime = (int)(nanoTime/1_000_000);
totalTime.addAndGet(nanoTime);
- avgTime.add(1, nanoTime);
minTime = minTime == -1 ? milliTime : Math.min(minTime, milliTime);
maxTime = Math.max(maxTime, milliTime);
}
@@ -150,7 +148,7 @@
* @return The average execution time in milliseconds.
*/
public int getAvgTime() {
- return (int)avgTime.getValue() / 1_000_000;
+ return (int)(getTotalTime() / getRuns());
}
/**
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java
index 7355d01..560b1c2 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/WeightedAverage.java
@@ -16,7 +16,7 @@
* A simple weighted average of numbers.
*/
public class WeightedAverage {
- private Float value = 0f;
+ private Double value = 0d;
private int weight = 0;
/**
@@ -29,7 +29,7 @@
public WeightedAverage add(int w, Number v) {
if (v != null) {
try {
- float w1 = weight, w2 = w;
+ double w1 = weight, w2 = w;
weight = Math.addExact(weight, w);
if (weight != 0) {
value = (value * (w1/weight)) + (v.floatValue() * (w2/weight));
@@ -46,7 +46,7 @@
*
* @return The weighted average of all numbers.
*/
- public float getValue() {
+ public double getValue() {
return value;
}
}
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
index 9576131..dfdb118 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockRest.java
@@ -74,6 +74,8 @@
final String contextPath, servletPath;
+ private final String[] roles;
+
/**
* Constructor.
*
@@ -104,6 +106,7 @@
headers = new LinkedHashMap<>(b.headers);
contextPath = b.contextPath;
servletPath = b.servletPath;
+ roles = b.roles;
} catch (Exception e) {
throw new RuntimeException(e);
}
@@ -211,6 +214,7 @@
boolean debug;
Map<String,Object> headers = new LinkedHashMap<>();
String contextPath, servletPath;
+ String[] roles = new String[0];
Builder(Object impl) {
this.impl = impl;
@@ -451,6 +455,28 @@
}
/**
+ * Adds the specified security roles for all requests.
+ *
+ * @param values The role names to add to all requests (e.g. <js>"ROLE_ADMIN"</js>).
+ * @return This object (for method chaining).
+ */
+ public Builder roles(String...values) {
+ this.roles = values;
+ return this;
+ }
+
+ /**
+ * Adds the specified security role for all requests.
+ *
+ * @param value The role name to add to all requests (e.g. <js>"ROLE_ADMIN"</js>).
+ * @return This object (for method chaining).
+ */
+ public Builder role(String value) {
+ this.roles = new String[]{value};
+ return this;
+ }
+
+ /**
* Create a new {@link MockRest} object based on the settings on this builder.
*
* @return A new {@link MockRest} object.
@@ -481,7 +507,7 @@
@Override /* MockHttpConnection */
public MockServletRequest request(String method, String path, Map<String,Object> headers, Object body) {
String p = RestUtils.trimContextPath(ctx.getPath(), path);
- return MockServletRequest.create(method, p).contextPath(emptyIfNull(contextPath)).servletPath(emptyIfNull(servletPath)).body(body).headers(this.headers).headers(headers).debug(debug).restContext(ctx);
+ return MockServletRequest.create(method, p).roles(roles).contextPath(emptyIfNull(contextPath)).servletPath(emptyIfNull(servletPath)).body(body).headers(this.headers).headers(headers).debug(debug).restContext(ctx);
}
/**
diff --git a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java
index 97dc3d4..e67574b 100644
--- a/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java
+++ b/juneau-rest/juneau-rest-mock/src/main/java/org/apache/juneau/rest/mock2/MockServletRequest.java
@@ -249,11 +249,25 @@
/**
* Adds the specified roles on this request.
*
- * @param roles The roles to add to this request.
+ * @param roles The roles to add to this request (e.g. <js>"ROLE_ADMIN"</js>).
* @return This object (for method chaining).
*/
public MockServletRequest roles(String...roles) {
- this.roles.addAll(Arrays.asList(roles));
+ this.roles = ASet.create(roles);
+ return this;
+ }
+
+ /**
+ * Adds the specified role on this request.
+ *
+ * <p>
+ * Note that {@link MockRest.Builder#roles(String...)} can be used to set the roles for all requests.
+ *
+ * @param role The role to add to this request (e.g. <js>"ROLE_ADMIN"</js>).
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest role(String role) {
+ this.roles = ASet.create(role);
return this;
}