Skip to content

Commit cd803e4

Browse files
Mathias KowalzikMathias Kowalzik
authored andcommitted
added tests and httpparams in service template
1 parent 989eec3 commit cd803e4

File tree

6 files changed

+146
-31
lines changed

6 files changed

+146
-31
lines changed

annotations/src/main/java/org/leandreck/endpoints/annotations/TypeScriptEndpoint.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@
2828

2929
/**
3030
* The name of the endpoint. Defaults to the name of the class annotated with TypeScriptEndpoint.
31+
*
3132
* @return name.
3233
*/
3334
String value() default "";
3435

3536
/**
36-
* Template to use for generating TypeScript-files for this TypeScriptEndpoint.
37-
* If none is specified the default-template will be used.
38-
* Default template is located at "/org/leandreck/endpoints/templates/typescript/service.ftl",
39-
* configured as the endpoint property for {@link TypeScriptTemplatesConfiguration}.
37+
* Template to use for generating TypeScript-files for this specific TypeScriptEndpoint, this overwrites any defaults.<br>
38+
* You can configure a default Template for all TypeScriptEndpoints in {@link TypeScriptTemplatesConfiguration}.<br>
39+
* If none is specified the default-template will be used.<br>
40+
* Default template is located at "/org/leandreck/endpoints/templates/typescript/service.ftl",<br>
4041
*
4142
* @return classpath location of the template
42-
* @deprecated Use the {@link TypeScriptTemplatesConfiguration} instead.
43+
* @see TypeScriptTemplatesConfiguration
4344
*/
44-
@Deprecated
4545
String template() default "";
4646
}

annotations/src/main/java/org/leandreck/endpoints/annotations/TypeScriptType.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@
3333

3434
/**
3535
* The name of the interface. Defaults to the name of the java type annotated with {@link TypeScriptType}.
36+
*
3637
* @return name.
3738
*/
3839
String value() default "";
3940

4041
/**
41-
* Template to use for generating TypeScript-files for this {@link TypeScriptType}.
42-
* If none is specified the default-template will be used.
43-
* Default template is located at "/org/leandreck/endpoints/templates/typescript/interface.ftl".
42+
* Template to use for generating TypeScript-files for this specific {@link TypeScriptType}, this overwrites any defaults.<br>
43+
* You can configure a default Template for all TypeScriptType in {@link TypeScriptTemplatesConfiguration}.<br>
44+
* If none is specified the default-template will be used.<br>
45+
* Default template is located at "/org/leandreck/endpoints/templates/typescript/interface.ftl".<br>
4446
*
4547
* @return classpath location of the template
46-
* @deprecated Use the {@link TypeScriptTemplatesConfiguration} instead.
48+
* @see TypeScriptTemplatesConfiguration
4749
*/
48-
@Deprecated
4950
String template() default "";
5051
}

