-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added test for spring messaging consumer bean
- Loading branch information
Sheheryar
authored and
Sheheryar
committed
Nov 8, 2023
1 parent
fd05041
commit 07b745d
Showing
89 changed files
with
1,386 additions
and
492 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
SPRINGWOLF_VERSION=0.16.0-SNAPSHOT | ||
SPRINGWOLF_VERSION=0.17.0-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...in/java/io/github/stavshamir/springwolf/asyncapi/controller/PublishingBaseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.asyncapi.controller; | ||
|
||
import io.github.stavshamir.springwolf.asyncapi.controller.dtos.MessageDto; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
/** | ||
* Used in plugins with publishing enabled. | ||
* Located in springwolf-core to allow sharing of code | ||
*/ | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public abstract class PublishingBaseController implements InitializingBean { | ||
|
||
private final PublishingPayloadCreator publishingPayloadCreator; | ||
|
||
protected abstract boolean isEnabled(); | ||
|
||
protected abstract void publishMessage(String topic, MessageDto message, Object payload); | ||
|
||
@PostMapping("/publish") | ||
public ResponseEntity<String> publish(@RequestParam String topic, @RequestBody MessageDto message) { | ||
if (!isEnabled()) { | ||
String errorMessage = "Publishing using %s is not enabled - message will not be published" | ||
.formatted(this.getClass().getSimpleName()); | ||
log.warn(errorMessage); | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorMessage); | ||
} | ||
|
||
PublishingPayloadCreator.Result result = publishingPayloadCreator.createPayloadObject(message); | ||
if (result.payload() != null) { | ||
publishMessage(topic, message, result.payload()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
return ResponseEntity.badRequest().body(result.errorMessage()); | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
log.debug( | ||
"Message publishing via %s is active.".formatted(this.getClass().getSimpleName())); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...in/java/io/github/stavshamir/springwolf/asyncapi/controller/PublishingPayloadCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.asyncapi.controller; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.stavshamir.springwolf.asyncapi.controller.dtos.MessageDto; | ||
import io.github.stavshamir.springwolf.schemas.SchemasService; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.text.MessageFormat; | ||
|
||
/** | ||
* Used in plugins with publishing enabled. | ||
* Located in springwolf-core to allow sharing of code | ||
*/ | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PublishingPayloadCreator { | ||
|
||
private final SchemasService schemasService; | ||
private final ObjectMapper objectMapper; | ||
|
||
public Result createPayloadObject(MessageDto message) { | ||
|
||
String messagePayloadType = message.getPayloadType(); | ||
for (Schema<?> value : schemasService.getDefinitions().values()) { | ||
String schemaPayloadType = value.getName(); | ||
// security: match against user input, but always use our controlled data from the DefaultSchemaService | ||
if (schemaPayloadType.equals(messagePayloadType)) { | ||
try { | ||
Class<?> payloadClass = Class.forName(schemaPayloadType); | ||
Object payload = objectMapper.readValue(message.getPayload(), payloadClass); | ||
return new Result(payload, null); | ||
} catch (ClassNotFoundException | JsonProcessingException ex) { | ||
String errorMessage = MessageFormat.format( | ||
"Unable to create payload {0} from data: {1}", schemaPayloadType, message.getPayload()); | ||
log.info(errorMessage, ex); | ||
return new Result(null, errorMessage); | ||
} | ||
} | ||
} | ||
|
||
String errorMessage = MessageFormat.format( | ||
"Specified payloadType {0} is not a registered springwolf schema.", messagePayloadType); | ||
log.info(errorMessage); | ||
return new Result(null, errorMessage); | ||
} | ||
|
||
public record Result(Object payload, String errorMessage) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...wolf/asyncapi/types/channel/operation/message/header/AsyncHeadersCloudEventConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.asyncapi.types.channel.operation.message.header; | ||
|
||
public class AsyncHeadersCloudEventConstants { | ||
public static final String CONTENT_TYPE = "content-type"; | ||
public static final String CONTENT_TYPE_DESC = "CloudEvent Content-Type Header"; | ||
public static final String ID = "ce_id"; | ||
public static final String ID_DESC = "CloudEvent Id Header"; | ||
public static final String SPECVERSION = "ce_specversion"; | ||
public static final String SPECVERSION_DESC = "CloudEvent Spec Version Header"; | ||
public static final String SOURCE = "ce_source"; | ||
public static final String SOURCE_DESC = "CloudEvent Source Header"; | ||
public static final String SUBJECT = "ce_subject"; | ||
public static final String SUBJECT_DESC = "CloudEvent Subject Header"; | ||
public static final String TIME = "ce_time"; | ||
public static final String TIME_DESC = "CloudEvent Time Header"; | ||
public static final String TYPE = "ce_type"; | ||
public static final String TYPE_DESC = "CloudEvent Payload Type Header"; | ||
} |
Oops, something went wrong.