Skip to content

feat(core): show publish button in ui only when publishing is enabled #766

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -25,6 +26,14 @@ public abstract class PublishingBaseController implements InitializingBean {

protected abstract void publishMessage(String topic, MessageDto message, Object payload);

@GetMapping("/publish")
public ResponseEntity<String> canPublish() {
if (!isEnabled()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok().build();
}

@PostMapping("/publish")
public ResponseEntity<String> publish(@RequestParam String topic, @RequestBody MessageDto message) {
if (!isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Data;
import lombok.extern.jackson.Jacksonized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
Expand All @@ -40,6 +41,7 @@
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -95,6 +97,23 @@ void setup() {
.build());
}

@Nested
class CanPublish {
@Test
void testControllerShouldReturnOkWhenPublishingIsEnabled() throws Exception {
mvc.perform(get("/springwolf/jms/publish").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful());
}

@Test
void testControllerShouldReturnNotFoundWhenPublishingIsDisabled() throws Exception {
when(springwolfJmsProducer.isEnabled()).thenReturn(false);

mvc.perform(get("/springwolf/jms/publish").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is4xxClientError());
}
}

@Test
void testControllerShouldReturnBadRequestIfPayloadIsEmpty() throws Exception {
String content =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Data;
import lombok.extern.jackson.Jacksonized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
Expand All @@ -40,6 +41,7 @@
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -95,6 +97,23 @@ void setup() {
.build());
}

@Nested
class CanPublish {
@Test
void testControllerShouldReturnOkWhenPublishingIsEnabled() throws Exception {
mvc.perform(get("/springwolf/kafka/publish").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful());
}

@Test
void testControllerShouldReturnNotFoundWhenPublishingIsDisabled() throws Exception {
when(springwolfKafkaProducer.isEnabled()).thenReturn(false);

mvc.perform(get("/springwolf/kafka/publish").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is4xxClientError());
}
}

@Test
void testControllerShouldReturnBadRequestIfPayloadIsEmpty() throws Exception {
String content =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ <h4>Message</h4>
<button
mat-raised-button
color="primary"
[disabled]="!canPublish"
(click)="
publish(
messageTextArea.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Schema } from "../../../models/schema.model";
import { AsyncApiService } from "../../../service/asyncapi/asyncapi.service";
import { PublisherService } from "../../../service/publisher.service";
import { wrapException } from "../../../util/error-boundary";
import { Subscription } from "rxjs";

@Component({
selector: "app-channel-main",
Expand All @@ -30,6 +31,7 @@ export class ChannelMainComponent implements OnInit {
headersTextAreaLineCount: number = 1;
messageBindingExample?: Example;
messageBindingExampleTextAreaLineCount: number = 1;
canPublish: boolean = false;

constructor(
private asyncApiService: AsyncApiService,
Expand Down Expand Up @@ -57,6 +59,12 @@ export class ChannelMainComponent implements OnInit {
this.headersTextAreaLineCount = this.headersExample?.lineCount || 1;
this.messageBindingExampleTextAreaLineCount =
this.messageBindingExample?.lineCount || 1;

this.publisherService
.canPublish(this.operation.protocol)
.subscribe((response) => {
this.canPublish = response;
});
});
}

Expand Down
6 changes: 5 additions & 1 deletion springwolf-ui/src/app/service/mock/mock-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ export class MockServer implements InMemoryDbService {
}

get(reqInfo: RequestInfo) {
console.log("Returning mock data");
if (reqInfo.req.url.endsWith("/docs")) {
console.log("Returning mock data");
const body = this.selectMockData();
return reqInfo.utils.createResponse$(() => {
return {
status: STATUS.OK,
body: body,
};
});
} else if (reqInfo.req.url.endsWith("/publish")) {
return reqInfo.utils.createResponse$(() => {
return { status: STATUS.OK, body: {} };
});
}

return undefined;
Expand Down
18 changes: 17 additions & 1 deletion springwolf-ui/src/app/service/publisher.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
/* SPDX-License-Identifier: Apache-2.0 */
import { Injectable } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Observable } from "rxjs";
import { Observable, map, share } from "rxjs";
import { EndpointService } from "./endpoint.service";

@Injectable()
export class PublisherService {
constructor(private http: HttpClient) {}
publishable: { [key: string]: Observable<boolean> } = {};

canPublish(protocol: string): Observable<boolean> {
if (this.publishable[protocol] === undefined) {
this.publishable[protocol] = this.http
.get<undefined>(EndpointService.getPublishEndpoint(protocol), {
observe: "response",
})
.pipe(
map((response: any) => response.status === 200),
share()
);
}

return this.publishable[protocol];
}

publish(
protocol: string,
Expand Down