Skip to content

Commit

Permalink
partition id validations in coral and legacy
Browse files Browse the repository at this point in the history
Signed-off-by: khatibtamal <[email protected]>
  • Loading branch information
khatibtamal committed Oct 3, 2024
1 parent 961df4b commit 129a4ff
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const mockGetTopicMessages = getTopicMessages as jest.MockedFunction<
typeof getTopicMessages
>;

const mockTopicOverview = {
topicInfo: {
noOfPartitions: 5,
},
};

const mockGetTopicMessagesResponse = {
0: "HELLO",
1: "WORLD",
Expand All @@ -24,7 +30,15 @@ const mockGetTopicMessagesNoContentResponse = {
const selectModeOptions = ["Default", "Custom", "Range"];

function DummyParent() {
return <Outlet context={{ topicName: "test", environmentId: "2" }} />;
return (
<Outlet
context={{
topicName: "test",
environmentId: "2",
topicOverview: mockTopicOverview,
}}
/>
);
}

describe("TopicMessages", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function isNoContentResult(
}

function TopicMessages() {
const { topicName, environmentId } = useTopicDetails();
const { topicName, environmentId, topicOverview } = useTopicDetails();
const numberOfPartitions = topicOverview.topicInfo.noOfPartitions;

const {
validateFilters,
Expand Down Expand Up @@ -76,7 +77,7 @@ function TopicMessages() {
const isConsuming = isInitialLoading || isRefetching;

function handleUpdateResultClick(): void {
const isValid = validateFilters();
const isValid = validateFilters(numberOfPartitions);

if (isValid) {
updateResults();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(true);
Expand All @@ -214,7 +214,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);
Expand All @@ -226,12 +226,67 @@ describe("useMessagesFilters.tsx", () => {
rangeOffsetEndFilters: null,
});
});
it("validateFilters returns false (negative partitionId)", () => {
const { result } = renderHook(() => useMessagesFilters(), {
wrapper: ({ children }) => (
<MemoryRouter
initialEntries={[
"/?defaultOffset=custom&customOffset=100&partitionId=-1",
]}
>
{children}
</MemoryRouter>
),
});

let isValid;

act(() => {
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);

expect(result.current.filterErrors).toStrictEqual({
customOffsetFilters: null,
partitionIdFilters: "Partition ID cannot be negative",
rangeOffsetStartFilters: null,
rangeOffsetEndFilters: null,
});
});
it("validateFilters returns false (invalid partitionId)", () => {
const { result } = renderHook(() => useMessagesFilters(), {
wrapper: ({ children }) => (
<MemoryRouter
initialEntries={[
"/?defaultOffset=custom&customOffset=100&partitionId=6",
]}
>
{children}
</MemoryRouter>
),
});

let isValid;

act(() => {
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);

expect(result.current.filterErrors).toStrictEqual({
customOffsetFilters: null,
partitionIdFilters: "Invalid partition ID",
rangeOffsetStartFilters: null,
rangeOffsetEndFilters: null,
});
});
it("validateFilters returns false (missing customOffset)", () => {
const { result } = renderHook(() => useMessagesFilters(), {
wrapper: ({ children }) => (
<MemoryRouter
initialEntries={["/?defaultOffset=custom&partitionId=100"]}
initialEntries={["/?defaultOffset=custom&partitionId=1"]}
>
{children}
</MemoryRouter>
Expand All @@ -241,7 +296,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);
Expand All @@ -259,7 +314,7 @@ describe("useMessagesFilters.tsx", () => {
wrapper: ({ children }) => (
<MemoryRouter
initialEntries={[
"/?defaultOffset=custom&customOffset=9999&partitionId=100",
"/?defaultOffset=custom&customOffset=9999&partitionId=1",
]}
>
{children}
Expand All @@ -270,7 +325,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);
Expand Down Expand Up @@ -433,7 +488,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(true);
Expand All @@ -460,7 +515,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);
Expand All @@ -486,7 +541,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);
Expand Down Expand Up @@ -514,7 +569,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);
Expand Down Expand Up @@ -542,7 +597,7 @@ describe("useMessagesFilters.tsx", () => {
let isValid;

act(() => {
isValid = result.current.validateFilters();
isValid = result.current.validateFilters(5);
});

expect(isValid).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const NAMES = {
const initialDefaultOffset: (typeof defaultOffsets)[0] = "5";

interface OffsetFilters {
validateFilters: () => boolean;
validateFilters: (totalNumberOfPartitions: number) => boolean;
filterErrors: FilterErrors;
getFetchingMode: () => TopicMessagesFetchModeTypes;
defaultOffsetFilters: {
Expand Down Expand Up @@ -70,15 +70,19 @@ function useMessagesFilters(): OffsetFilters {
rangeOffsetEndFilters: null,
});

function validateFilters() {
function validateFilters(totalNumberOfPartitions: number) {
if (getFetchingMode() === "default") {
return true;
}

const partitionIdFiltersError =
getPartitionId() === "" || getPartitionId() === null
? "Please enter a partition ID"
: null;
: Number(getPartitionId()) < 0
? "Partition ID cannot be negative"
: Number(getPartitionId()) >= totalNumberOfPartitions
? "Invalid partition ID"
: null;

let customOffsetFiltersError = null;
let rangeOffsetStartFiltersError = null;
Expand Down Expand Up @@ -269,7 +273,7 @@ function useMessagesFilters(): OffsetFilters {
}

function setPartitionId(partitionId: string): void {
if (getDefaultOffset() !== "custom") {
if (getDefaultOffset() !== "custom" && getDefaultOffset() !== "range") {
setDefaultOffset("custom");
}
if (partitionId.length === 0) {
Expand Down
21 changes: 17 additions & 4 deletions core/src/main/resources/static/js/browseAcls.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,13 @@ app.controller("browseAclsCtrl", function($scope, $http, $location, $window) {
return;
}

if($scope.selectedPartitionId >= $scope.topicOverview[0].noOfPartitions)
{
$scope.alert = "Please fill in a valid partition id.";
$scope.showAlertToast();
return;
}

if(!$scope.selectedNumberOfOffsets || $scope.selectedNumberOfOffsets === ""){
$scope.alert = "Please fill how many events/offsets to be displayed";
$scope.showAlertToast();
Expand Down Expand Up @@ -928,16 +935,22 @@ app.controller("browseAclsCtrl", function($scope, $http, $location, $window) {
return;
}

if(!$scope.selectedOffsetRangeStart || $scope.selectedOffsetRangeStart === "" ||
!$scope.selectedOffsetRangeEnd || $scope.selectedOffsetRangeEnd === ""
if($scope.selectedPartitionId >= $scope.topicOverview[0].noOfPartitions)
{
$scope.alert = "Please fill in a valid partition id.";
$scope.showAlertToast();
return;
}

if($scope.selectedOffsetRangeStart === "" || $scope.selectedOffsetRangeEnd === ""
){
$scope.alert = "Please fill how many offsets range start and end.";
$scope.showAlertToast();
return;
}

if($scope.selectedOffsetRangeStart <= 0 || isNaN($scope.selectedOffsetRangeStart) ||
$scope.selectedOffsetRangeEnd <= 0 || isNaN($scope.selectedOffsetRangeEnd))
if($scope.selectedOffsetRangeStart < 0 || isNaN($scope.selectedOffsetRangeStart) ||
$scope.selectedOffsetRangeEnd < 0 || isNaN($scope.selectedOffsetRangeEnd))
{
$scope.alert = "Please fill in a valid number topic offsets start and end.";
$scope.showAlertToast();
Expand Down

0 comments on commit 129a4ff

Please sign in to comment.