Skip to content

Commit

Permalink
fix: suppliers and consumers return a function (#130)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
CameronRushton authored Jul 9, 2021
1 parent d1ef991 commit 0141591
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 7 deletions.
15 changes: 12 additions & 3 deletions template/src/main/java/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,19 @@ public static void main(String[] args) {
{% for funcName, funcSpec in funcs %}
@Bean
{{ funcSpec.functionSignature | safe }} {
// Add business logic here.
return null;
{%- if funcSpec.isSubscriber and funcSpec.type !== 'function' %}
return data -> {
// Add business logic here.
logger.info(data.toString());
};
{%- else %}
return data -> {
// Add business logic here.
return new {{ funcSpec.publishPayload | safe }}();
};
{%- endif %}
}
{%- endfor %}
{% endfor %}

{% if dynamicFuncs.size %}
/* Here is an example of how to send a message to a dynamic topic:
Expand Down
77 changes: 73 additions & 4 deletions test/__snapshots__/integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,22 @@ public class Application {
@Bean
public Supplier<MySchema> testLevel1MessageIdOperationSupplier() {
// Add business logic here.
return null;
return data -> {
// Add business logic here.
return new MySchema();
};
}
@Bean
public Consumer<MySchema> testLevel1MessageIdOperationConsumer() {
// Add business logic here.
return null;
return data -> {
// Add business logic here.
logger.info(data.toString());
};
}
/* Here is an example of how to send a message to a dynamic topic:
public void sendTestLevel1MessageIdOperation(
Expand Down Expand Up @@ -210,3 +216,66 @@ logging:
"
`;
exports[`template integration tests using the generator should return payload when using x-scs-function-name instead of logging the message 1`] = `
"import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.function.Function;
// Uncomment this if you want to use one of the sample functions commented out below.
/*
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.binder.BinderHeaders;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
*/
@SpringBootApplication
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
//Uncomment this if you want to use one of the sample functions commented out below.
/*
private static final String DYNAMIC_BINDING = \\"dynamic\\";
@Autowired
private StreamBridge streamBridge;
*/
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public Function<MySchema, MyOtherSchema> sameFunctionName() {
return data -> {
// Add business logic here.
return new MyOtherSchema();
};
}
/* Here is an example of how to send a message to a dynamic topic:
public void sendSameFunctionName(
MyOtherSchema payload, String messageId, String operation
) {
String topic = String.format(\\"testLevel1/%s/%s\\",
messageId, operation);
Message message = MessageBuilder
.withPayload(payload)
.setHeader(BinderHeaders.TARGET_DESTINATION, topic)
.build();
streamBridge.send(DYNAMIC_BINDING, message);
}
*/
}
"
`;
15 changes: 15 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ describe('template integration tests using the generator', () => {
expect(file).toMatchSnapshot();
}
});

it('should return payload when using x-scs-function-name instead of logging the message', async () => {
const OUTPUT_DIR = generateFolderName();

const generator = new Generator(path.normalize('./'), OUTPUT_DIR, { forceWrite: true });
await generator.generateFromFile(path.resolve('test', 'mocks/test-scs-function-name.yaml'));

const expectedFiles = [
'src/main/java/Application.java'
];
for (const index in expectedFiles) {
const file = await readFile(path.join(OUTPUT_DIR, expectedFiles[index]), 'utf8');
expect(file).toMatchSnapshot();
}
});
});
59 changes: 59 additions & 0 deletions test/mocks/test-scs-function-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
components:
schemas:
mySchema:
title: MySchema
type: object
properties:
prop1:
default: default string
title: PropertyTitle
type: string
myOtherSchema:
title: MyOtherSchema
type: object
properties:
prop1:
default: default string
title: PropertyTitle
type: string
messages:
myEvent:
payload:
$ref: '#/components/schemas/mySchema'
schemaFormat: application/vnd.aai.asyncapi+json;version=2.0.0
contentType: application/json
myOtherEvent:
payload:
$ref: '#/components/schemas/myOtherSchema'
schemaFormat: application/vnd.aai.asyncapi+json;version=2.0.0
contentType: application/json
channels:
'testLevel1/{messageId}/{operation}':
subscribe:
x-scs-function-name: sameFunctionName
message:
$ref: '#/components/messages/myOtherEvent'
publish:
x-scs-function-name: sameFunctionName
bindings:
smf:
topicSubscriptions:
- testLevel1/*/*
channelType: clientEndpoint
bindingVersion: 0.1.0
message:
$ref: '#/components/messages/myEvent'
parameters:
messageId:
schema:
type: string
operation:
schema:
type: string
enum:
- POST
- DELETE
asyncapi: 2.0.0
info:
title: solace-test-app
version: 0.0.1

0 comments on commit 0141591

Please sign in to comment.