Skip to content

Commit 0141591

Browse files
fix: suppliers and consumers return a function (#130)
* test: Added basic jest testing and coverage reports * test: fixed descibte test suite name * test: merge master * test: remove husky install script * test: removed husky precommits; removed coverage setup for later PR; reverted eslintrc rename; use snapshot testing * test: use const instead of let * chore: add sonar exclusion for entire test directory * chore: add correct path for sonar exclusion * chore: add correct path for sonar exclusion * feat: suppliers and consumers return functions * test: Remove exclusion; try using nosonar * fix: missing semicolon * fix: support functions instead of just suppliers and consumers * fix: syntax typo - change && to the word and * test: created test with x-scs-function-name * chore: fix linting - switched quote types on string
1 parent d1ef991 commit 0141591

File tree

4 files changed

+159
-7
lines changed

4 files changed

+159
-7
lines changed

template/src/main/java/Application.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,19 @@ public static void main(String[] args) {
7070
{% for funcName, funcSpec in funcs %}
7171
@Bean
7272
{{ funcSpec.functionSignature | safe }} {
73-
// Add business logic here.
74-
return null;
73+
{%- if funcSpec.isSubscriber and funcSpec.type !== 'function' %}
74+
return data -> {
75+
// Add business logic here.
76+
logger.info(data.toString());
77+
};
78+
{%- else %}
79+
return data -> {
80+
// Add business logic here.
81+
return new {{ funcSpec.publishPayload | safe }}();
82+
};
83+
{%- endif %}
7584
}
76-
{%- endfor %}
85+
{% endfor %}
7786

7887
{% if dynamicFuncs.size %}
7988
/* Here is an example of how to send a message to a dynamic topic:

test/__snapshots__/integration.test.js.snap

+73-4
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,22 @@ public class Application {
113113
114114
@Bean
115115
public Supplier<MySchema> testLevel1MessageIdOperationSupplier() {
116-
// Add business logic here.
117-
return null;
116+
return data -> {
117+
// Add business logic here.
118+
return new MySchema();
119+
};
118120
}
121+
119122
@Bean
120123
public Consumer<MySchema> testLevel1MessageIdOperationConsumer() {
121-
// Add business logic here.
122-
return null;
124+
return data -> {
125+
// Add business logic here.
126+
logger.info(data.toString());
127+
};
123128
}
124129
125130
131+
126132
/* Here is an example of how to send a message to a dynamic topic:
127133
128134
public void sendTestLevel1MessageIdOperation(
@@ -210,3 +216,66 @@ logging:
210216
211217
"
212218
`;
219+
220+
exports[`template integration tests using the generator should return payload when using x-scs-function-name instead of logging the message 1`] = `
221+
"import org.slf4j.Logger;
222+
import org.slf4j.LoggerFactory;
223+
import org.springframework.boot.SpringApplication;
224+
import org.springframework.boot.autoconfigure.SpringBootApplication;
225+
import org.springframework.context.annotation.Bean;
226+
227+
import java.util.function.Function;
228+
229+
// Uncomment this if you want to use one of the sample functions commented out below.
230+
/*
231+
import org.springframework.beans.factory.annotation.Autowired;
232+
import org.springframework.cloud.stream.binder.BinderHeaders;
233+
import org.springframework.cloud.stream.function.StreamBridge;
234+
import org.springframework.messaging.Message;
235+
import org.springframework.messaging.support.MessageBuilder;
236+
*/
237+
238+
239+
240+
@SpringBootApplication
241+
public class Application {
242+
243+
private static final Logger logger = LoggerFactory.getLogger(Application.class);
244+
//Uncomment this if you want to use one of the sample functions commented out below.
245+
/*
246+
private static final String DYNAMIC_BINDING = \\"dynamic\\";
247+
@Autowired
248+
private StreamBridge streamBridge;
249+
*/
250+
251+
public static void main(String[] args) {
252+
SpringApplication.run(Application.class);
253+
}
254+
255+
@Bean
256+
public Function<MySchema, MyOtherSchema> sameFunctionName() {
257+
return data -> {
258+
// Add business logic here.
259+
return new MyOtherSchema();
260+
};
261+
}
262+
263+
264+
265+
/* Here is an example of how to send a message to a dynamic topic:
266+
267+
public void sendSameFunctionName(
268+
MyOtherSchema payload, String messageId, String operation
269+
) {
270+
String topic = String.format(\\"testLevel1/%s/%s\\",
271+
messageId, operation);
272+
Message message = MessageBuilder
273+
.withPayload(payload)
274+
.setHeader(BinderHeaders.TARGET_DESTINATION, topic)
275+
.build();
276+
streamBridge.send(DYNAMIC_BINDING, message);
277+
}
278+
*/
279+
}
280+
"
281+
`;

test/integration.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,19 @@ describe('template integration tests using the generator', () => {
4242
expect(file).toMatchSnapshot();
4343
}
4444
});
45+
46+
it('should return payload when using x-scs-function-name instead of logging the message', async () => {
47+
const OUTPUT_DIR = generateFolderName();
48+
49+
const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true });
50+
await generator.generateFromFile(path.resolve('test', 'mocks/test-scs-function-name.yaml'));
51+
52+
const expectedFiles = [
53+
'src/main/java/Application.java'
54+
];
55+
for (const index in expectedFiles) {
56+
const file = await readFile(path.join(OUTPUT_DIR, expectedFiles[index]), 'utf8');
57+
expect(file).toMatchSnapshot();
58+
}
59+
});
4560
});
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
components:
2+
schemas:
3+
mySchema:
4+
title: MySchema
5+
type: object
6+
properties:
7+
prop1:
8+
default: default string
9+
title: PropertyTitle
10+
type: string
11+
myOtherSchema:
12+
title: MyOtherSchema
13+
type: object
14+
properties:
15+
prop1:
16+
default: default string
17+
title: PropertyTitle
18+
type: string
19+
messages:
20+
myEvent:
21+
payload:
22+
$ref: '#/components/schemas/mySchema'
23+
schemaFormat: application/vnd.aai.asyncapi+json;version=2.0.0
24+
contentType: application/json
25+
myOtherEvent:
26+
payload:
27+
$ref: '#/components/schemas/myOtherSchema'
28+
schemaFormat: application/vnd.aai.asyncapi+json;version=2.0.0
29+
contentType: application/json
30+
channels:
31+
'testLevel1/{messageId}/{operation}':
32+
subscribe:
33+
x-scs-function-name: sameFunctionName
34+
message:
35+
$ref: '#/components/messages/myOtherEvent'
36+
publish:
37+
x-scs-function-name: sameFunctionName
38+
bindings:
39+
smf:
40+
topicSubscriptions:
41+
- testLevel1/*/*
42+
channelType: clientEndpoint
43+
bindingVersion: 0.1.0
44+
message:
45+
$ref: '#/components/messages/myEvent'
46+
parameters:
47+
messageId:
48+
schema:
49+
type: string
50+
operation:
51+
schema:
52+
type: string
53+
enum:
54+
- POST
55+
- DELETE
56+
asyncapi: 2.0.0
57+
info:
58+
title: solace-test-app
59+
version: 0.0.1

0 commit comments

Comments
 (0)