-
Notifications
You must be signed in to change notification settings - Fork 54
/
OpenApiExtensionTest.java
172 lines (153 loc) · 7.66 KB
/
OpenApiExtensionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.ballerina.openapi.extension;
import io.ballerina.projects.Package;
import io.ballerina.projects.PackageCompilation;
import io.ballerina.projects.ProjectEnvironmentBuilder;
import io.ballerina.projects.directory.BuildProject;
import io.ballerina.projects.directory.SingleFileProject;
import io.ballerina.projects.environment.Environment;
import io.ballerina.projects.environment.EnvironmentBuilder;
import io.ballerina.tools.diagnostics.Diagnostic;
import io.ballerina.tools.diagnostics.DiagnosticInfo;
import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* This class includes tests for Ballerina WebSub compiler plugin.
*/
public class OpenApiExtensionTest {
private static final Path RESOURCE_DIRECTORY = Paths
.get("src", "test", "resources", "ballerina_sources").toAbsolutePath();
private static final Path DISTRIBUTION_PATH = Paths
.get("build", "target", "ballerina-distribution").toAbsolutePath();
@Test
public void testDocGenerationForBallerinaProject() throws IOException {
Package currentPackage = loadPackage("sample_1", false);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testDocGenerationForBallerinaProjectWithMultipleServices() throws IOException {
Package currentPackage = loadPackage("sample_2", false);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testDocGenerationForBallerinaProjectWithMultipleModules() throws IOException {
Package currentPackage = loadPackage("sample_3", false);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testDocGenerationForBallerinaProjectWithAnnotation() throws IOException {
Package currentPackage = loadPackage("sample_4", false);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testDocGenerationForSingleBalFile() {
Package currentPackage = loadPackage("sample_5/service.bal", true);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testDocGenerationForSingleBalFileWithAnnotation() {
Package currentPackage = loadPackage("sample_6/service.bal", true);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testDocGenerationForHttpLoadBalance() {
Package currentPackage = loadPackage("sample_7/service.bal", true);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testEmptyOpenApiContractForBalProject() {
Package currentPackage = loadPackage("sample_8", false);
PackageCompilation compilation = currentPackage.getCompilation();
List<Diagnostic> openApiDiagnostics = compilation.diagnosticResult().diagnostics().stream()
.filter(d ->
Objects.nonNull(d.diagnosticInfo().code())
&& d.diagnosticInfo().severity().equals(DiagnosticSeverity.WARNING))
.collect(Collectors.toList());
Assert.assertEquals(openApiDiagnostics.size(), 1);
Diagnostic diagnostic = openApiDiagnostics.get(0);
DiagnosticInfo info = diagnostic.diagnosticInfo();
Assert.assertEquals(info.code(), "OPENAPI_VALIDATOR_003");
Assert.assertEquals(info.messageFormat(), "given OpenAPI contract file path is an empty string.");
}
@Test
public void testDocGenerationWithEmptyServiceInfoAnnotation() {
Package currentPackage = loadPackage("sample_9", false);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testGeneratedDocEmbedDisableWithServiceInfoAnnotation() {
Package currentPackage = loadPackage("sample_10", false);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testGeneratedDocEmbedWithUserProvidedOpenApiDoc() {
Package currentPackage = loadPackage("sample_11", false);
PackageCompilation compilation = currentPackage.getCompilation();
Assert.assertEquals(compilation.diagnosticResult().errorCount(), 0);
Assert.assertTrue(noOpenApiWarningAvailable(compilation));
}
@Test
public void testInvalidOpenApiContractForBalProject() {
Package currentPackage = loadPackage("sample_12", false);
PackageCompilation compilation = currentPackage.getCompilation();
List<Diagnostic> openApiDiagnostics = compilation.diagnosticResult().diagnostics().stream()
.filter(d ->
Objects.nonNull(d.diagnosticInfo().code())
&& d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR))
.collect(Collectors.toList());
Assert.assertEquals(openApiDiagnostics.size(), 1);
Diagnostic diagnostic = openApiDiagnostics.get(0);
DiagnosticInfo info = diagnostic.diagnosticInfo();
Assert.assertEquals(info.code(), "OPENAPI_VALIDATOR_001");
Assert.assertTrue(info.messageFormat().contains("OpenAPI contract does not exist in the given location"));
}
private Package loadPackage(String path, boolean isSingleFile) {
Path projectDirPath = RESOURCE_DIRECTORY.resolve(path);
if (isSingleFile) {
return SingleFileProject.load(getEnvironmentBuilder(), projectDirPath).currentPackage();
}
BuildProject project = BuildProject.load(getEnvironmentBuilder(), projectDirPath);
return project.currentPackage();
}
private static ProjectEnvironmentBuilder getEnvironmentBuilder() {
Environment environment = EnvironmentBuilder.getBuilder().setBallerinaHome(DISTRIBUTION_PATH).build();
return ProjectEnvironmentBuilder.getBuilder(environment);
}
private boolean noOpenApiWarningAvailable(PackageCompilation compilation) {
return compilation.diagnosticResult().diagnostics().stream()
.filter(d -> DiagnosticSeverity.WARNING.equals(d.diagnosticInfo().severity()))
.noneMatch(d ->
Objects.nonNull(d.diagnosticInfo().code())
&& d.diagnosticInfo().code().startsWith("OPENAPI_10"));
}
}