Skip to content

Commit

Permalink
Cover more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermarc committed Mar 22, 2024
1 parent 7bd1cbf commit a46ccb2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.sap.openrewrite.recipe</groupId>
<artifactId>cap-java-recipes</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
public class RequestContexRunnerRules {

@RecipeDescriptor(
name = "Replaces setTenant() with systemUser()",
name = "Replaces setTenant() with systemUser() using u as lambda name",
description = "Descriptions")
public static class SetTenantToSystemUser {
public static class SetTenantToSystemUserWithU {

@BeforeTemplate
public RequestContextRunner modifyUserBefore(RequestContextRunner runner, String tenant) {
Expand All @@ -25,4 +25,21 @@ public RequestContextRunner systemUserAfter(RequestContextRunner runner, String

}

@RecipeDescriptor(
name = "Replaces setTenant() with systemUser() using user as lambda name",
description = "Descriptions")
public static class SetTenantToSystemUserWithUser {

@BeforeTemplate
public RequestContextRunner modifyUserBefore(RequestContextRunner runner, String tenant) {
return runner.modifyUser(user -> user.setTenant(tenant));
}

@AfterTemplate
public RequestContextRunner systemUserAfter(RequestContextRunner runner, String tenant) {
return runner.systemUser(tenant);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,49 @@ public void defaults(RecipeSpec spec) {
}

@Test
void testReplaceModifyUser() {
void testReplaceModifyUserWithU() {
rewriteRun(
java(
"""
import com.sap.cds.services.runtime.RequestContextRunner;
import java.util.Map;
class Test {
void test(RequestContextRunner cdsContextRunner, Map<String, String> map) {
cdsContextRunner.modifyUser(u -> u.setTenant(map.get("test").toString()));
}
}
""",
"""
import com.sap.cds.services.runtime.RequestContextRunner;
import java.util.Map;
class Test {
void test(RequestContextRunner cdsContextRunner, Map<String, String> map) {
cdsContextRunner.systemUser(map.get("test").toString());
}
}
"""
)
);

}

@Test
void testReplaceModifyUserWithUser() {
rewriteRun(
java(
"""
import com.sap.cds.services.runtime.CdsRuntime;
class Test {
void test(CdsRuntime runtime) {
String test = "test";
runtime.requestContext().modifyUser(u -> u.setTenant(test));
String test = "tenant";
runtime.requestContext().modifyUser(user -> user.setTenant(test));
}
}
""",
"""
Expand All @@ -37,10 +66,9 @@ void test(CdsRuntime runtime) {
class Test {
void test(CdsRuntime runtime) {
String test = "test";
String test = "tenant";
runtime.requestContext().systemUser(test);
}
}
"""
)
Expand All @@ -49,3 +77,4 @@ void test(CdsRuntime runtime) {
}

}

0 comments on commit a46ccb2

Please sign in to comment.