Skip to content
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

Fix #269 #272

Merged
merged 2 commits into from
Oct 24, 2024
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
158 changes: 144 additions & 14 deletions src/__tests__/modeler/modeler-integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,149 @@
import { ModelerApiClient } from '../../modeler/index'
import fs from 'fs'

test('It can get info', async () => {
const modeler = new ModelerApiClient()
import { ModelerApiClient } from '../../modeler'

const res = await modeler.getInfo()
expect(res.version).toBe('v1')
})
describe('ModelerApiClient', () => {
let modeler: ModelerApiClient

beforeAll(() => {
modeler = new ModelerApiClient()
})

afterAll(async () => {
// Cleanup any remaining test data
const existingProjects = await modeler.searchProjects({
filter: { name: '__test__' },
})
for (const project of existingProjects.items) {
const projectData = await modeler.getProject(project.id)
for (const file of projectData.content.files) {
await modeler.deleteFile(file.id)
}
for (const folder of projectData.content.folders) {
await modeler.deleteFolder(folder.id)
}
await modeler.deleteProject(project.id)
}
})

describe('createProject', () => {
it('should create a new project', async () => {
const projectResponse = await modeler.createProject('__test__')
expect(projectResponse.name).toBe('__test__')
})
})

describe('getProject', () => {
it('should retrieve an existing project', async () => {
const projectResponse = await modeler.createProject('__test__')
const retrievedProject = await modeler.getProject(projectResponse.id)
expect(retrievedProject.metadata.name).toBe('__test__')
})

it('should throw an error if the project does not exist', async () => {
await expect(modeler.getProject('non-existent-id')).rejects.toThrow()
})
})

describe('updateProject', () => {
it('should update an existing project', async () => {
const projectResponse = await modeler.createProject('__test__')
const updatedProject = await modeler.renameProject(
projectResponse.id,
'__test__ updated'
)
expect(updatedProject.name).toBe('__test__ updated')
})

it('should throw an error if the project does not exist', async () => {
await expect(
modeler.renameProject('non-existent-id', 'Updated name')
).rejects.toThrow()
})
})

describe('deleteProject', () => {
it('should delete an existing project', async () => {
const projectResponse = await modeler.createProject('__test__')
await modeler.deleteProject(projectResponse.id)
await expect(modeler.getProject(projectResponse.id)).rejects.toThrow()
})

it('should throw an error if the project does not exist', async () => {
await expect(modeler.deleteProject('non-existent-id')).rejects.toThrow()
})
})

describe('searchProjects', () => {
it('should return a list of projects matching the filter', async () => {
await modeler.createProject('__test__')
const searchResponse = await modeler.searchProjects({
filter: { name: '__test__' },
})
expect(searchResponse.items.length).toBeGreaterThan(0)
expect(searchResponse.items[0].name).toBe('__test__')
})

it('should return an empty list if no projects match the filter', async () => {
const searchResponse = await modeler.searchProjects({
filter: { name: 'non-existent-project' },
})
expect(searchResponse.items.length).toBe(0)
})
})

describe('createFolder', () => {
it('should create a new folder in an existing project', async () => {
const projectResponse = await modeler.createProject('__test__')
const folderResponse = await modeler.createFolder({
projectId: projectResponse.id,
name: 'Test Folder',
})
expect(folderResponse.name).toBe('Test Folder')
})

it('should throw an error if the project does not exist', async () => {
await expect(
modeler.createFolder({
projectId: 'non-existent-project-id',
name: 'Test Folder',
})
).rejects.toThrow()
})
})

test('Can create project', async () => {
const modeler = new ModelerApiClient()
describe('createFile', () => {
it('should create a new file in an existing folder', async () => {
const projectResponse = await modeler.createProject('__test__')
const folderResponse = await modeler.createFolder({
projectId: projectResponse.id,
name: 'Test Folder',
})
const fileResponse = await modeler.createFile({
folderId: folderResponse.id,
projectId: projectResponse.id,
name: 'Test File',
content: fs.readFileSync(
'./src/__tests__/testdata/generic-test.bpmn',
'utf-8'
),
fileType: 'bpmn',
})
expect(fileResponse.name).toBe('Test File')
})

let res
res = await modeler.searchProjects({ filter: { name: '__test__' } })
if (res.items.length === 0) {
res = await modeler.createProject('__test__')
}
expect(res).toBeTruthy()
it('should throw an error if the folder does not exist', async () => {
await expect(
modeler.createFile({
folderId: 'non-existent-folder-id',
name: 'Test File',
content: fs.readFileSync(
'./src/__tests__/testdata/generic-test.bpmn',
'utf-8'
),
fileType: 'bpmn',
})
).rejects.toThrow()
})
})
})
36 changes: 21 additions & 15 deletions src/modeler/lib/ModelerAPIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ export class ModelerApiClient {
async deleteFile(fileId: string): Promise<null> {
const headers = await this.getHeaders()
const rest = await this.rest
return rest(`files/${fileId}`, {
headers,
}).then(this.decodeResponseOrThrow) as Promise<null>
return rest
.delete(`files/${fileId}`, {
headers,
})
.then(this.decodeResponseOrThrow) as Promise<null>
}

/**
Expand All @@ -241,12 +243,14 @@ export class ModelerApiClient {
): Promise<Dto.FileMetadataDto> {
const headers = await this.getHeaders()
const rest = await this.rest
return rest(`files/${fileId}`, {
headers,
body: JSON.stringify(update),
}).then((res) =>
JSON.parse(this.decodeResponseOrThrow(res))
) as Promise<Dto.FileMetadataDto>
return rest
.patch(`files/${fileId}`, {
headers,
body: JSON.stringify(update),
})
.then((res) =>
JSON.parse(this.decodeResponseOrThrow(res))
) as Promise<Dto.FileMetadataDto>
}

/**
Expand Down Expand Up @@ -280,12 +284,14 @@ export class ModelerApiClient {
): Promise<Dto.PubSearchResultDtoFileMetadataDto> {
const headers = await this.getHeaders()
const rest = await this.rest
return rest(`files/search`, {
headers,
body: JSON.stringify(req),
}).then((res) =>
JSON.parse(this.decodeResponseOrThrow(res))
) as Promise<Dto.PubSearchResultDtoFileMetadataDto>
return rest
.post(`files/search`, {
headers,
body: JSON.stringify(req),
})
.then((res) =>
JSON.parse(this.decodeResponseOrThrow(res))
) as Promise<Dto.PubSearchResultDtoFileMetadataDto>
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/modeler/lib/ModelerDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ export interface CreateFileDto {
/** maxLength: 255, minLength: 1 */
name: string
/** maxLength: 255, minLength: 1 */
folderId: string
folderId?: string
/** maxLength: 255, minLength: 1 */
projectId: string
projectId?: string
content: string
fileType: string
pattern: 'bpmn' | 'dmn' | 'form' | 'connector_template'
fileType: 'bpmn' | 'dmn' | 'form' | 'connector_template'
}

export interface PathElementDto {
Expand Down Expand Up @@ -112,7 +111,7 @@ export interface CreateFolderDto {
/** maxLength: 255 minLength: 1 */
projectId: string
/** maxLength: 255 minLength: 1 */
parentId: string
parentId?: string
}

export interface FolderMetadataDto {
Expand Down
Loading