Skip to content

Commit 018132e

Browse files
committed
Fix JaxRs recipe
1 parent cce0b5f commit 018132e

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.jee.jaxrs.actions;
17+
18+
import org.intellij.lang.annotations.Language;
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.sbm.engine.context.ProjectContext;
21+
import org.springframework.sbm.java.api.Method;
22+
import org.springframework.sbm.java.migration.conditions.HasTypeAnnotation;
23+
import org.springframework.sbm.project.resource.TestProjectContext;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* @author Fabian Krüger
29+
*/
30+
public class MigrateJaxRsAnnotations_Test {
31+
32+
private final static String SPRING_VERSION = "5.3.13";
33+
34+
@Test
35+
void convertJaxRsMethodToSpringMvc() {
36+
@Language("java")
37+
String restControllerCode = """
38+
package com.example.jeerest.rest;
39+
40+
import com.example.jeerest.Movie;
41+
import com.example.jeerest.MoviesBean;
42+
import org.springframework.beans.factory.annotation.Autowired;
43+
44+
import javax.ws.rs.DELETE;
45+
import javax.ws.rs.GET;
46+
import javax.ws.rs.PUT;
47+
import javax.ws.rs.Path;
48+
import javax.ws.rs.PathParam;
49+
import javax.ws.rs.Produces;
50+
import javax.ws.rs.QueryParam;
51+
import javax.ws.rs.core.MediaType;
52+
import java.util.List;
53+
54+
@Path("movies")
55+
@Produces({"application/json"})
56+
public class MoviesRest {
57+
@GET
58+
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
59+
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
60+
return service.getMovies(first, max, field, searchTerm);
61+
}
62+
}
63+
""";
64+
65+
ProjectContext context = TestProjectContext
66+
.buildProjectContext()
67+
.withJavaSources(restControllerCode)
68+
.withBuildFileHavingDependencies("org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final",
69+
"org.springframework:spring-core:" + SPRING_VERSION)
70+
.build();
71+
72+
Method jaxRsMethod = context.getProjectJavaSources()
73+
.list()
74+
.get(0)
75+
.getTypes()
76+
.get(0)
77+
.getMethods()
78+
.get(0);
79+
80+
ConvertJaxRsAnnotations convertJaxRsAnnotations = ConvertJaxRsAnnotations
81+
.builder()
82+
.condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build())
83+
.description("Convert JAX-RS annotations into Spring Boot annotations.")
84+
.build();
85+
86+
convertJaxRsAnnotations.convertJaxRsMethodToSpringMvc(jaxRsMethod);
87+
88+
89+
@Language("java")
90+
String expected =
91+
"""
92+
package com.example.jeerest.rest;
93+
94+
import com.example.jeerest.Movie;
95+
import com.example.jeerest.MoviesBean;
96+
import org.springframework.beans.factory.annotation.Autowired;
97+
98+
import javax.ws.rs.DELETE;
99+
import javax.ws.rs.PUT;
100+
import javax.ws.rs.Path;
101+
import javax.ws.rs.PathParam;
102+
import javax.ws.rs.Produces;
103+
import javax.ws.rs.QueryParam;
104+
import javax.ws.rs.core.MediaType;
105+
import java.util.List;
106+
107+
@Path("movies")
108+
@Produces({"application/json"})
109+
public class MoviesRest {
110+
@RequestMapping(method = RequestMethod.GET)
111+
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
112+
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
113+
return service.getMovies(first, max, field, searchTerm);
114+
}
115+
}
116+
""";
117+
118+
assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo(
119+
expected
120+
);
121+
122+
123+
}
124+
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.jee.jaxrs.recipes;
17+
18+
import org.intellij.lang.annotations.Language;
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.sbm.engine.context.ProjectContext;
21+
import org.springframework.sbm.java.migration.conditions.HasTypeAnnotation;
22+
import org.springframework.sbm.jee.jaxrs.actions.ConvertJaxRsAnnotations;
23+
import org.springframework.sbm.project.resource.TestProjectContext;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* @author Fabian Krüger
29+
*/
30+
public class ConvertJaxRsAnnotationsest {
31+
32+
private final static String SPRING_VERSION = "5.3.13";
33+
34+
@Test
35+
void noPathOnMethodLevel() {
36+
@Language("java")
37+
String restControllerCode = """
38+
package com.example.jeerest.rest;
39+
40+
import com.example.jeerest.Movie;
41+
import com.example.jeerest.MoviesBean;
42+
import org.springframework.beans.factory.annotation.Autowired;
43+
44+
import javax.ws.rs.DELETE;
45+
import javax.ws.rs.GET;
46+
import javax.ws.rs.PUT;
47+
import javax.ws.rs.Path;
48+
import javax.ws.rs.PathParam;
49+
import javax.ws.rs.Produces;
50+
import javax.ws.rs.QueryParam;
51+
import javax.ws.rs.core.MediaType;
52+
import java.util.List;
53+
54+
@Path("movies")
55+
@Produces({"application/json"})
56+
public class MoviesRest {
57+
58+
@GET
59+
@Path("{id}")
60+
public Movie find(@PathParam("id") Long id) {
61+
return service.find(id);
62+
}
63+
64+
@GET
65+
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
66+
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
67+
return service.getMovies(first, max, field, searchTerm);
68+
}
69+
}
70+
""";
71+
72+
ProjectContext context = TestProjectContext.buildProjectContext()
73+
.withJavaSources(restControllerCode)
74+
.withBuildFileHavingDependencies(
75+
"org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final",
76+
"org.springframework:spring-core:"+SPRING_VERSION
77+
)
78+
.build();
79+
80+
ConvertJaxRsAnnotations convertJaxRsAnnotations = ConvertJaxRsAnnotations
81+
.builder()
82+
.condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build())
83+
.description("Convert JAX-RS annotations into Spring Boot annotations.")
84+
.build();
85+
86+
convertJaxRsAnnotations.apply(context);
87+
88+
89+
@Language("java")
90+
String expected =
91+
"""
92+
package com.example.jeerest.rest;
93+
94+
import com.example.jeerest.Movie;
95+
import com.example.jeerest.MoviesBean;
96+
import org.springframework.beans.factory.annotation.Autowired;
97+
98+
import javax.ws.rs.DELETE;
99+
import javax.ws.rs.PUT;
100+
import javax.ws.rs.PathParam;
101+
import javax.ws.rs.Produces;
102+
import javax.ws.rs.QueryParam;
103+
import javax.ws.rs.core.MediaType;
104+
import java.util.List;
105+
106+
107+
@Produces({"application/json"})
108+
@RestController
109+
@RequestMapping(value = "movies")
110+
public class MoviesRest {
111+
112+
@RequestMapping(value = "{id}", method = RequestMethod.GET)
113+
public Movie find(@PathParam("id") Long id) {
114+
return service.find(id);
115+
}
116+
117+
@RequestMapping(method = RequestMethod.GET)
118+
public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
119+
@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
120+
return service.getMovies(first, max, field, searchTerm);
121+
}
122+
}
123+
""";
124+
125+
assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo(
126+
expected
127+
);
128+
129+
130+
}
131+
132+
}

0 commit comments

Comments
 (0)