From fb4bce41bc2bdc444ff0c88d361db44f15830c46 Mon Sep 17 00:00:00 2001 From: matthieulapatate Date: Mon, 4 Apr 2022 10:24:49 +0200 Subject: [PATCH 1/7] Add secondary dowloading for webapp --- .../app/springboot/domain/ProjectService.ts | 1 + .../springboot/secondary/ProjectRepository.ts | 8 ++++++ .../domain/ProjectService.fixture.ts | 2 ++ .../secondary/ProjectRepository.spec.ts | 27 +++++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/src/main/webapp/app/springboot/domain/ProjectService.ts b/src/main/webapp/app/springboot/domain/ProjectService.ts index 2553cf80097..f661386af91 100644 --- a/src/main/webapp/app/springboot/domain/ProjectService.ts +++ b/src/main/webapp/app/springboot/domain/ProjectService.ts @@ -5,4 +5,5 @@ export interface ProjectService { addMaven(project: Project): Promise; addFrontendMavenPlugin(project: Project): Promise; addJavaBase(project: Project): Promise; + download(project: Project): Promise; } diff --git a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts index 372844b02a9..4e1719652e6 100644 --- a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts +++ b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts @@ -25,4 +25,12 @@ export default class ProjectRepository implements ProjectService { const restProject: RestProject = toRestProject(project); await this.axiosHttp.post('api/servers/java/base', restProject); } + + async download(project: Project): Promise { + const restProject: RestProject = toRestProject(project); + return this.axiosHttp + .post('api/projects/download', restProject) + .then(response => new Blob([response.data])) + .catch(err => new Blob()); + } } diff --git a/src/test/javascript/spec/springboot/domain/ProjectService.fixture.ts b/src/test/javascript/spec/springboot/domain/ProjectService.fixture.ts index 569dcf01d32..53046b838f0 100644 --- a/src/test/javascript/spec/springboot/domain/ProjectService.fixture.ts +++ b/src/test/javascript/spec/springboot/domain/ProjectService.fixture.ts @@ -6,6 +6,7 @@ export interface ProjectServiceFixture extends ProjectService { addMaven: SinonStub; addFrontendMavenPlugin: SinonStub; addJavaBase: SinonStub; + download: SinonStub; } export const stubProjectService = (): ProjectServiceFixture => ({ @@ -13,4 +14,5 @@ export const stubProjectService = (): ProjectServiceFixture => ({ addMaven: sinon.stub(), addFrontendMavenPlugin: sinon.stub(), addJavaBase: sinon.stub(), + download: sinon.stub(), }); diff --git a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts index b2276491a33..2af35196fae 100644 --- a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts +++ b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts @@ -60,4 +60,31 @@ describe('ProjectRepository', () => { expect(uri).toBe('api/developer-tools/frontend-maven-plugin'); expect(payload).toEqual(expectedRestProject); }); + + it('should download the project', () => { + const axiosHttpStub = stubAxiosHttp(); + axiosHttpStub.post.resolves(); + const projectRepository = new ProjectRepository(axiosHttpStub); + const project: Project = createProject({ folder: 'folder/path' }); + + projectRepository.download(project); + + const expectedRestProject: RestProject = toRestProject(project); + const [uri, payload] = axiosHttpStub.post.getCall(0).args; + expect(uri).toBe('api/projects/download'); + expect(payload).toEqual(expectedRestProject); + }); + + it('should not download the project', () => { + const axiosHttpStub = stubAxiosHttp(); + axiosHttpStub.post.rejects({}); + const projectRepository = new ProjectRepository(axiosHttpStub); + const project: Project = createProject({ folder: 'folder/path' }); + + try { + projectRepository.download(project); + } catch (e) { + expect(e).toMatch('error'); + } + }); }); From ca21311d579ddd523fffb19211d5a59569f2668b Mon Sep 17 00:00:00 2001 From: matthieulapatate Date: Wed, 6 Apr 2022 11:46:44 +0200 Subject: [PATCH 2/7] Return plain ArrayBuffer response in secondary download method --- src/main/webapp/app/springboot/domain/ProjectService.ts | 2 +- .../webapp/app/springboot/secondary/ProjectRepository.ts | 7 +++---- .../javascript/spec/springboot/domain/Project.fixture.ts | 9 +++++++++ .../spec/springboot/secondary/ProjectRepository.spec.ts | 6 ++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/webapp/app/springboot/domain/ProjectService.ts b/src/main/webapp/app/springboot/domain/ProjectService.ts index f661386af91..fd4986ecd9e 100644 --- a/src/main/webapp/app/springboot/domain/ProjectService.ts +++ b/src/main/webapp/app/springboot/domain/ProjectService.ts @@ -5,5 +5,5 @@ export interface ProjectService { addMaven(project: Project): Promise; addFrontendMavenPlugin(project: Project): Promise; addJavaBase(project: Project): Promise; - download(project: Project): Promise; + download(project: Project): Promise; } diff --git a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts index 4e1719652e6..e2bc4f7ceed 100644 --- a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts +++ b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts @@ -26,11 +26,10 @@ export default class ProjectRepository implements ProjectService { await this.axiosHttp.post('api/servers/java/base', restProject); } - async download(project: Project): Promise { + async download(project: Project): Promise { const restProject: RestProject = toRestProject(project); return this.axiosHttp - .post('api/projects/download', restProject) - .then(response => new Blob([response.data])) - .catch(err => new Blob()); + .post('api/projects/download', restProject) + .then(response => new Uint8Array(response.data)); } } diff --git a/src/test/javascript/spec/springboot/domain/Project.fixture.ts b/src/test/javascript/spec/springboot/domain/Project.fixture.ts index 3239f9ac643..f5d3da8401e 100644 --- a/src/test/javascript/spec/springboot/domain/Project.fixture.ts +++ b/src/test/javascript/spec/springboot/domain/Project.fixture.ts @@ -1,4 +1,6 @@ import { Project } from '@/springboot/domain/Project'; +import ProjectRepository from '../../../../../main/webapp/app/springboot/secondary/ProjectRepository'; +import { stubAxiosHttp } from '../../http/AxiosHttpStub'; export const createProject = (project?: Partial): Project => ({ folder: 'folder/path', @@ -8,3 +10,10 @@ export const createProject = (project?: Partial): Project => ({ serverPort: 8080, ...project, }); + +export const createStubedProjectRepository = (): [ProjectRepository, any] => { + const axiosHttpStub = stubAxiosHttp(); + axiosHttpStub.post.resolves(); + const projectRepository = new ProjectRepository(axiosHttpStub); + return [projectRepository, axiosHttpStub]; +}; diff --git a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts index 2af35196fae..ba5ae9500ff 100644 --- a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts +++ b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts @@ -2,7 +2,7 @@ import { Project } from '@/springboot/domain/Project'; import ProjectRepository from '@/springboot/secondary/ProjectRepository'; import { stubAxiosHttp } from '../../http/AxiosHttpStub'; import { RestProject, toRestProject } from '@/springboot/secondary/RestProject'; -import { createProject } from '../domain/Project.fixture'; +import { createProject, createStubedProjectRepository } from '../domain/Project.fixture'; describe('ProjectRepository', () => { it('should init project', () => { @@ -62,9 +62,7 @@ describe('ProjectRepository', () => { }); it('should download the project', () => { - const axiosHttpStub = stubAxiosHttp(); - axiosHttpStub.post.resolves(); - const projectRepository = new ProjectRepository(axiosHttpStub); + const [projectRepository, axiosHttpStub] = createStubedProjectRepository(); const project: Project = createProject({ folder: 'folder/path' }); projectRepository.download(project); From 1fca1633757db9b77051b8b93ee8a337c3d7b46d Mon Sep 17 00:00:00 2001 From: matthieulapatate Date: Wed, 6 Apr 2022 11:49:59 +0200 Subject: [PATCH 3/7] Catch error in secondary or test doesn't pass --- src/main/webapp/app/springboot/secondary/ProjectRepository.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts index e2bc4f7ceed..29329622edc 100644 --- a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts +++ b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts @@ -30,6 +30,7 @@ export default class ProjectRepository implements ProjectService { const restProject: RestProject = toRestProject(project); return this.axiosHttp .post('api/projects/download', restProject) - .then(response => new Uint8Array(response.data)); + .then(response => new Uint8Array(response.data)) + .catch(error => new Uint8Array()); } } From 56008ec2a97fe913277ed4be43d8a5d127fcffae Mon Sep 17 00:00:00 2001 From: matthieulapatate Date: Wed, 6 Apr 2022 15:19:05 +0200 Subject: [PATCH 4/7] Asynchronous test on secondary download function --- .../springboot/secondary/ProjectRepository.ts | 3 +-- .../spec/springboot/domain/Project.fixture.ts | 5 +++-- .../secondary/ProjectRepository.spec.ts | 19 +++---------------- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts index 29329622edc..e2bc4f7ceed 100644 --- a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts +++ b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts @@ -30,7 +30,6 @@ export default class ProjectRepository implements ProjectService { const restProject: RestProject = toRestProject(project); return this.axiosHttp .post('api/projects/download', restProject) - .then(response => new Uint8Array(response.data)) - .catch(error => new Uint8Array()); + .then(response => new Uint8Array(response.data)); } } diff --git a/src/test/javascript/spec/springboot/domain/Project.fixture.ts b/src/test/javascript/spec/springboot/domain/Project.fixture.ts index f5d3da8401e..bf8a23c6d1a 100644 --- a/src/test/javascript/spec/springboot/domain/Project.fixture.ts +++ b/src/test/javascript/spec/springboot/domain/Project.fixture.ts @@ -1,6 +1,7 @@ import { Project } from '@/springboot/domain/Project'; import ProjectRepository from '../../../../../main/webapp/app/springboot/secondary/ProjectRepository'; import { stubAxiosHttp } from '../../http/AxiosHttpStub'; +import { AxiosHttpResponse } from '../../../../../main/webapp/app/http/AxiosHttp'; export const createProject = (project?: Partial): Project => ({ folder: 'folder/path', @@ -11,9 +12,9 @@ export const createProject = (project?: Partial): Project => ({ ...project, }); -export const createStubedProjectRepository = (): [ProjectRepository, any] => { +export const createStubedProjectRepository = (resolve?: any): [ProjectRepository, any] => { const axiosHttpStub = stubAxiosHttp(); - axiosHttpStub.post.resolves(); + axiosHttpStub.post.resolves(resolve); const projectRepository = new ProjectRepository(axiosHttpStub); return [projectRepository, axiosHttpStub]; }; diff --git a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts index ba5ae9500ff..0cf0a89c5bb 100644 --- a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts +++ b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts @@ -61,28 +61,15 @@ describe('ProjectRepository', () => { expect(payload).toEqual(expectedRestProject); }); - it('should download the project', () => { - const [projectRepository, axiosHttpStub] = createStubedProjectRepository(); + it('should download the project', async () => { + const [projectRepository, axiosHttpStub] = createStubedProjectRepository({ data: [1, 2, 3] }); const project: Project = createProject({ folder: 'folder/path' }); - projectRepository.download(project); + expect(await projectRepository.download(project)).toEqual(new Uint8Array([1, 2, 3])); const expectedRestProject: RestProject = toRestProject(project); const [uri, payload] = axiosHttpStub.post.getCall(0).args; expect(uri).toBe('api/projects/download'); expect(payload).toEqual(expectedRestProject); }); - - it('should not download the project', () => { - const axiosHttpStub = stubAxiosHttp(); - axiosHttpStub.post.rejects({}); - const projectRepository = new ProjectRepository(axiosHttpStub); - const project: Project = createProject({ folder: 'folder/path' }); - - try { - projectRepository.download(project); - } catch (e) { - expect(e).toMatch('error'); - } - }); }); From 7c6c77ee1dc2d03a3faa28fb4bf082b033e8489e Mon Sep 17 00:00:00 2001 From: matthieulapatate Date: Fri, 8 Apr 2022 17:27:25 +0200 Subject: [PATCH 5/7] Change secondary return type --- src/main/webapp/app/http/AxiosHttp.ts | 4 ++-- .../webapp/app/springboot/domain/ProjectService.ts | 2 +- .../app/springboot/secondary/ProjectRepository.ts | 11 ++++++++--- .../springboot/secondary/ProjectRepository.spec.ts | 7 ++++--- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/webapp/app/http/AxiosHttp.ts b/src/main/webapp/app/http/AxiosHttp.ts index d02800c1bb5..2e4b569f5b7 100644 --- a/src/main/webapp/app/http/AxiosHttp.ts +++ b/src/main/webapp/app/http/AxiosHttp.ts @@ -13,8 +13,8 @@ export class AxiosHttp { return this.axiosInstance.put(uri, data); } - async post(uri: string, data?: Payload): Promise> { - return this.axiosInstance.post(uri, data); + async post(uri: string, data?: Payload, config?: AxiosRequestConfig): Promise> { + return this.axiosInstance.post(uri, data, config); } async delete(uri: string): Promise> { diff --git a/src/main/webapp/app/springboot/domain/ProjectService.ts b/src/main/webapp/app/springboot/domain/ProjectService.ts index fd4986ecd9e..3fa5b708a44 100644 --- a/src/main/webapp/app/springboot/domain/ProjectService.ts +++ b/src/main/webapp/app/springboot/domain/ProjectService.ts @@ -5,5 +5,5 @@ export interface ProjectService { addMaven(project: Project): Promise; addFrontendMavenPlugin(project: Project): Promise; addJavaBase(project: Project): Promise; - download(project: Project): Promise; + download(project: Project): Promise; } diff --git a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts index e2bc4f7ceed..2a5d8b20bcc 100644 --- a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts +++ b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts @@ -26,10 +26,15 @@ export default class ProjectRepository implements ProjectService { await this.axiosHttp.post('api/servers/java/base', restProject); } - async download(project: Project): Promise { + async download(project: Project): Promise { const restProject: RestProject = toRestProject(project); return this.axiosHttp - .post('api/projects/download', restProject) - .then(response => new Uint8Array(response.data)); + .post('api/projects/download', restProject, { + responseType: 'blob', + headers: { + 'Content-Type': 'application/json;charset=UTF-8', + }, + }) + .then(response => response.data); } } diff --git a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts index 0cf0a89c5bb..739cf6bff24 100644 --- a/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts +++ b/src/test/javascript/spec/springboot/secondary/ProjectRepository.spec.ts @@ -64,11 +64,12 @@ describe('ProjectRepository', () => { it('should download the project', async () => { const [projectRepository, axiosHttpStub] = createStubedProjectRepository({ data: [1, 2, 3] }); const project: Project = createProject({ folder: 'folder/path' }); - - expect(await projectRepository.download(project)).toEqual(new Uint8Array([1, 2, 3])); - const expectedRestProject: RestProject = toRestProject(project); + + const datas = await projectRepository.download(project); const [uri, payload] = axiosHttpStub.post.getCall(0).args; + + expect(datas).toEqual([1, 2, 3]); expect(uri).toBe('api/projects/download'); expect(payload).toEqual(expectedRestProject); }); From c113792a344c232cb68e4c3f2828d8948f8c4c61 Mon Sep 17 00:00:00 2001 From: matthieulapatate Date: Mon, 11 Apr 2022 11:04:11 +0200 Subject: [PATCH 6/7] Remove unused import and switch blobpart array into blobpart --- src/main/webapp/app/springboot/domain/ProjectService.ts | 2 +- src/main/webapp/app/springboot/secondary/ProjectRepository.ts | 4 ++-- src/test/javascript/spec/springboot/domain/Project.fixture.ts | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/webapp/app/springboot/domain/ProjectService.ts b/src/main/webapp/app/springboot/domain/ProjectService.ts index 3fa5b708a44..05dfd29a63a 100644 --- a/src/main/webapp/app/springboot/domain/ProjectService.ts +++ b/src/main/webapp/app/springboot/domain/ProjectService.ts @@ -5,5 +5,5 @@ export interface ProjectService { addMaven(project: Project): Promise; addFrontendMavenPlugin(project: Project): Promise; addJavaBase(project: Project): Promise; - download(project: Project): Promise; + download(project: Project): Promise; } diff --git a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts index 2a5d8b20bcc..ed745dc591e 100644 --- a/src/main/webapp/app/springboot/secondary/ProjectRepository.ts +++ b/src/main/webapp/app/springboot/secondary/ProjectRepository.ts @@ -26,10 +26,10 @@ export default class ProjectRepository implements ProjectService { await this.axiosHttp.post('api/servers/java/base', restProject); } - async download(project: Project): Promise { + async download(project: Project): Promise { const restProject: RestProject = toRestProject(project); return this.axiosHttp - .post('api/projects/download', restProject, { + .post('api/projects/download', restProject, { responseType: 'blob', headers: { 'Content-Type': 'application/json;charset=UTF-8', diff --git a/src/test/javascript/spec/springboot/domain/Project.fixture.ts b/src/test/javascript/spec/springboot/domain/Project.fixture.ts index bf8a23c6d1a..769a82f8c10 100644 --- a/src/test/javascript/spec/springboot/domain/Project.fixture.ts +++ b/src/test/javascript/spec/springboot/domain/Project.fixture.ts @@ -1,7 +1,6 @@ import { Project } from '@/springboot/domain/Project'; import ProjectRepository from '../../../../../main/webapp/app/springboot/secondary/ProjectRepository'; import { stubAxiosHttp } from '../../http/AxiosHttpStub'; -import { AxiosHttpResponse } from '../../../../../main/webapp/app/http/AxiosHttp'; export const createProject = (project?: Partial): Project => ({ folder: 'folder/path', From f41173a25622540f853cefcc4ceabb8740a8b4df Mon Sep 17 00:00:00 2001 From: matthieulapatate Date: Mon, 11 Apr 2022 11:14:09 +0200 Subject: [PATCH 7/7] Add @ as root project for import (impossible for test directory) --- src/test/javascript/spec/springboot/domain/Project.fixture.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/javascript/spec/springboot/domain/Project.fixture.ts b/src/test/javascript/spec/springboot/domain/Project.fixture.ts index 769a82f8c10..ef1bd3c01f6 100644 --- a/src/test/javascript/spec/springboot/domain/Project.fixture.ts +++ b/src/test/javascript/spec/springboot/domain/Project.fixture.ts @@ -1,5 +1,5 @@ import { Project } from '@/springboot/domain/Project'; -import ProjectRepository from '../../../../../main/webapp/app/springboot/secondary/ProjectRepository'; +import ProjectRepository from '@/springboot/secondary/ProjectRepository'; import { stubAxiosHttp } from '../../http/AxiosHttpStub'; export const createProject = (project?: Partial): Project => ({