Skip to content

Commit

Permalink
Merge pull request #400
Browse files Browse the repository at this point in the history
feat(20611): Add uniqueness validation to topic filter

* feat(20611): add custom validation to the topic filter

* feat(20611): fix error messages

* fix(20611): simplify default payload for topic and client filters

* test(20611): fix test

* test(20611): fix test

* test(20611): add test

* fix(20611): fix translations
  • Loading branch information
vanch3d authored May 3, 2024
1 parent 8e3e6d0 commit 62fb35e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/// <reference types="cypress" />

import { Button } from '@chakra-ui/react'
import { Node } from 'reactflow'

import { MockStoreWrapper } from '@datahub/__test-utils__/MockStoreWrapper.tsx'
import { DataHubNodeType } from '@datahub/types.ts'
import { getNodePayload } from '@datahub/utils/node.utils.ts'
import { ClientFilterData, DataHubNodeType } from '@datahub/types.ts'

import { ClientFilterPanel } from './ClientFilterPanel.tsx'

const MOCK_CLIENT_FILTER: Node<ClientFilterData> = {
id: '3',
type: DataHubNodeType.CLIENT_FILTER,
position: { x: 0, y: 0 },
data: { clients: ['client10', 'client20', 'client30'] },
}

const wrapper: React.JSXElementConstructor<{ children: React.ReactNode }> = ({ children }) => (
<MockStoreWrapper
config={{
initialState: {
nodes: [
{
id: '3',
type: DataHubNodeType.CLIENT_FILTER,
position: { x: 0, y: 0 },
data: getNodePayload(DataHubNodeType.CLIENT_FILTER),
},
],
nodes: [MOCK_CLIENT_FILTER],
},
}}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
/// <reference types="cypress" />

import { Button } from '@chakra-ui/react'
import { Node } from 'reactflow'

import { MockStoreWrapper } from '@datahub/__test-utils__/MockStoreWrapper.tsx'
import { DataHubNodeType } from '@datahub/types.ts'
import { getNodePayload } from '@datahub/utils/node.utils.ts'
import { DataHubNodeType, TopicFilterData } from '@datahub/types.ts'
import { TopicFilterPanel } from './TopicFilterPanel.tsx'
import { mockDataPolicy } from '@datahub/api/hooks/DataHubDataPoliciesService/__handlers__'

const MOCK_TOPIC_FILTER: Node<TopicFilterData> = {
id: '3',
type: DataHubNodeType.CLIENT_FILTER,
position: { x: 0, y: 0 },
data: { topics: ['root/test1', 'root/test2', 'root/test1', 'root/topic/ref/1'] },
}

const wrapper: React.JSXElementConstructor<{ children: React.ReactNode }> = ({ children }) => (
<MockStoreWrapper
config={{
initialState: {
nodes: [
{
id: '3',
type: DataHubNodeType.TOPIC_FILTER,
position: { x: 0, y: 0 },
data: getNodePayload(DataHubNodeType.TOPIC_FILTER),
},
],
nodes: [MOCK_TOPIC_FILTER],
},
}}
>
Expand All @@ -33,6 +34,7 @@ describe('TopicFilterPanel', () => {
beforeEach(() => {
cy.viewport(800, 800)
cy.intercept('/api/v1/management/protocol-adapters/adapters', { statusCode: 404 })
cy.intercept('/api/v1/data-hub/data-validation/policies', { statusCode: 404 })
})

it('should render the fields for a Validator', () => {
Expand All @@ -47,6 +49,22 @@ describe('TopicFilterPanel', () => {
cy.get('label#root_topics_1-label + input').should('have.value', 'root/test2')
})

it('should validate properly the topic filters', () => {
cy.intercept('/api/v1/data-hub/data-validation/policies', {
items: [mockDataPolicy],
}).as('getPolicies')
cy.mountWithProviders(<TopicFilterPanel selectedNode="3" />, { wrapper })
cy.get('label#root_topics_0-label').should('have.attr', 'data-invalid')
cy.get('label#root_topics_1-label').should('not.have.attr', 'data-invalid')
cy.get('label#root_topics_2-label').should('have.attr', 'data-invalid')
cy.get('label#root_topics_3-label').should('not.have.attr', 'data-invalid')

cy.wait('@getPolicies')

cy.get('button[type=submit]').click()
cy.get('label#root_topics_3-label').should('have.attr', 'data-invalid')
})

it('should be accessible', () => {
cy.injectAxe()
cy.mountWithProviders(<TopicFilterPanel selectedNode="3" />, { wrapper })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ import { datahubRJSFWidgets } from '@datahub/designer/datahubRJSFWidgets.tsx'
import useDataHubDraftStore from '@datahub/hooks/useDataHubDraftStore.ts'
import { validateDuplicates } from '@datahub/utils/rjsf.utils.ts'
import { ReactFlowSchemaForm } from '@datahub/components/forms/'
import { useGetAllDataPolicies } from '@datahub/api/hooks/DataHubDataPoliciesService/useGetAllDataPolicies.tsx'

export const TopicFilterPanel: FC<PanelProps> = ({ selectedNode, onFormSubmit }) => {
const { t } = useTranslation('datahub')
const { isLoading, data } = useGetAllDataPolicies()
const { nodes } = useDataHubDraftStore()

const listFilters = useMemo(() => {
if (isLoading || !data) return undefined
return data.items?.map((e) => e.matching.topicFilter)
}, [data, isLoading])

const formData = useMemo(() => {
const adapterNode = nodes.find((e) => e.id === selectedNode) as Node<TopicFilterData> | undefined
return adapterNode ? adapterNode.data : null
Expand All @@ -27,11 +34,16 @@ export const TopicFilterPanel: FC<PanelProps> = ({ selectedNode, onFormSubmit })
if (hasDuplicate) {
for (const [key, value] of duplicates) {
for (const index of value) {
errors['topics']?.[index]?.addError(t(`the topic ${key} is already defined`))
errors['topics']?.[index]?.addError(t('error.validation.topicFilter.duplicate', { filter: key }))
}
}
}

for (const [index, value] of (formData?.['topics'] || []).entries()) {
if (listFilters?.includes(value))
errors['topics']?.[index]?.addError(t('error.validation.topicFilter.alreadyMatching', { filter: value }))
}

return errors
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@
"protobuf": {
"template": "// CANNOT DECODE THE PROTOBUF SOURCE - {{ source }}",
"encoding": "The encoding of the PROTOBUF code into a Base64 descriptor cannot be validated."
},
"topicFilter": {
"duplicate": "The topic filter {{ filter }} is already defined",
"alreadyMatching": "A policy is already matching the topic filter {{ filter }}"
}
},
"loading": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ describe('getNodePayload', () => {
{
type: DataHubNodeType.TOPIC_FILTER,
expected: {
topics: ['root/test1', 'root/test2'],
topics: ['topic/example/1'],
},
},
{
type: DataHubNodeType.CLIENT_FILTER,
expected: {
clients: ['client10', 'client20', 'client30'],
clients: ['client/example/1'],
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export const getNodeId = (stub = 'node') => `${stub}_${uuidv4()}`
export const getNodePayload = (type: string): DataHubNodeData => {
if (type === DataHubNodeType.TOPIC_FILTER) {
return {
topics: ['root/test1', 'root/test2'],
topics: ['topic/example/1'],
} as TopicFilterData
}
if (type === DataHubNodeType.CLIENT_FILTER) {
return {
clients: ['client10', 'client20', 'client30'],
clients: ['client/example/1'],
} as ClientFilterData
}

Expand Down

0 comments on commit 62fb35e

Please sign in to comment.