Skip to content

Commit

Permalink
Changed setter type of GreaseInterceptor.setRate() from Double to Str…
Browse files Browse the repository at this point in the history
…ing, whereas the String gets parsed as double internally (#1188)
  • Loading branch information
t-burch authored Jul 12, 2024
1 parent a3995fa commit 8b06e72
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 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 @@ -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 8b06e72

Please sign in to comment.