Skip to content

Commit

Permalink
fix: generated code missing import statements when using template ext…
Browse files Browse the repository at this point in the history
…ension (#244)

* import consumer when functionAsConsumer is true

* clean up integration tests; move common code into their own homes

* clean up code smells

* added missing semicolon because it really mattered (not)
  • Loading branch information
CameronRushton authored Feb 25, 2022
1 parent 2aaba6e commit 602f8b7
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 127 deletions.
4 changes: 2 additions & 2 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ function addtoExtraIncludesFromFunctionSpecs(asyncapi, params, extraIncludes) {
extraIncludes.needBean = true;
extraIncludes.needFunction = true;
}
if ((spec.type === 'supplier' && !(spec.dynamic && spec.dynamicType === 'streamBridge')) || spec.functionAsConsumer) {
if ((spec.type === 'supplier' && !(spec.dynamic && spec.dynamicType === 'streamBridge'))) {
extraIncludes.needBean = true;
extraIncludes.needSupplier = true;
}
if (spec.type === 'consumer') {
if (spec.type === 'consumer' || spec.functionAsConsumer) {
extraIncludes.needBean = true;
extraIncludes.needConsumer = true;
}
Expand Down
133 changes: 83 additions & 50 deletions test/__snapshots__/integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,89 @@ public class Application {
"
`;
exports[`template integration tests using the generator should generate a consumer and return a payload when using x-scs-function-name and dynamic topic binding 1`] = `
"
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
@SpringBootApplication
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
@Autowired
private StreamBridge streamBridge;
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
// This is a consumer that calls a send method, instead of a function, because it has a dynamic channel and we need streamBridge.
@Bean
public Consumer<MySchema> sameFunctionName() {
return data -> {
// Add business logic here.
logger.info(data.toString());
String messageId = \\"string\\";
String operation = \\"string\\";
MyOtherSchema payload = new MyOtherSchema();
sendSameFunctionName(payload, messageId, operation);
};
}
public void sendSameFunctionName(
MyOtherSchema payload, String messageId, String operation
) {
String topic = String.format(\\"testLevel1/%s/%s\\",
messageId, operation);
streamBridge.send(topic, payload);
}
}
"
`;
exports[`template integration tests using the generator should generate a function and return a payload when using x-scs-function-name and a static topic 1`] = `
"
import java.util.function.Function;
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 org.springframework.messaging.Message;
@SpringBootApplication
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public Function<Message<?>, Message<?>> sameFunctionName() {
return data -> {
// Add business logic here.
logger.info(data.toString());
return new Message<?>();
};
}
}
"
`;
exports[`template integration tests using the generator should generate a model subclass when it sees an allOf 1`] = `
"package com.acme;
Expand Down Expand Up @@ -1501,53 +1584,3 @@ public class Debtor {
}
"
`;
exports[`template integration tests using the generator should return payload when using x-scs-function-name instead of logging the message 1`] = `
"
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
@SpringBootApplication
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
@Autowired
private StreamBridge streamBridge;
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
// This is a consumer that calls a send method, instead of a function, because it has a dynamic channel and we need streamBridge.
@Bean
public Consumer<MySchema> sameFunctionName() {
return data -> {
// Add business logic here.
logger.info(data.toString());
String messageId = \\"string\\";
String operation = \\"string\\";
MyOtherSchema payload = new MyOtherSchema();
sendSameFunctionName(payload, messageId, operation);
};
}
public void sendSameFunctionName(
MyOtherSchema payload, String messageId, String operation
) {
String topic = String.format(\\"testLevel1/%s/%s\\",
messageId, operation);
streamBridge.send(topic, payload);
}
}
"
`;
Loading

0 comments on commit 602f8b7

Please sign in to comment.