|
15 | 15 | */
|
16 | 16 | package org.springframework.sbm.jee.jaxrs.actions;
|
17 | 17 |
|
| 18 | +import org.intellij.lang.annotations.Language; |
18 | 19 | import org.springframework.sbm.java.api.JavaSource;
|
19 | 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; |
20 | 23 | import org.springframework.sbm.project.resource.TestProjectContext;
|
21 | 24 | import org.junit.jupiter.api.Test;
|
22 | 25 |
|
23 | 26 | import static org.assertj.core.api.Assertions.assertThat;
|
24 | 27 |
|
25 | 28 | public class ConvertJaxRsAnnotationsTest {
|
26 | 29 |
|
| 30 | + private final static String SPRING_VERSION = "5.3.13"; |
| 31 | + |
| 32 | + @Test |
| 33 | + void convertJaxRsMethodWithoutPathToSpringMvc() { |
| 34 | + @Language("java") |
| 35 | + String restControllerCode = """ |
| 36 | + package com.example.jeerest.rest; |
| 37 | + |
| 38 | + import com.example.jeerest.Movie; |
| 39 | + import com.example.jeerest.MoviesBean; |
| 40 | + import org.springframework.beans.factory.annotation.Autowired; |
| 41 | + |
| 42 | + import javax.ws.rs.DELETE; |
| 43 | + import javax.ws.rs.GET; |
| 44 | + import javax.ws.rs.PUT; |
| 45 | + import javax.ws.rs.Path; |
| 46 | + import javax.ws.rs.PathParam; |
| 47 | + import javax.ws.rs.Produces; |
| 48 | + import javax.ws.rs.QueryParam; |
| 49 | + import javax.ws.rs.core.MediaType; |
| 50 | + import java.util.List; |
| 51 | + |
| 52 | + @Path("movies") |
| 53 | + @Produces({"application/json"}) |
| 54 | + public class MoviesRest { |
| 55 | + @GET |
| 56 | + public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max, |
| 57 | + @QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) { |
| 58 | + return service.getMovies(first, max, field, searchTerm); |
| 59 | + } |
| 60 | + } |
| 61 | + """; |
| 62 | + |
| 63 | + ProjectContext context = TestProjectContext |
| 64 | + .buildProjectContext() |
| 65 | + .withJavaSources(restControllerCode) |
| 66 | + .withBuildFileHavingDependencies("org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_2.1_spec:1.0.1.Final", |
| 67 | + "org.springframework:spring-core:" + SPRING_VERSION) |
| 68 | + .build(); |
| 69 | + |
| 70 | + Method jaxRsMethod = context.getProjectJavaSources() |
| 71 | + .list() |
| 72 | + .get(0) |
| 73 | + .getTypes() |
| 74 | + .get(0) |
| 75 | + .getMethods() |
| 76 | + .get(0); |
| 77 | + |
| 78 | + ConvertJaxRsAnnotations convertJaxRsAnnotations = ConvertJaxRsAnnotations |
| 79 | + .builder() |
| 80 | + .condition(HasTypeAnnotation.builder().annotation("javax.ws.rs.Path").build()) |
| 81 | + .description("Convert JAX-RS annotations into Spring Boot annotations.") |
| 82 | + .build(); |
| 83 | + |
| 84 | + convertJaxRsAnnotations.convertJaxRsMethodToSpringMvc(jaxRsMethod); |
| 85 | + |
| 86 | + |
| 87 | + @Language("java") |
| 88 | + String expected = |
| 89 | + """ |
| 90 | + package com.example.jeerest.rest; |
| 91 | + |
| 92 | + import com.example.jeerest.Movie; |
| 93 | + import com.example.jeerest.MoviesBean; |
| 94 | + import org.springframework.beans.factory.annotation.Autowired; |
| 95 | + |
| 96 | + import javax.ws.rs.DELETE; |
| 97 | + import javax.ws.rs.PUT; |
| 98 | + import javax.ws.rs.Path; |
| 99 | + import javax.ws.rs.PathParam; |
| 100 | + import javax.ws.rs.Produces; |
| 101 | + import javax.ws.rs.QueryParam; |
| 102 | + import javax.ws.rs.core.MediaType; |
| 103 | + import java.util.List; |
| 104 | + |
| 105 | + @Path("movies") |
| 106 | + @Produces({"application/json"}) |
| 107 | + public class MoviesRest { |
| 108 | + @RequestMapping(method = RequestMethod.GET) |
| 109 | + public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max, |
| 110 | + @QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) { |
| 111 | + return service.getMovies(first, max, field, searchTerm); |
| 112 | + } |
| 113 | + } |
| 114 | + """; |
| 115 | + |
| 116 | + assertThat(context.getProjectJavaSources().list().get(0).print()).isEqualTo( |
| 117 | + expected |
| 118 | + ); |
| 119 | + } |
| 120 | + |
27 | 121 | @Test
|
28 | 122 | void replaceMethodAnnotationsWithOneAnnotation() throws Exception {
|
29 | 123 |
|
|
0 commit comments