annotations/src/main/java/org/leandreck/endpoints/processor/model/MethodNode.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ public class MethodNode {
3030
private final List<TypeNode> queryParameterTypes;
3131
private final List<String> httpMethods;
3232
private final Set<TypeNode> types;
33+
private final List<TypeNode> methodParameterTypes;
3334

3435
public MethodNode(final String name, final String url, final boolean ignored, final List<String> httpMethods, final TypeNode returnType) {
3536
this.name = name;
3637
this.url = url;
3738
this.ignored = ignored;
3839
this.returnType = returnType;
3940
this.httpMethods = httpMethods;
40-
requestBodyType = null;
41-
pathVariableTypes = Collections.emptyList();
42-
queryParameterTypes = Collections.emptyList();
41+
this.requestBodyType = null;
42+
this.pathVariableTypes = Collections.emptyList();
43+
this.queryParameterTypes = Collections.emptyList();
4344
this.types = collectTypes();
45+
this.methodParameterTypes = Collections.emptyList();
4446
}
4547

4648
public MethodNode(final String name, final String url, final boolean ignored, final List<String> httpMethods,
@@ -55,6 +57,9 @@ public MethodNode(final String name, final String url, final boolean ignored, fi
5557
this.pathVariableTypes = pathVariableTypes;
5658
this.queryParameterTypes = queryParameterTypes;
5759
this.types = collectTypes();
60+
this.methodParameterTypes = new ArrayList<>(pathVariableTypes.size() + queryParameterTypes.size());
61+
this.methodParameterTypes.addAll(pathVariableTypes);
62+
this.methodParameterTypes.addAll(queryParameterTypes);
5863
}
5964

6065
private Set<TypeNode> collectTypes() {
@@ -103,4 +108,12 @@ public List<TypeNode> getPathVariableTypes() {
103108
public List<TypeNode> getQueryParameterTypes() {
104109
return Collections.unmodifiableList(queryParameterTypes);
105110
}
111+
112+
/**
113+
* Returns the combined list of {@link #getPathVariableTypes()} and {@link #getQueryParameterTypes()}
114+
* @return All {@link TypeNode}s which are parameters to this MethodNode.
115+
*/
116+
public List<TypeNode> getMethodParameterTypes() {
117+
return Collections.unmodifiableList(methodParameterTypes);
118+
}
106119
}

annotations/src/main/java/org/leandreck/endpoints/processor/model/TypeNodeFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private List<TypeNode> defineChildren(final TypeElement typeElement, final List<
244244
.collect(toList());
245245
}
246246

247-
private static String defineTemplate(final TypeElement typeElement, final TemplateConfiguration templateConfiguration,
247+
static String defineTemplate(final TypeElement typeElement, final TemplateConfiguration templateConfiguration,
248248
final TypeScriptType typeScriptTypeAnnotation,
249249
final TypeNodeKind kind) {
250250

@@ -253,7 +253,7 @@ private static String defineTemplate(final TypeElement typeElement, final Templa
253253
}
254254

255255
final String template;
256-
if (typeScriptTypeAnnotation == null || typeScriptTypeAnnotation.template().isEmpty()) {
256+
if (typeScriptTypeAnnotation == null || typeScriptTypeAnnotation.template() == null || typeScriptTypeAnnotation.template().isEmpty()) {
257257
if (TypeNodeKind.ENUM.equals(kind)) {
258258
template = templateConfiguration.getEnumTemplate();
259259
} else {

annotations/src/main/resources/org/leandreck/endpoints/templates/typescript/service.ftl

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import { ${type.typeName} } from './${type.typeName?lower_case}.model.generated';
3030
</#list>
3131

32-
import { HttpClient, HttpRequest } from '@angular/common/http';
32+
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http';
3333
import { Injectable } from '@angular/core';
3434

3535
import { Observable } from 'rxjs/Observable';
@@ -45,9 +45,10 @@ export class ${serviceName} {
4545
<#list getGetMethods() as method>
4646
<#assign expandedURL = method.url?replace('{', '\' + ')>
4747
<#assign expandedURL = expandedURL?replace('}', ' + \'')>
48-
public ${method.name}Get(<#list method.pathVariableTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list>): Observable<${method.returnType.type}> {
48+
public ${method.name}Get(<#list method.methodParameterTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list>): Observable<${method.returnType.type}> {
4949
const url = this.serviceBaseURL + '${expandedURL}';
50-
return this.httpClient.get<${method.returnType.type}>(url)
50+
const params = new HttpParams()<#list method.queryParameterTypes><#items as queryParam>.set('${queryParam.fieldName}', ${queryParam.fieldName})</#items></#list>;
51+
return this.httpClient.get<${method.returnType.type}>(url, {params: params})
5152
.catch((error: Response) => this.handleError(error));
5253
}
5354

@@ -57,9 +58,10 @@ export class ${serviceName} {
5758
<#list getHeadMethods() as method>
5859
<#assign expandedURL = method.url?replace('{', '\' + ')>
5960
<#assign expandedURL = expandedURL?replace('}', ' + \'')>
60-
public ${method.name}Head(<#list method.pathVariableTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list>): Observable<${method.returnType.type}> {
61+
public ${method.name}Head(<#list method.methodParameterTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list>): Observable<${method.returnType.type}> {
6162
const url = this.serviceBaseURL + '${expandedURL}';
62-
return this.httpClient.head<${method.returnType.type}>(url)
63+
const params = new HttpParams()<#list method.queryParameterTypes><#items as queryParam>.set('${queryParam.fieldName}', ${queryParam.fieldName})</#items></#list>;
64+
return this.httpClient.head<${method.returnType.type}>(url, {params: params})
6365
.catch((error: Response) => this.handleError(error));
6466
}
6567

@@ -69,9 +71,10 @@ export class ${serviceName} {
6971
<#list getPostMethods() as method>
7072
<#assign expandedURL = method.url?replace('{', '\' + ')>
7173
<#assign expandedURL = expandedURL?replace('}', ' + \'')>
72-
public ${method.name}Post(<#list method.pathVariableTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list><#if method.pathVariableTypes?size gt 0>, </#if>${method.requestBodyType.fieldName}: ${method.requestBodyType.type}): Observable<${method.returnType.type}> {
74+
public ${method.name}Post(<#list method.methodParameterTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list><#if method.pathVariableTypes?size gt 0>, </#if>${method.requestBodyType.fieldName}: ${method.requestBodyType.type}): Observable<${method.returnType.type}> {
7375
const url = this.serviceBaseURL + '${expandedURL}';
74-
return this.httpClient.post<${method.returnType.type}>(url, ${method.requestBodyType.fieldName})
76+
const params = new HttpParams()<#list method.queryParameterTypes><#items as queryParam>.set('${queryParam.fieldName}', ${queryParam.fieldName})</#items></#list>;
77+
return this.httpClient.post<${method.returnType.type}>(url, ${method.requestBodyType.fieldName}, {params: params})
7578
.catch((error: Response) => this.handleError(error));
7679
}
7780

@@ -81,9 +84,10 @@ export class ${serviceName} {
8184
<#list getPutMethods() as method>
8285
<#assign expandedURL = method.url?replace('{', '\' + ')>
8386
<#assign expandedURL = expandedURL?replace('}', ' + \'')>
84-
public ${method.name}Put(<#list method.pathVariableTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list><#if method.pathVariableTypes?size gt 0>, </#if>${method.requestBodyType.fieldName}: ${method.requestBodyType.type}): Observable<${method.returnType.type}> {
87+
public ${method.name}Put(<#list method.methodParameterTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list><#if method.pathVariableTypes?size gt 0>, </#if>${method.requestBodyType.fieldName}: ${method.requestBodyType.type}): Observable<${method.returnType.type}> {
8588
const url = this.serviceBaseURL + '${expandedURL}';
86-
return this.httpClient.put<${method.returnType.type}>(url, ${method.requestBodyType.fieldName})
89+
const params = new HttpParams()<#list method.queryParameterTypes><#items as queryParam>.set('${queryParam.fieldName}', ${queryParam.fieldName})</#items></#list>;
90+
return this.httpClient.put<${method.returnType.type}>(url, ${method.requestBodyType.fieldName}, {params: params})
8791
.catch((error: Response) => this.handleError(error));
8892
}
8993

@@ -93,9 +97,10 @@ export class ${serviceName} {
9397
<#list getPatchMethods() as method>
9498
<#assign expandedURL = method.url?replace('{', '\' + ')>
9599
<#assign expandedURL = expandedURL?replace('}', ' + \'')>
96-
public ${method.name}Patch(<#list method.pathVariableTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list><#if method.pathVariableTypes?size gt 0>, </#if>${method.requestBodyType.fieldName}: ${method.requestBodyType.type}): Observable<${method.returnType.type}> {
100+
public ${method.name}Patch(<#list method.methodParameterTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list><#if method.pathVariableTypes?size gt 0>, </#if>${method.requestBodyType.fieldName}: ${method.requestBodyType.type}): Observable<${method.returnType.type}> {
97101
const url = this.serviceBaseURL + '${expandedURL}';
98-
return this.httpClient.patch<${method.returnType.type}>(url, ${method.requestBodyType.fieldName})
102+
const params = new HttpParams()<#list method.queryParameterTypes><#items as queryParam>.set('${queryParam.fieldName}', ${queryParam.fieldName})</#items></#list>;
103+
return this.httpClient.patch<${method.returnType.type}>(url, ${method.requestBodyType.fieldName}, {params: params})
99104
.catch((error: Response) => this.handleError(error));
100105
}
101106

@@ -105,9 +110,10 @@ export class ${serviceName} {
105110
<#list getDeleteMethods() as method>
106111
<#assign expandedURL = method.url?replace('{', '\' + ')>
107112
<#assign expandedURL = expandedURL?replace('}', ' + \'')>
108-
public ${method.name}Delete(<#list method.pathVariableTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list>): Observable<${method.returnType.type}> {
113+
public ${method.name}Delete(<#list method.methodParameterTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list>): Observable<${method.returnType.type}> {
109114
const url = this.serviceBaseURL + '${expandedURL}';
110-
return this.httpClient.delete<${method.returnType.type}>(url)
115+
const params = new HttpParams()<#list method.queryParameterTypes><#items as queryParam>.set('${queryParam.fieldName}', ${queryParam.fieldName})</#items></#list>;
116+
return this.httpClient.delete<${method.returnType.type}>(url, {params: params})
111117
.catch((error: Response) => this.handleError(error));
112118
}
113119

@@ -117,9 +123,10 @@ export class ${serviceName} {
117123
<#list getOptionsMethods() as method>
118124
<#assign expandedURL = method.url?replace('{', '\' + ')>
119125
<#assign expandedURL = expandedURL?replace('}', ' + \'')>
120-
public ${method.name}Options(<#list method.pathVariableTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list><#if method.pathVariableTypes?size gt 0>, </#if>): Observable<ArrayBuffer> {
126+
public ${method.name}Options(<#list method.methodParameterTypes as variable>${variable.fieldName}: ${variable.type}<#sep>, </#sep></#list>): Observable<${method.returnType.type}> {
121127
const url = this.serviceBaseURL + '${expandedURL}';
122-
return this.httpClient.options<${method.returnType.type}>(url)
128+
const params = new HttpParams()<#list method.queryParameterTypes><#items as queryParam>.set('${queryParam.fieldName}', ${queryParam.fieldName})</#items></#list>;
129+
return this.httpClient.options<${method.returnType.type}>(url, {params: params})
123130
.catch((error: Response) => this.handleError(error));
124131
}
125132

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright © 2016 Mathias Kowalzik (Mathias.Kowalzik@leandreck.org)
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+
* http://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.leandreck.endpoints.processor.model
17+
18+
import org.leandreck.endpoints.annotations.TypeScriptTemplatesConfiguration
19+
import org.leandreck.endpoints.annotations.TypeScriptType
20+
import org.leandreck.endpoints.processor.config.MultipleConfigurationsFoundException
21+
import org.leandreck.endpoints.processor.config.TemplateConfiguration
22+
import spock.lang.Shared
23+
import spock.lang.Specification
24+
import spock.lang.Subject
25+
import spock.lang.Title
26+
import spock.lang.Unroll
27+
28+
import javax.annotation.processing.RoundEnvironment
29+
import javax.lang.model.element.TypeElement
30+
31+
@Title("RequestMappingFactory Unittests")
32+
@Subject(TypeNodeFactory)
33+
class TypeNodeFactoryUnitSpec extends Specification {
34+
35+
@Shared
36+
TemplateConfiguration templateConfiguration = new TemplateConfiguration(
37+
"",
38+
"enumTemplate",
39+
"",
40+
"interfaceTemplate",
41+
""
42+
)
43+
44+
@Unroll
45+
def "If template() is #templateValue and TypeNodeKind is #typeNodeKind it will returned #expectedTemplate"() {
46+
given: "an Element annotated with TypeScriptEndpoint"
47+
TypeElement typeElement = Stub TypeElement
48+
TypeScriptType typeScriptType = Stub TypeScriptType
49+
typeScriptType.template() >> templateValue
50+
51+
when:
52+
def actual = TypeNodeFactory.defineTemplate(typeElement, templateConfiguration, typeScriptType, typeNodeKind)
53+
54+
then:
55+
notThrown MissingConfigurationTemplateException
56+
57+
and:
58+
actual == expectedTemplate
59+
60+
where: "possible values are"
61+
templateValue | typeNodeKind || expectedTemplate
62+
null | TypeNodeKind.SIMPLE || "interfaceTemplate"
63+
null | TypeNodeKind.ARRAY || "interfaceTemplate"
64+
null | TypeNodeKind.COLLECTION || "interfaceTemplate"
65+
null | TypeNodeKind.MAP || "interfaceTemplate"
66+
null | TypeNodeKind.ENUM || "enumTemplate"
67+
68+
"" | TypeNodeKind.SIMPLE || "interfaceTemplate"
69+
"" | TypeNodeKind.ARRAY || "interfaceTemplate"
70+
"" | TypeNodeKind.COLLECTION || "interfaceTemplate"
71+
"" | TypeNodeKind.MAP || "interfaceTemplate"
72+
"" | TypeNodeKind.ENUM || "enumTemplate"
73+
74+
"some value" | TypeNodeKind.SIMPLE || "some value"
75+
"some value" | TypeNodeKind.ARRAY || "some value"
76+
"some value" | TypeNodeKind.COLLECTION || "some value"
77+
"some value" | TypeNodeKind.MAP || "some value"
78+
"some value" | TypeNodeKind.ENUM || "some value"
79+
}
80+
81+
def "If templateConfiguration is null a MissingConfigurationTemplateException is thrown"() {
82+
given: "an Element annotated with TypeScriptEndpoint"
83+
TypeElement typeElement = Stub TypeElement
84+
85+
when:
86+
def actual = TypeNodeFactory.defineTemplate(typeElement, null, null, null)
87+
88+
then:
89+
MissingConfigurationTemplateException mcte = thrown()
90+
91+
and:
92+
mcte.element == typeElement
93+
}
94+
}

0 commit comments

Comments
 (0)