|
17 | 17 | import io.swagger.v3.plugins.gradle.tasks.ResolveTask; |
18 | 18 | import org.eclipse.edc.plugins.edcbuild.extensions.BuildExtension; |
19 | 19 | import org.gradle.api.Project; |
| 20 | +import org.gradle.api.internal.project.ProjectInternal; |
20 | 21 | import org.gradle.api.plugins.JavaPlugin; |
| 22 | +import org.gradle.api.provider.Provider; |
| 23 | +import org.gradle.api.tasks.TaskProvider; |
21 | 24 | import org.gradle.testfixtures.ProjectBuilder; |
22 | 25 | import org.junit.jupiter.api.BeforeEach; |
23 | 26 | import org.junit.jupiter.api.Test; |
|
30 | 33 | class SwaggerResolveConventionTest { |
31 | 34 |
|
32 | 35 | private static final String PROJECT_NAME = "testproject"; |
33 | | - private Project project; |
| 36 | + private final Project project = ProjectBuilder.builder().withName(PROJECT_NAME).build(); |
| 37 | + private final SwaggerResolveConvention convention = new SwaggerResolveConvention(); |
34 | 38 |
|
35 | 39 | @BeforeEach |
36 | 40 | void setUp() { |
37 | | - project = ProjectBuilder.builder().withName(PROJECT_NAME).build(); |
38 | 41 | project.getRepositories().mavenCentral(); |
39 | 42 | project.getPluginManager().apply(SWAGGER_GRADLE_PLUGIN); |
40 | 43 | project.getPluginManager().apply(JavaPlugin.class); |
41 | 44 | project.getExtensions().create("edcBuild", BuildExtension.class, project.getObjects()); |
42 | 45 | } |
43 | 46 |
|
44 | 47 | @Test |
45 | | - void apply_whenApiGroupNotSpecified_shouldUseDefault() { |
46 | | - var convention = new SwaggerResolveConvention(); |
| 48 | + void shouldDisableTask_whenNoGroupSpecified() { |
47 | 49 | convention.apply(project); |
48 | 50 |
|
49 | 51 | var resolveTask = (ResolveTask) project.getTasks().getByName("resolve"); |
50 | 52 |
|
51 | | - assertThat(resolveTask.getOutputDir().get().toString()).endsWith("/resources/openapi/yaml"); |
52 | | - assertThat(resolveTask.getOutputFileName().get()).isEqualTo(PROJECT_NAME); |
53 | | - assertThat(resolveTask.getOutputFormat().get()).isEqualTo(ResolveTask.Format.YAML); |
54 | | - |
| 53 | + assertThat(resolveTask.isEnabled()).isFalse(); |
| 54 | + assertThat(resolveTask.getDependsOn()).hasSize(0); |
55 | 55 | } |
56 | 56 |
|
57 | 57 | @Test |
58 | 58 | void apply_whenApiGroupSpecified_shouldAppend() { |
59 | 59 | var swagger = ConventionFunctions.requireExtension(project, BuildExtension.class).getSwagger(); |
60 | | - swagger.getApiGroup().set("test-api"); |
61 | | - var convention = new SwaggerResolveConvention(); |
62 | | - convention.apply(project); |
63 | | - |
64 | | - var resolveTask = (ResolveTask) project.getTasks().getByName("resolve"); |
| 60 | + swagger.apiGroup("test-api"); |
65 | 61 |
|
66 | | - assertThat(resolveTask.getOutputDir().get().toString()).endsWith("/resources/openapi/yaml/test-api"); |
67 | | - assertThat(resolveTask.getOutputFileName().get()).isEqualTo(PROJECT_NAME); |
68 | | - assertThat(resolveTask.getOutputFormat().get()).isEqualTo(ResolveTask.Format.YAML); |
| 62 | + convention.apply(project); |
| 63 | + ((ProjectInternal) project).evaluate(); |
| 64 | + var resolveTask = project.getTasks().getByName("resolve"); |
| 65 | + |
| 66 | + assertThat(resolveTask.getDependsOn()).hasSize(1).first().isInstanceOfSatisfying(TaskProvider.class, taskProvider -> { |
| 67 | + assertThat(taskProvider.isPresent()).isTrue(); |
| 68 | + assertThat(taskProvider.get()).isInstanceOfSatisfying(ResolveTask.class, actual -> { |
| 69 | + assertThat(actual.getOutputDir().get().toString()).endsWith("/resources/openapi/yaml/test-api"); |
| 70 | + assertThat(actual.getOutputFileName().get()).isEqualTo(PROJECT_NAME); |
| 71 | + assertThat(actual.getOutputFormat().get()).isEqualTo(ResolveTask.Format.YAML); |
| 72 | + }); |
| 73 | + }); |
69 | 74 | } |
70 | 75 |
|
71 | 76 | @Test |
72 | 77 | void apply_whenOutputDirSet_shouldAppend() { |
73 | 78 | var swagger = ConventionFunctions.requireExtension(project, BuildExtension.class).getSwagger(); |
74 | | - swagger.getApiGroup().set("test-api"); |
| 79 | + swagger.apiGroup("test-api"); |
75 | 80 | swagger.getOutputDirectory().set(new File("some/funny/path")); |
76 | | - var convention = new SwaggerResolveConvention(); |
| 81 | + |
| 82 | + convention.apply(project); |
| 83 | + ((ProjectInternal) project).evaluate(); |
| 84 | + var resolveTask = project.getTasks().getByName("resolve"); |
| 85 | + |
| 86 | + assertThat(resolveTask.getDependsOn()).hasSize(1).first().isInstanceOfSatisfying(TaskProvider.class, taskProvider -> { |
| 87 | + assertThat(taskProvider.isPresent()).isTrue(); |
| 88 | + assertThat(taskProvider.get()).isInstanceOfSatisfying(ResolveTask.class, actual -> { |
| 89 | + assertThat(actual.getOutputDir().get().toString()).endsWith("/some/funny/path/test-api"); |
| 90 | + assertThat(actual.getOutputFileName().get()).isEqualTo(PROJECT_NAME); |
| 91 | + assertThat(actual.getOutputFormat().get()).isEqualTo(ResolveTask.Format.YAML); |
| 92 | + }); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void shouldSetPackage_whenSpecified() { |
| 98 | + var swagger = ConventionFunctions.requireExtension(project, BuildExtension.class).getSwagger(); |
| 99 | + swagger.apiGroup("group", "the.package.name", "another.package.name"); |
| 100 | + |
77 | 101 | convention.apply(project); |
| 102 | + ((ProjectInternal) project).evaluate(); |
| 103 | + var resolveTask = project.getTasks().getByName("resolve"); |
78 | 104 |
|
79 | | - var resolveTask = (ResolveTask) project.getTasks().getByName("resolve"); |
| 105 | + assertThat(resolveTask.getDependsOn()).hasSize(1).map(TaskProvider.class::cast).map(Provider::get).map(ResolveTask.class::cast) |
| 106 | + .first().satisfies(task -> { |
| 107 | + assertThat(task.getResourcePackages().get()).containsExactlyInAnyOrder("the.package.name", "another.package.name"); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void shouldRegisterMultipleTasks_whenMultipleApiGroups() { |
| 113 | + var swagger = ConventionFunctions.requireExtension(project, BuildExtension.class).getSwagger(); |
| 114 | + swagger.apiGroup("group-one"); |
| 115 | + swagger.apiGroup("group-two"); |
| 116 | + |
| 117 | + convention.apply(project); |
| 118 | + ((ProjectInternal) project).evaluate(); |
| 119 | + var resolveTask = project.getTasks().getByName("resolve"); |
| 120 | + |
| 121 | + assertThat(resolveTask.getDependsOn()).hasSize(2).map(TaskProvider.class::cast).map(Provider::get).map(ResolveTask.class::cast) |
| 122 | + .anySatisfy(p -> assertThat(p.getOutputDir().get().toString()).endsWith("/resources/openapi/yaml/group-one")) |
| 123 | + .anySatisfy(p -> assertThat(p.getOutputDir().get().toString()).endsWith("/resources/openapi/yaml/group-two")); |
| 124 | + } |
| 125 | + |
| 126 | + @Deprecated(since = "1.5.0") |
| 127 | + @Test |
| 128 | + void shouldApplyDeprecatedApiGroupCall_whenItIsDefined() { |
| 129 | + var swagger = ConventionFunctions.requireExtension(project, BuildExtension.class).getSwagger(); |
| 130 | + swagger.getApiGroup().set("test-api"); // deprecated call |
| 131 | + swagger.apiGroup("another-one"); |
| 132 | + swagger.getOutputDirectory().set(new File("some/funny/path")); |
| 133 | + |
| 134 | + convention.apply(project); |
| 135 | + ((ProjectInternal) project).evaluate(); |
| 136 | + var resolveTask = project.getTasks().getByName("resolve"); |
| 137 | + |
| 138 | + assertThat(resolveTask.getDependsOn()).hasSize(1).first().isInstanceOfSatisfying(TaskProvider.class, taskProvider -> { |
| 139 | + assertThat(taskProvider.isPresent()).isTrue(); |
| 140 | + assertThat(taskProvider.get()).isInstanceOfSatisfying(ResolveTask.class, actual -> { |
| 141 | + assertThat(actual.getOutputDir().get().toString()).endsWith("/some/funny/path/test-api"); |
| 142 | + assertThat(actual.getOutputFileName().get()).isEqualTo(PROJECT_NAME); |
| 143 | + assertThat(actual.getOutputFormat().get()).isEqualTo(ResolveTask.Format.YAML); |
| 144 | + }); |
| 145 | + }); |
80 | 146 |
|
81 | | - assertThat(resolveTask.getOutputDir().get().toString()).endsWith("/some/funny/path/test-api"); |
82 | | - assertThat(resolveTask.getOutputFileName().get()).isEqualTo(PROJECT_NAME); |
83 | | - assertThat(resolveTask.getOutputFormat().get()).isEqualTo(ResolveTask.Format.YAML); |
84 | 147 | } |
85 | 148 | } |
0 commit comments