New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#413] Add /metadata.set endpoint #414
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f155c2
skeleton for set metadata endpoint
paulodiniz cc75d99
unused imports
paulodiniz 620cee4
added test
paulodiniz 8b38ccf
Added docs
paulodiniz 6a98251
Validating request
paulodiniz d77405f
Better docs structure for metadata
paulodiniz 4a40523
Better presentation for Key/Value table on metadata
paulodiniz 599b557
Introduces user public namespace
paulodiniz 9a6062c
Apply suggestions from code review
paulodiniz 2e3de90
Deleting unnecessary test
paulodiniz File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...nd/api/communication/src/main/java/co/airy/core/api/communication/MetadataController.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,41 @@ | ||
package co.airy.core.api.communication; | ||
|
||
import co.airy.avro.communication.MetadataAction; | ||
import co.airy.avro.communication.MetadataActionType; | ||
import co.airy.core.api.communication.payload.SetMetadataRequestPayload; | ||
import co.airy.payload.response.EmptyResponsePayload; | ||
import co.airy.payload.response.RequestErrorResponsePayload; | ||
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.RestController; | ||
|
||
import javax.validation.Valid; | ||
import java.time.Instant; | ||
|
||
@RestController | ||
public class MetadataController { | ||
private final Stores stores; | ||
|
||
public MetadataController(Stores stores) { | ||
this.stores = stores; | ||
} | ||
|
||
@PostMapping("/metadata.set") | ||
ResponseEntity<?> setMetadata(@RequestBody @Valid SetMetadataRequestPayload setMetadataRequestPayload) { | ||
final MetadataAction metadataAction = MetadataAction.newBuilder() | ||
.setActionType(MetadataActionType.SET) | ||
.setTimestamp(Instant.now().toEpochMilli()) | ||
.setConversationId(setMetadataRequestPayload.getConversationId()) | ||
.setValue(setMetadataRequestPayload.getValue()) | ||
.setKey(setMetadataRequestPayload.getKey()) | ||
.build(); | ||
try { | ||
stores.storeMetadata(metadataAction); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new RequestErrorResponsePayload(e.getMessage())); | ||
} | ||
return ResponseEntity.ok(new EmptyResponsePayload()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ation/src/main/java/co/airy/core/api/communication/payload/SetMetadataRequestPayload.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,23 @@ | ||
package co.airy.core.api.communication.payload; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Pattern; | ||
|
||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Data | ||
public class SetMetadataRequestPayload { | ||
@NotNull | ||
private String conversationId; | ||
@NotNull | ||
@Pattern(regexp = "^((?!__.*__).)*$") | ||
private String key; | ||
@NotNull | ||
private String value; | ||
} |
102 changes: 102 additions & 0 deletions
102
...pi/communication/src/test/java/co/airy/core/api/communication/MetadataControllerTest.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,102 @@ | ||
package co.airy.core.api.communication; | ||
|
||
import co.airy.avro.communication.Channel; | ||
import co.airy.avro.communication.ChannelConnectionState; | ||
import co.airy.core.api.communication.util.TestConversation; | ||
import co.airy.kafka.schema.application.ApplicationCommunicationChannels; | ||
import co.airy.kafka.schema.application.ApplicationCommunicationMessages; | ||
import co.airy.kafka.schema.application.ApplicationCommunicationMetadata; | ||
import co.airy.kafka.schema.application.ApplicationCommunicationReadReceipts; | ||
import co.airy.kafka.test.KafkaTestHelper; | ||
import co.airy.kafka.test.junit.SharedKafkaTestResource; | ||
import co.airy.spring.core.AirySpringBootApplication; | ||
import co.airy.spring.test.WebTestHelper; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import java.util.UUID; | ||
|
||
import static co.airy.test.Timing.retryOnException; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AirySpringBootApplication.class) | ||
@TestPropertySource(value = "classpath:test.properties") | ||
@ExtendWith(SpringExtension.class) | ||
@AutoConfigureMockMvc | ||
public class MetadataControllerTest { | ||
@RegisterExtension | ||
public static final SharedKafkaTestResource sharedKafkaTestResource = new SharedKafkaTestResource(); | ||
|
||
private static KafkaTestHelper kafkaTestHelper; | ||
|
||
@Autowired | ||
private WebTestHelper webTestHelper; | ||
|
||
private static final ApplicationCommunicationMessages applicationCommunicationMessages = new ApplicationCommunicationMessages(); | ||
private static final ApplicationCommunicationChannels applicationCommunicationChannels = new ApplicationCommunicationChannels(); | ||
private static final ApplicationCommunicationMetadata applicationCommunicationMetadata = new ApplicationCommunicationMetadata(); | ||
private static final ApplicationCommunicationReadReceipts applicationCommunicationReadReceipts = new ApplicationCommunicationReadReceipts(); | ||
|
||
@BeforeAll | ||
static void beforeAll() throws Exception { | ||
kafkaTestHelper = new KafkaTestHelper(sharedKafkaTestResource, | ||
applicationCommunicationMessages, | ||
applicationCommunicationChannels, | ||
applicationCommunicationMetadata, | ||
applicationCommunicationReadReceipts); | ||
|
||
kafkaTestHelper.beforeAll(); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() throws Exception { | ||
kafkaTestHelper.afterAll(); | ||
} | ||
|
||
@BeforeEach | ||
void beforeEach() throws Exception { | ||
webTestHelper.waitUntilHealthy(); | ||
} | ||
@Test | ||
void canSetMetadata() throws Exception { | ||
final Channel channel = Channel.newBuilder() | ||
.setConnectionState(ChannelConnectionState.CONNECTED) | ||
.setId("channel-id") | ||
.setName("channel-name") | ||
.setSource("facebook") | ||
.setSourceChannelId("ps-id") | ||
.build(); | ||
final String conversationId = UUID.randomUUID().toString(); | ||
|
||
kafkaTestHelper.produceRecord(new ProducerRecord<>(applicationCommunicationChannels.name(), channel.getId(), channel)); | ||
kafkaTestHelper.produceRecords(TestConversation.generateRecords(conversationId, channel, 1)); | ||
|
||
retryOnException( | ||
() -> webTestHelper.post("/metadata.set", | ||
"{\"conversation_id\":\"" + conversationId + "\", \"key\": \"awesome.key\", \"value\": \"awesome-value\"}", | ||
"user-id") | ||
.andExpect(status().isOk()), | ||
"Error setting metadata" | ||
); | ||
|
||
retryOnException( | ||
() -> webTestHelper.post("/metadata.set", | ||
"{\"conversation_id\":\"" + conversationId + "\", \"key\": \"__private.key__\", \"value\": \"awesome-value\"}", | ||
"user-id") | ||
.andExpect(status().isBadRequest()), | ||
"Error setting metadata" | ||
paulodiniz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL