Skip to content

Commit

Permalink
Merge branch 'master' into #1170-apis-json-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
t-burch authored Jul 12, 2024
2 parents 9779dba + af117df commit c637331
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static com.predic8.membrane.core.interceptor.Interceptor.Flow.*;
import static com.predic8.membrane.core.interceptor.Outcome.*;
import static java.lang.Double.parseDouble;
import static java.util.EnumSet.*;

@MCElement(name = "greaser")
Expand Down Expand Up @@ -63,8 +64,8 @@ public Outcome handleResponse(Exchange exc) throws Exception {
}

@MCAttribute
public void setRate(double rate) {
this.rate = Math.max(0, Math.min(1, rate));
public void setRate(String rate) {
this.rate = Math.max(0, Math.min(1, parseDouble(rate)));
}

public double getRate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public class ApiDocsInterceptor extends AbstractInterceptor {

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
if(!initialized) {
ruleApiSpecs = initializeRuleApiSpecs();
initialized = true;
synchronized(this) {
if (!initialized) {
ruleApiSpecs = initializeRuleApiSpecs();
initialized = true;
}
}
var publisher = new OpenAPIPublisher(ruleApiSpecs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class GreaseInterceptorTest {
@BeforeEach
void setup() {
greaseInterceptor = new GreaseInterceptor();
greaseInterceptor.setRate(1);
greaseInterceptor.setRate("1");
greaseInterceptor.setStrategies(List.of(new JsonGrease() {{
setAdditionalProperties(false);}}));
}
Expand All @@ -65,25 +65,25 @@ void testRate() throws Exception {
// Test with rate = 1
assertNotEquals(json, requestExc.getRequest().getBodyAsStringDecoded());

greaseInterceptor.setRate(0.1);
greaseInterceptor.setRate("0.1");
assertEquals(0.1, calculateRate(greaseInterceptor, json), 0.02);

greaseInterceptor.setRate(0.5);
greaseInterceptor.setRate("0.5");
assertEquals(0.5, calculateRate(greaseInterceptor, json), 0.02);

greaseInterceptor.setRate(0.01);
greaseInterceptor.setRate("0.01");
assertEquals(0.01, calculateRate(greaseInterceptor, json), 0.02);
}

@Test
void testSetRate() {
greaseInterceptor.setRate(0.5);
greaseInterceptor.setRate("0.5");
assertEquals(0.5, greaseInterceptor.getRate());
greaseInterceptor.setRate(1.5);
greaseInterceptor.setRate("1.5");
assertEquals(1.0, greaseInterceptor.getRate());
greaseInterceptor.setRate(-0.5);
greaseInterceptor.setRate("-0.5");
assertEquals(0.0, greaseInterceptor.getRate());
greaseInterceptor.setRate(0.0001);
greaseInterceptor.setRate("0.0001");
assertEquals(0.0001, greaseInterceptor.getRate());
}

Expand Down

0 comments on commit c637331

Please sign in to comment.