From 3c96abb64fba6af18695922028f6cf674b920275 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Fri, 17 Nov 2023 17:23:35 +0100 Subject: [PATCH 01/10] CST-12498 Provided working mock, finishing the tests files --- .../ldnServicesRD$-mock.ts | 137 ++++++++++++++++-- .../ldn-services-directory.component.ts | 21 +++ 2 files changed, 148 insertions(+), 10 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts index 3bd1bf249f8..3f559c6c48c 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts @@ -2,9 +2,13 @@ import {LdnService} from '../ldn-services-model/ldn-services.model'; import {LDN_SERVICE} from '../ldn-services-model/ldn-service.resource-type'; import {RemoteData} from '../../../core/data/remote-data'; import {PaginatedList} from '../../../core/data/paginated-list.model'; -import {Observable, of} from 'rxjs'; -// Create a mock data object for a single LDN notify service +import {Observable, of} from "rxjs"; +import {createSuccessfulRemoteDataObject$} from "../../../shared/remote-data.utils"; + export const mockLdnService: LdnService = { + uuid: "1", + enabled: false, + score: 0, id: 1, name: 'Service Name', description: 'Service Description', @@ -14,30 +18,147 @@ export const mockLdnService: LdnService = { { pattern: 'patternA', constraint: 'itemFilterA', - automatic: false, + automatic: 'false', }, { pattern: 'patternB', constraint: 'itemFilterB', - automatic: true, + automatic: 'true', }, ], notifyServiceOutboundPatterns: [ { pattern: 'patternC', constraint: 'itemFilterC', + automatic: 'true', }, ], type: LDN_SERVICE, _links: { self: { - href: 'http://localhost/api/ldn/ldnservices/1', + href: 'http://localhost/api/ldn/ldnservices/1' }, }, + get self(): string { + return ""; + }, }; +//export const mockLdnServiceRD$: Observable>> = of((mockLdnService as unknown) as RemoteData>); +export const mockLdnServiceRD$: Observable> = createSuccessfulRemoteDataObject$(mockLdnService); + -const mockLdnServices = { +export const mockLdnServices: LdnService[] = [{ + uuid: "1", + enabled: false, + score: 0, + id: 1, + name: 'Service Name', + description: 'Service Description', + url: 'Service URL', + ldnUrl: 'Service LDN URL', + notifyServiceInboundPatterns: [ + { + pattern: 'patternA', + constraint: 'itemFilterA', + automatic: 'false', + }, + { + pattern: 'patternB', + constraint: 'itemFilterB', + automatic: 'true', + }, + ], + notifyServiceOutboundPatterns: [ + { + pattern: 'patternC', + constraint: 'itemFilterC', + automatic: 'true', + }, + ], + type: LDN_SERVICE, + _links: { + self: { + href: 'http://localhost/api/ldn/ldnservices/1' + }, + }, + get self(): string { + return ""; + }, +},{ + uuid: "2", + enabled: false, + score: 0, + id: 2, + name: 'Service Name', + description: 'Service Description', + url: 'Service URL', + ldnUrl: 'Service LDN URL', + notifyServiceInboundPatterns: [ + { + pattern: 'patternA', + constraint: 'itemFilterA', + automatic: 'false', + }, + { + pattern: 'patternB', + constraint: 'itemFilterB', + automatic: 'true', + }, + ], + notifyServiceOutboundPatterns: [ + { + pattern: 'patternC', + constraint: 'itemFilterC', + automatic: 'true', + }, + ], + type: LDN_SERVICE, + _links: { + self: { + href: 'http://localhost/api/ldn/ldnservices/1' + }, + }, + get self(): string { + return ""; + }, +} +] +export const mockLdnServicesRD$: Observable>> = of((mockLdnServices as unknown) as RemoteData>); + + +/*export const mockLdnServiceRD$: RemoteData> = { + errorMessage: null, + lastUpdated: 1700176600821, + msToLive: 900000, + payload: { + page: [mockLdnService], + pageInfo: { + elementsPerPage: 20, + totalPages: 1, + totalElements: 1, + currentPage: 1, + }, + type: {value: "paginated-list"}, + _links: { + self: { + href: "http://localhost:8080/server/api/ldn/ldnservices?size=20&sort=dc.title,ASC" + }, + page: [ + { + "href": "http://localhost/api/ldn/ldnservices/1" + } + ] + }, + }, + statusCode: 200, + state: 'Success', + timeCompleted: 1700176600821, +}*/ + + + +const mockLdnServices2 = { payload: { elementsPerPage: 20, totalPages: 1, @@ -63,7 +184,3 @@ const mockLdnServices = { hasSucceeded: true, msToLive: 0, }; - - -// Create a mock ldnServicesRD$ observable -export const mockLdnServicesRD$: Observable>> = of((mockLdnServices as unknown) as RemoteData>); diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts index ac9354a3e48..99e420568cd 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts @@ -22,6 +22,7 @@ import {Operation} from 'fast-json-patch'; import {getFirstCompletedRemoteData} from '../../../core/shared/operators'; import {NotificationsService} from '../../../shared/notifications/notifications.service'; import {TranslateService} from '@ngx-translate/core'; +import {mockLdnServiceRD$, mockLdnServicesRD$} from "../ldn-service-serviceMock/ldnServicesRD$-mock"; @Component({ @@ -36,6 +37,9 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { servicesData: any[] = []; @ViewChild('deleteModal', {static: true}) deleteModal: TemplateRef; ldnServicesRD$: Observable>>; + //TODO: remove mocks an put in test after finishing + mockLdnServiceRD$: Observable>>; + mockLdnServicesRD$: Observable>>; config: FindListOptions = Object.assign(new FindListOptions(), { elementsPerPage: 20 }); @@ -47,6 +51,7 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { private modalRef: any; + constructor( protected ldnServicesService: LdnServicesService, protected paginationService: PaginationService, @@ -68,6 +73,22 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { )) ); + this.mockLdnServiceRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( + switchMap((config) => this.ldnServicesService.findAll(config, false, false).pipe( + getFirstCompletedRemoteData() + )) + + ); + this.mockLdnServicesRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( + switchMap((config) => this.ldnServicesService.findAll(config, false, false).pipe( + getFirstCompletedRemoteData() + )) + + ); + this.ldnServicesRD$.subscribe((rd: RemoteData>) => {console.log('realremotedata:',rd);}) + this.mockLdnServiceRD$.subscribe((rd: RemoteData>) => {console.log('mockremotedata:',rd);}) + this.mockLdnServicesRD$.subscribe((rd: RemoteData>) => {console.log('mockremotedata[ldnservice]:',rd);}) + } ngOnDestroy(): void { From 92b791ed153b0981ee0ca1c2fa212f0663a792c4 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Fri, 17 Nov 2023 20:34:28 +0100 Subject: [PATCH 02/10] CST-12498 Added tests, still needs cleanup and check on one behaviour --- .../ldnServicesRD$-mock.ts | 3 +- .../ldn-services-directory.component.spec.ts | 119 ++++++++++++++---- .../ldn-services-directory.component.ts | 34 +++-- 3 files changed, 112 insertions(+), 44 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts index 3f559c6c48c..5d409964c26 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts @@ -45,7 +45,8 @@ export const mockLdnService: LdnService = { }; //export const mockLdnServiceRD$: Observable>> = of((mockLdnService as unknown) as RemoteData>); -export const mockLdnServiceRD$: Observable> = createSuccessfulRemoteDataObject$(mockLdnService); +//export const mockLdnServiceRD$ = createSuccessfulRemoteDataObject$(createPaginatedList(mockLdnService[0])as PaginatedList); +export const mockLdnServiceRD$ = createSuccessfulRemoteDataObject$(mockLdnService); export const mockLdnServices: LdnService[] = [{ diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts index 87a1da27a1c..6912379b786 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts @@ -1,18 +1,26 @@ -import {ComponentFixture, TestBed} from '@angular/core/testing'; -import {LdnServicesOverviewComponent} from './ldn-services-directory.component'; -import {ChangeDetectorRef, EventEmitter} from '@angular/core'; -import {NotificationsService} from '../../../shared/notifications/notifications.service'; -import {NotificationsServiceStub} from '../../../shared/testing/notifications-service.stub'; -import {TranslateModule, TranslateService} from '@ngx-translate/core'; -import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; -import {LdnServicesService} from '../ldn-services-data/ldn-services-data.service'; -import {PaginationService} from '../../../core/pagination/pagination.service'; -import {PaginationServiceStub} from '../../../shared/testing/pagination-service.stub'; -import {of} from 'rxjs'; +import { ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'; +import { ChangeDetectorRef, EventEmitter, TemplateRef, ViewChild } from '@angular/core'; +import { NotificationsService } from '../../../shared/notifications/notifications.service'; +import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; +import { TranslateModule, TranslateService } from '@ngx-translate/core'; +import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; +import { LdnServicesService } from '../ldn-services-data/ldn-services-data.service'; +import { PaginationService } from '../../../core/pagination/pagination.service'; +import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub'; +import { of } from 'rxjs'; +import { LdnService } from "../ldn-services-model/ldn-services.model"; +import { PaginatedList } from "../../../core/data/paginated-list.model"; +import { RemoteData } from "../../../core/data/remote-data"; +import { LdnServicesOverviewComponent } from './ldn-services-directory.component'; describe('LdnServicesOverviewComponent', () => { let component: LdnServicesOverviewComponent; let fixture: ComponentFixture; + let ldnServicesService: LdnServicesService; + let paginationService: PaginationService; + let modalService: NgbModal; + let notificationsService: NotificationsService; + let translateService: TranslateService; const translateServiceStub = { get: () => of('translated-text'), @@ -26,29 +34,92 @@ describe('LdnServicesOverviewComponent', () => { imports: [TranslateModule.forRoot()], declarations: [LdnServicesOverviewComponent], providers: [ - {provide: LdnServicesService, useValue: {}}, - {provide: PaginationService, useValue: new PaginationServiceStub()}, - { - provide: NgbModal, useValue: { - open: () => {/*comment*/ - } - } - }, - {provide: ChangeDetectorRef, useValue: {}}, - {provide: NotificationsService, useValue: NotificationsServiceStub}, - {provide: TranslateService, useValue: translateServiceStub}, + { provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch']) }, + { provide: PaginationService, useValue: new PaginationServiceStub() }, + { provide: NgbModal, useValue: { open: () => { /*comment*/ } } }, + { provide: ChangeDetectorRef, useValue: {} }, + { provide: NotificationsService, useValue: NotificationsServiceStub }, + { provide: TranslateService, useValue: translateServiceStub }, ] - }) - .compileComponents(); + }).compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(LdnServicesOverviewComponent); component = fixture.componentInstance; + ldnServicesService = TestBed.inject(LdnServicesService); + paginationService = TestBed.inject(PaginationService); + modalService = TestBed.inject(NgbModal); + notificationsService = TestBed.inject(NotificationsService); + translateService = TestBed.inject(TranslateService); + component.modalRef = jasmine.createSpyObj({ close: null }); + component.isProcessingSub = jasmine.createSpyObj({ unsubscribe: null }); + component.ldnServicesRD$ = of({} as RemoteData>); // You can adjust the mock data as needed fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); + + describe('ngOnInit', () => { + it('should call setLdnServices', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + component.ngOnInit(); + tick(); + expect(component.setLdnServices).toHaveBeenCalled(); + })); + + it('should set ldnServicesRD$ with mock data', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + const mockData = { /* your mock data here */ }; + component.ldnServicesRD$ = of(mockData as RemoteData>); + component.ngOnInit(); + tick(); + expect(component.setLdnServices).toHaveBeenCalled(); + // Add more expectations based on your mock data and component behavior + })); + }); + + describe('ngOnDestroy', () => { + it('should call paginationService.clearPagination and unsubscribe', () => { + spyOn(paginationService, 'clearPagination'); + spyOn(component.isProcessingSub, 'unsubscribe'); + component.ngOnDestroy(); + expect(paginationService.clearPagination).toHaveBeenCalledWith(component.pageConfig.id); + expect(component.isProcessingSub.unsubscribe).toHaveBeenCalled(); + }); + }); + + describe('openDeleteModal', () => { + it('should open delete modal', () => { + spyOn(modalService, 'open'); + component.openDeleteModal(component.deleteModal); + expect(modalService.open).toHaveBeenCalledWith(component.deleteModal); + }); + }); + + describe('closeModal', () => { + it('should close modal and detect changes', () => { + spyOn(component.modalRef, 'close'); + spyOn(component.cdRef, 'detectChanges'); + component.closeModal(); + expect(component.modalRef.close).toHaveBeenCalled(); + expect(component.cdRef.detectChanges).toHaveBeenCalled(); + }); + }); + + describe('deleteSelected', () => { + it('should delete selected service and update data', fakeAsync(() => { + const serviceId = '123'; + const mockRemoteData = { /* insert mock data with service id 123 */ }; + spyOn(component, 'setLdnServices').and.callThrough(); + const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData>)); + component.selectedServiceId = serviceId; + component.deleteSelected(serviceId, ldnServicesService); + tick(); + expect(deleteSpy).toHaveBeenCalledWith(serviceId); + expect(mockRemoteData) + })); + }); }); diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts index 99e420568cd..476d8ef1b59 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts @@ -22,7 +22,7 @@ import {Operation} from 'fast-json-patch'; import {getFirstCompletedRemoteData} from '../../../core/shared/operators'; import {NotificationsService} from '../../../shared/notifications/notifications.service'; import {TranslateService} from '@ngx-translate/core'; -import {mockLdnServiceRD$, mockLdnServicesRD$} from "../ldn-service-serviceMock/ldnServicesRD$-mock"; +import { mockLdnServiceRD$ } from "../ldn-service-serviceMock/ldnServicesRD$-mock"; @Component({ @@ -38,8 +38,8 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { @ViewChild('deleteModal', {static: true}) deleteModal: TemplateRef; ldnServicesRD$: Observable>>; //TODO: remove mocks an put in test after finishing - mockLdnServiceRD$: Observable>>; - mockLdnServicesRD$: Observable>>; + //mockLdnServiceRD$: Observable>; + // mockLdnServicesRD$: Observable>>; config: FindListOptions = Object.assign(new FindListOptions(), { elementsPerPage: 20 }); @@ -48,17 +48,17 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { pageSize: 20 }); isProcessingSub: Subscription; - private modalRef: any; + modalRef: any; constructor( - protected ldnServicesService: LdnServicesService, - protected paginationService: PaginationService, - protected modalService: NgbModal, - private cdRef: ChangeDetectorRef, - private notificationService: NotificationsService, - private translateService: TranslateService, + protected ldnServicesService: LdnServicesService, + protected paginationService: PaginationService, + protected modalService: NgbModal, + public cdRef: ChangeDetectorRef, + private notificationService: NotificationsService, + private translateService: TranslateService, ) { } @@ -72,22 +72,18 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { getFirstCompletedRemoteData() )) - ); - this.mockLdnServiceRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( - switchMap((config) => this.ldnServicesService.findAll(config, false, false).pipe( - getFirstCompletedRemoteData() - )) + );/* + this.mockLdnServiceRD$ = mockLdnServiceRD$ - ); this.mockLdnServicesRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( switchMap((config) => this.ldnServicesService.findAll(config, false, false).pipe( getFirstCompletedRemoteData() )) - ); + );this.paginationService.getFindListOptions(this.pageConfig.id, this.config) this.ldnServicesRD$.subscribe((rd: RemoteData>) => {console.log('realremotedata:',rd);}) - this.mockLdnServiceRD$.subscribe((rd: RemoteData>) => {console.log('mockremotedata:',rd);}) - this.mockLdnServicesRD$.subscribe((rd: RemoteData>) => {console.log('mockremotedata[ldnservice]:',rd);}) + this.mockLdnServiceRD$.subscribe((rd: RemoteData) => {console.log('mockremotedata:',rd);}) + this.mockLdnServicesRD$.subscribe((rd: RemoteData>) => {console.log('mockremotedata[ldnservice]:',rd);})*/ } From bdd2a8bcab6b75eba52e43dccea7c80ff28f371b Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Fri, 17 Nov 2023 21:19:38 +0100 Subject: [PATCH 03/10] CST-12498 Tests added, need to check TODOs in ldn-services-directory.component.spec.ts --- .../ldn-services-directory.component.spec.ts | 241 ++++++++++-------- 1 file changed, 132 insertions(+), 109 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts index 6912379b786..8213e5bc535 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts @@ -1,125 +1,148 @@ -import { ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'; -import { ChangeDetectorRef, EventEmitter, TemplateRef, ViewChild } from '@angular/core'; -import { NotificationsService } from '../../../shared/notifications/notifications.service'; -import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; -import { TranslateModule, TranslateService } from '@ngx-translate/core'; -import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; -import { LdnServicesService } from '../ldn-services-data/ldn-services-data.service'; -import { PaginationService } from '../../../core/pagination/pagination.service'; -import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub'; -import { of } from 'rxjs'; -import { LdnService } from "../ldn-services-model/ldn-services.model"; -import { PaginatedList } from "../../../core/data/paginated-list.model"; -import { RemoteData } from "../../../core/data/remote-data"; -import { LdnServicesOverviewComponent } from './ldn-services-directory.component'; +import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; +import {ChangeDetectorRef, EventEmitter} from '@angular/core'; +import {NotificationsService} from '../../../shared/notifications/notifications.service'; +import {NotificationsServiceStub} from '../../../shared/testing/notifications-service.stub'; +import {TranslateModule, TranslateService} from '@ngx-translate/core'; +import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; +import {LdnServicesService} from '../ldn-services-data/ldn-services-data.service'; +import {PaginationService} from '../../../core/pagination/pagination.service'; +import {PaginationServiceStub} from '../../../shared/testing/pagination-service.stub'; +import {of} from 'rxjs'; +import {LdnService} from "../ldn-services-model/ldn-services.model"; +import {PaginatedList} from "../../../core/data/paginated-list.model"; +import {RemoteData} from "../../../core/data/remote-data"; +import {LdnServicesOverviewComponent} from './ldn-services-directory.component'; describe('LdnServicesOverviewComponent', () => { - let component: LdnServicesOverviewComponent; - let fixture: ComponentFixture; - let ldnServicesService: LdnServicesService; - let paginationService: PaginationService; - let modalService: NgbModal; - let notificationsService: NotificationsService; - let translateService: TranslateService; + let component: LdnServicesOverviewComponent; + let fixture: ComponentFixture; + let ldnServicesService: LdnServicesService; + let paginationService: PaginationService; + let modalService: NgbModal; + let notificationsService: NotificationsService; + let translateService: TranslateService; - const translateServiceStub = { - get: () => of('translated-text'), - onLangChange: new EventEmitter(), - onTranslationChange: new EventEmitter(), - onDefaultLangChange: new EventEmitter() - }; + const translateServiceStub = { + get: () => of('translated-text'), + onLangChange: new EventEmitter(), + onTranslationChange: new EventEmitter(), + onDefaultLangChange: new EventEmitter() + }; - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [TranslateModule.forRoot()], - declarations: [LdnServicesOverviewComponent], - providers: [ - { provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch']) }, - { provide: PaginationService, useValue: new PaginationServiceStub() }, - { provide: NgbModal, useValue: { open: () => { /*comment*/ } } }, - { provide: ChangeDetectorRef, useValue: {} }, - { provide: NotificationsService, useValue: NotificationsServiceStub }, - { provide: TranslateService, useValue: translateServiceStub }, - ] - }).compileComponents(); - }); + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [TranslateModule.forRoot()], + declarations: [LdnServicesOverviewComponent], + providers: [ + {provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch'])}, + {provide: PaginationService, useValue: new PaginationServiceStub()}, + { + provide: NgbModal, useValue: { + open: () => { /*comment*/ + } + } + }, + {provide: ChangeDetectorRef, useValue: {}}, + {provide: NotificationsService, useValue: NotificationsServiceStub}, + {provide: TranslateService, useValue: translateServiceStub}, + ] + }).compileComponents(); + }); - beforeEach(() => { - fixture = TestBed.createComponent(LdnServicesOverviewComponent); - component = fixture.componentInstance; - ldnServicesService = TestBed.inject(LdnServicesService); - paginationService = TestBed.inject(PaginationService); - modalService = TestBed.inject(NgbModal); - notificationsService = TestBed.inject(NotificationsService); - translateService = TestBed.inject(TranslateService); - component.modalRef = jasmine.createSpyObj({ close: null }); - component.isProcessingSub = jasmine.createSpyObj({ unsubscribe: null }); - component.ldnServicesRD$ = of({} as RemoteData>); // You can adjust the mock data as needed - fixture.detectChanges(); - }); + beforeEach(() => { + fixture = TestBed.createComponent(LdnServicesOverviewComponent); + component = fixture.componentInstance; + ldnServicesService = TestBed.inject(LdnServicesService); + paginationService = TestBed.inject(PaginationService); + modalService = TestBed.inject(NgbModal); + notificationsService = TestBed.inject(NotificationsService); + translateService = TestBed.inject(TranslateService); + component.modalRef = jasmine.createSpyObj({close: null}); + component.isProcessingSub = jasmine.createSpyObj({unsubscribe: null}); + component.ldnServicesRD$ = of({} as RemoteData>); + fixture.detectChanges(); + }); - it('should create', () => { - expect(component).toBeTruthy(); - }); + it('should create', () => { + expect(component).toBeTruthy(); + }); - describe('ngOnInit', () => { - it('should call setLdnServices', fakeAsync(() => { - spyOn(component, 'setLdnServices').and.callThrough(); - component.ngOnInit(); - tick(); - expect(component.setLdnServices).toHaveBeenCalled(); - })); + describe('ngOnInit', () => { + it('should call setLdnServices', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + component.ngOnInit(); + tick(); + expect(component.setLdnServices).toHaveBeenCalled(); + })); - it('should set ldnServicesRD$ with mock data', fakeAsync(() => { - spyOn(component, 'setLdnServices').and.callThrough(); - const mockData = { /* your mock data here */ }; - component.ldnServicesRD$ = of(mockData as RemoteData>); - component.ngOnInit(); - tick(); - expect(component.setLdnServices).toHaveBeenCalled(); - // Add more expectations based on your mock data and component behavior - })); - }); + it('should set ldnServicesRD$ with mock data', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + const mockData = { + payload: { + page: [ + {id: 1, name: 'Service 1', description: 'Description 1', enabled: true}, + {id: 2, name: 'Service 2', description: 'Description 2', enabled: false}, + {id: 3, name: 'Service 3', description: 'Description 3', enabled: true}, + ], + totalElements: 3 + } + } as RemoteData>; + component.ldnServicesRD$ = of(mockData as RemoteData>); + component.ngOnInit(); + tick(); + expect(component.setLdnServices).toHaveBeenCalled(); + fixture.detectChanges(); + console.log(fixture.nativeElement.innerHTML); + console.log('Mock Data:', mockData); + const tableRows = fixture.nativeElement.querySelectorAll('tbody tr'); + console.log('Table Rows:', tableRows); + //TODO: check why the tbody tr length table rows is 0 when i sjhould be 3 accordingly to the mock + expect(tableRows.length).toBe(mockData.payload.page.length); + const firstRowContent = tableRows[0].textContent; + expect(firstRowContent).toContain('Service 1'); + expect(firstRowContent).toContain('Description 1'); + })); + }); - describe('ngOnDestroy', () => { - it('should call paginationService.clearPagination and unsubscribe', () => { - spyOn(paginationService, 'clearPagination'); - spyOn(component.isProcessingSub, 'unsubscribe'); - component.ngOnDestroy(); - expect(paginationService.clearPagination).toHaveBeenCalledWith(component.pageConfig.id); - expect(component.isProcessingSub.unsubscribe).toHaveBeenCalled(); + describe('ngOnDestroy', () => { + it('should call paginationService.clearPagination and unsubscribe', () => { + spyOn(paginationService, 'clearPagination'); + spyOn(component.isProcessingSub, 'unsubscribe'); + component.ngOnDestroy(); + expect(paginationService.clearPagination).toHaveBeenCalledWith(component.pageConfig.id); + expect(component.isProcessingSub.unsubscribe).toHaveBeenCalled(); + }); }); - }); - describe('openDeleteModal', () => { - it('should open delete modal', () => { - spyOn(modalService, 'open'); - component.openDeleteModal(component.deleteModal); - expect(modalService.open).toHaveBeenCalledWith(component.deleteModal); + describe('openDeleteModal', () => { + it('should open delete modal', () => { + spyOn(modalService, 'open'); + component.openDeleteModal(component.deleteModal); + expect(modalService.open).toHaveBeenCalledWith(component.deleteModal); + }); }); - }); - describe('closeModal', () => { - it('should close modal and detect changes', () => { - spyOn(component.modalRef, 'close'); - spyOn(component.cdRef, 'detectChanges'); - component.closeModal(); - expect(component.modalRef.close).toHaveBeenCalled(); - expect(component.cdRef.detectChanges).toHaveBeenCalled(); + describe('closeModal', () => { + it('should close modal and detect changes', () => { + spyOn(component.modalRef, 'close'); + spyOn(component.cdRef, 'detectChanges'); + component.closeModal(); + expect(component.modalRef.close).toHaveBeenCalled(); + expect(component.cdRef.detectChanges).toHaveBeenCalled(); + }); }); - }); - describe('deleteSelected', () => { - it('should delete selected service and update data', fakeAsync(() => { - const serviceId = '123'; - const mockRemoteData = { /* insert mock data with service id 123 */ }; - spyOn(component, 'setLdnServices').and.callThrough(); - const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData>)); - component.selectedServiceId = serviceId; - component.deleteSelected(serviceId, ldnServicesService); - tick(); - expect(deleteSpy).toHaveBeenCalledWith(serviceId); - expect(mockRemoteData) - })); - }); + describe('deleteSelected', () => { + it('should delete selected service and update data', fakeAsync(() => { + const serviceId = '123'; + //TODO: finish up this test to use a mockdata containing something + const mockRemoteData = { /* insert mock data with service id 123 */}; + spyOn(component, 'setLdnServices').and.callThrough(); + const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData>)); + component.selectedServiceId = serviceId; + component.deleteSelected(serviceId, ldnServicesService); + tick(); + expect(deleteSpy).toHaveBeenCalledWith(serviceId); + })); + }); }); From d1ae9adaac67a96c339b01aad6a3a174c79a81f1 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Sat, 18 Nov 2023 01:09:52 +0100 Subject: [PATCH 04/10] CST-12498 Added javadocs --- .../ldn-services-directory.component.ts | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts index 476d8ef1b59..4fc503026b3 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts @@ -66,6 +66,9 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { this.setLdnServices(); } + /** + * Sets up the LDN services by fetching and observing the paginated list of services. + */ setLdnServices() { this.ldnServicesRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( switchMap((config) => this.ldnServicesService.findAll(config, false, false).pipe( @@ -94,20 +97,39 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { } } + /** + * Opens the delete confirmation modal. + * + * @param {any} content - The content of the modal. + */ openDeleteModal(content) { this.modalRef = this.modalService.open(content); } + /** + * Closes the currently open modal and triggers change detection. + */ closeModal() { this.modalRef.close(); this.cdRef.detectChanges(); } + /** + * Sets the selected LDN service ID for deletion and opens the delete confirmation modal. + * + * @param {number} serviceId - The ID of the service to be deleted. + */ selectServiceToDelete(serviceId: number) { this.selectedServiceId = serviceId; this.openDeleteModal(this.deleteModal); } + /** + * Deletes the selected LDN service. + * + * @param {string} serviceId - The ID of the service to be deleted. + * @param {LdnServicesService} ldnServicesService - The service for managing LDN services. + */ deleteSelected(serviceId: string, ldnServicesService: LdnServicesService): void { if (this.selectedServiceId !== null) { ldnServicesService.delete(serviceId).pipe(getFirstCompletedRemoteData()).subscribe((rd: RemoteData) => { @@ -134,7 +156,12 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { } } - + /** + * Toggles the status (enabled/disabled) of an LDN service. + * + * @param {any} ldnService - The LDN service object. + * @param {LdnServicesService} ldnServicesService - The service for managing LDN services. + */ toggleStatus(ldnService: any, ldnServicesService: LdnServicesService): void { const newStatus = !ldnService.enabled; const originalStatus = ldnService.enabled; @@ -159,6 +186,4 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { } ); } - - } From f7ca7a688d1cff0ab98a79dfed537b792fbd31f5 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Sat, 18 Nov 2023 01:13:17 +0100 Subject: [PATCH 05/10] CST-12498 Added javadocs --- .../ldn-services-directory.component.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts index 4fc503026b3..3d1341b4711 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts @@ -25,6 +25,11 @@ import {TranslateService} from '@ngx-translate/core'; import { mockLdnServiceRD$ } from "../ldn-service-serviceMock/ldnServicesRD$-mock"; +/** + * The `LdnServicesOverviewComponent` is a component that provides an overview of LDN (Linked Data Notifications) services. + * It displays a paginated list of LDN services, allows users to edit and delete services, + * toggle the status of each service directly form the page and allows for creation of new services redirecting the user on the creation form + */ @Component({ selector: 'ds-ldn-services-directory', templateUrl: './ldn-services-directory.component.html', From 80991e650191e67cff190fe774be2d9fb777e54b Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Mon, 20 Nov 2023 10:11:01 +0100 Subject: [PATCH 06/10] CST-12498 Fixed create form labels alignment --- .../ldn-service-form/ldn-service-form.component.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html index bc71eda994f..650fd8d3be1 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html +++ b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html @@ -178,7 +178,7 @@

{{ 'ldn-create-service.title' | translate }}

-
+
@@ -202,7 +202,7 @@

{{ 'ldn-create-service.title' | translate }}

-
+
@@ -301,7 +301,7 @@

{{ 'ldn-create-service.title' | translate }}

-
+
From f35e6ae0dc7f35c2d534690e6cce706f528346a8 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Mon, 20 Nov 2023 13:09:42 +0100 Subject: [PATCH 07/10] CST-12498 Fixed tests for directory, no more failing ones --- .../ldn-service-form.component.ts | 103 +++++++- .../ldn-itemfilters-data.service.ts | 14 ++ .../ldn-services-directory.component.spec.ts | 233 +++++++++--------- 3 files changed, 226 insertions(+), 124 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts index 2d589547bc7..28aa6c4ef05 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts @@ -19,7 +19,10 @@ import {PaginationComponentOptions} from '../../../shared/pagination/pagination- import {LdnItemfiltersService} from '../ldn-services-data/ldn-itemfilters-data.service'; import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; - +/** + * Angular component representing the form for creating or editing LDN services. + * This component handles the creation, validation, and submission of LDN service data. + */ @Component({ selector: 'ds-ldn-service-form', templateUrl: './ldn-service-form.component.html', @@ -100,23 +103,43 @@ export class LdnServiceFormComponent implements OnInit { } + /** + * Sets up the item filters by fetching and observing the paginated list of item filters. + */ setItemfilters() { this.itemfiltersRD$ = this.ldnItemfiltersService.findAll().pipe( getFirstCompletedRemoteData()); } + /** + * Handles the form submission by opening the confirmation modal. + */ onSubmit() { this.openConfirmModal(this.confirmModal); } + /** + * Opens the confirmation modal. + * + * @param {any} content - The content of the modal. + */ openConfirmModal(content) { this.modalRef = this.modalService.open(content); } + /** + * Opens the reset form modal. + * + * @param {any} content - The content of the modal. + */ openResetFormModal(content) { this.modalRef = this.modalService.open(content); } + /** + * Handles the creation of an LDN service by validating form fields, + * and submitting the form data to the LDN services endpoint. + */ createService() { this.formModel.get('name').markAsTouched(); this.formModel.get('score').markAsTouched(); @@ -175,6 +198,12 @@ export class LdnServiceFormComponent implements OnInit { }); } + /** + * Checks if at least one pattern in the specified form array has a value. + * + * @param {FormArray} formArray - The form array containing patterns to check. + * @returns {boolean} - True if at least one pattern has a value, otherwise false. + */ checkPatterns(formArray: FormArray): boolean { for (let i = 0; i < formArray.length; i++) { const pattern = formArray.at(i).get('pattern').value; @@ -185,37 +214,65 @@ export class LdnServiceFormComponent implements OnInit { return false; } - + /** + * Closes the currently open modal and returns to the services directory.. + */ resetFormAndLeave() { this.sendBack(); this.closeModal(); } + /** + * Closes the currently open modal and triggers change detection. + */ closeModal() { this.modalRef.close(); this.cdRef.detectChanges(); } + /** + * Adds a new inbound pattern form group to the notifyServiceInboundPatterns form array. + */ addInboundPattern() { const notifyServiceInboundPatternsArray = this.formModel.get('notifyServiceInboundPatterns') as FormArray; notifyServiceInboundPatternsArray.push(this.createInboundPatternFormGroup()); } + /** + * Removes the inbound pattern form group at the specified index from the notifyServiceInboundPatterns form array. + * + * @param {number} index - The index of the inbound pattern form group to remove. + * @memberof LdnServiceFormComponent + */ removeInboundPattern(index: number) { const notifyServiceInboundPatternsArray = this.formModel.get('notifyServiceInboundPatterns') as FormArray; notifyServiceInboundPatternsArray.removeAt(index); } + /** + * Adds a new outbound pattern form group to the notifyServiceOutboundPatterns form array. + */ addOutboundPattern() { const notifyServiceOutboundPatternsArray = this.formModel.get('notifyServiceOutboundPatterns') as FormArray; notifyServiceOutboundPatternsArray.push(this.createOutboundPatternFormGroup()); } + /** + * Removes the outbound pattern form group at the specified index from the notifyServiceOutboundPatterns form array. + * + * @param {number} index - The index of the outbound pattern form group to remove. + */ removeOutboundPattern(index: number) { const notifyServiceOutboundPatternsArray = this.formModel.get('notifyServiceOutboundPatterns') as FormArray; notifyServiceOutboundPatternsArray.removeAt(index); } + /** + * Toggles the value of the 'automatic' control at the specified index in the notifyServiceInboundPatterns form array. + * + * @param {number} i - The index of the 'automatic' control to toggle. + * @memberof LdnServiceFormComponent + */ toggleAutomatic(i: number) { const automaticControl = this.formModel.get(`notifyServiceInboundPatterns.${i}.automatic`); if (automaticControl) { @@ -223,7 +280,12 @@ export class LdnServiceFormComponent implements OnInit { } } - + /** + * Selects an outbound pattern for a specific index in the notifyServiceOutboundPatterns form array. + * + * @param {string} patternValue - The selected pattern value. + * @param {number} index - The index of the outbound pattern in the form array. + */ selectOutboundPattern(patternValue: string, index: number): void { const patternArray = (this.formModel.get('notifyServiceOutboundPatterns') as FormArray) patternArray.controls[index].patchValue({pattern: patternValue}) @@ -231,6 +293,12 @@ export class LdnServiceFormComponent implements OnInit { } + /** + * Selects an inbound pattern for a specific index in the form array. + * + * @param {string} patternValue - The selected pattern value. + * @param {number} index - The index of the inbound pattern in the form array. + */ selectInboundPattern(patternValue: string, index: number): void { const patternArray = (this.formModel.get('notifyServiceInboundPatterns') as FormArray) patternArray.controls[index].patchValue({pattern: patternValue}) @@ -238,21 +306,40 @@ export class LdnServiceFormComponent implements OnInit { } + /** + * Selects an inbound item filter for a specific index in the form array. + * + * @param {string} filterValue - The selected item filter value. + * @param {number} index - The index of the inbound item filter in the form array. + */ selectInboundItemFilter(filterValue: string, index: number): void { const filterArray = (this.formModel.get('notifyServiceInboundPatterns') as FormArray) filterArray.controls[index].patchValue({constraint: filterValue}) } + /** + * Selects an outbound item filter for a specific index in the form array. + * + * @param {string} filterValue - The selected item filter value. + * @param {number} index - The index of the outbound item filter in the form array. + */ selectOutboundItemFilter(filterValue: string, index: number) { const filterArray = (this.formModel.get('notifyServiceOutboundPatterns') as FormArray) filterArray.controls[index].patchValue({constraint: filterValue}) } + /** + * Sends the user back to the LDN services list. + */ private sendBack() { this.router.navigateByUrl('admin/ldn/services'); } - - + /** + * Creates a form group for an outbound pattern in the notifyServiceOutboundPatterns form array. + * + * @private + * @returns {FormGroup} - The created form group. + */ private createOutboundPatternFormGroup(): FormGroup { return this.formBuilder.group({ pattern: [''], @@ -261,6 +348,12 @@ export class LdnServiceFormComponent implements OnInit { }); } + /** + * Creates a form group for an inbound pattern in the notifyServiceInboundPatterns form array. + * + * @private + * @returns {FormGroup} - The created form group. + */ private createInboundPatternFormGroup(): FormGroup { return this.formBuilder.group({ pattern: [''], diff --git a/src/app/admin/admin-ldn-services/ldn-services-data/ldn-itemfilters-data.service.ts b/src/app/admin/admin-ldn-services/ldn-services-data/ldn-itemfilters-data.service.ts index 7aa6231b4d4..15a7bcccdaa 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-data/ldn-itemfilters-data.service.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-data/ldn-itemfilters-data.service.ts @@ -37,10 +37,24 @@ export class LdnItemfiltersService extends IdentifiableDataService i this.findAllData = new FindAllDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive); } + /** + * Gets the endpoint URL for the itemfilters. + * + * @returns {string} - The endpoint URL. + */ getEndpoint() { return this.halService.getEndpoint(this.linkPath); } + /** + * Finds all itemfilters based on the provided options and link configurations. + * + * @param {FindListOptions} options - The options for finding a list of itemfilters. + * @param {boolean} useCachedVersionIfAvailable - Whether to use the cached version if available. + * @param {boolean} reRequestOnStale - Whether to re-request the data if it's stale. + * @param {...FollowLinkConfig[]} linksToFollow - Configurations for following specific links. + * @returns {Observable>>} - An observable of remote data containing a paginated list of itemfilters. + */ findAll(options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig[]): Observable>> { return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); } diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts index 8213e5bc535..c5ec66c7574 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts @@ -12,137 +12,132 @@ import {LdnService} from "../ldn-services-model/ldn-services.model"; import {PaginatedList} from "../../../core/data/paginated-list.model"; import {RemoteData} from "../../../core/data/remote-data"; import {LdnServicesOverviewComponent} from './ldn-services-directory.component'; +import {createSuccessfulRemoteDataObject$} from "../../../shared/remote-data.utils"; +import {createPaginatedList} from "../../../shared/testing/utils.test"; -describe('LdnServicesOverviewComponent', () => { - let component: LdnServicesOverviewComponent; - let fixture: ComponentFixture; - let ldnServicesService: LdnServicesService; - let paginationService: PaginationService; - let modalService: NgbModal; - let notificationsService: NotificationsService; - let translateService: TranslateService; +describe('LdnServicesOverviewComponent', ( ) => { + let component: LdnServicesOverviewComponent; + let fixture: ComponentFixture; + let ldnServicesService; + let paginationService; + let modalService: NgbModal; + let notificationsService: NotificationsService; + let translateService: TranslateService; - const translateServiceStub = { - get: () => of('translated-text'), - onLangChange: new EventEmitter(), - onTranslationChange: new EventEmitter(), - onDefaultLangChange: new EventEmitter() - }; + const translateServiceStub = { + get: () => of('translated-text'), + onLangChange: new EventEmitter(), + onTranslationChange: new EventEmitter(), + onDefaultLangChange: new EventEmitter() + }; - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [TranslateModule.forRoot()], - declarations: [LdnServicesOverviewComponent], - providers: [ - {provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch'])}, - {provide: PaginationService, useValue: new PaginationServiceStub()}, - { - provide: NgbModal, useValue: { - open: () => { /*comment*/ - } - } - }, - {provide: ChangeDetectorRef, useValue: {}}, - {provide: NotificationsService, useValue: NotificationsServiceStub}, - {provide: TranslateService, useValue: translateServiceStub}, - ] - }).compileComponents(); - }); + beforeEach(async () => { + paginationService = new PaginationServiceStub(); + ldnServicesService = jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch']) + await TestBed.configureTestingModule({ + imports: [TranslateModule.forRoot()], + declarations: [LdnServicesOverviewComponent], + providers: [ + { + provide: LdnServicesService, + useValue: ldnServicesService + }, + {provide: PaginationService, useValue: paginationService}, + { + provide: NgbModal, useValue: { + open: () => { /*comment*/} + } + }, + {provide: ChangeDetectorRef, useValue: {}}, + {provide: NotificationsService, useValue: NotificationsServiceStub}, + {provide: TranslateService, useValue: translateServiceStub}, + ] + }).compileComponents(); + }); - beforeEach(() => { - fixture = TestBed.createComponent(LdnServicesOverviewComponent); - component = fixture.componentInstance; - ldnServicesService = TestBed.inject(LdnServicesService); - paginationService = TestBed.inject(PaginationService); - modalService = TestBed.inject(NgbModal); - notificationsService = TestBed.inject(NotificationsService); - translateService = TestBed.inject(TranslateService); - component.modalRef = jasmine.createSpyObj({close: null}); - component.isProcessingSub = jasmine.createSpyObj({unsubscribe: null}); - component.ldnServicesRD$ = of({} as RemoteData>); - fixture.detectChanges(); - }); + beforeEach(() => { + fixture = TestBed.createComponent(LdnServicesOverviewComponent); + component = fixture.componentInstance; + ldnServicesService = TestBed.inject(LdnServicesService); + paginationService = TestBed.inject(PaginationService); + modalService = TestBed.inject(NgbModal); + notificationsService = TestBed.inject(NotificationsService); + translateService = TestBed.inject(TranslateService); + component.modalRef = jasmine.createSpyObj({close: null}); + component.isProcessingSub = jasmine.createSpyObj({unsubscribe: null}); + component.ldnServicesRD$ = of({} as RemoteData>); + fixture.detectChanges(); + }); - it('should create', () => { - expect(component).toBeTruthy(); - }); + it('should create', () => { + expect(component).toBeTruthy(); + }); - describe('ngOnInit', () => { - it('should call setLdnServices', fakeAsync(() => { - spyOn(component, 'setLdnServices').and.callThrough(); - component.ngOnInit(); - tick(); - expect(component.setLdnServices).toHaveBeenCalled(); - })); + describe('ngOnInit', () => { + it('should call setLdnServices', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + component.ngOnInit(); + tick(); + expect(component.setLdnServices).toHaveBeenCalled(); + })); - it('should set ldnServicesRD$ with mock data', fakeAsync(() => { - spyOn(component, 'setLdnServices').and.callThrough(); - const mockData = { - payload: { - page: [ - {id: 1, name: 'Service 1', description: 'Description 1', enabled: true}, - {id: 2, name: 'Service 2', description: 'Description 2', enabled: false}, - {id: 3, name: 'Service 3', description: 'Description 3', enabled: true}, - ], - totalElements: 3 - } - } as RemoteData>; - component.ldnServicesRD$ = of(mockData as RemoteData>); - component.ngOnInit(); - tick(); - expect(component.setLdnServices).toHaveBeenCalled(); - fixture.detectChanges(); - console.log(fixture.nativeElement.innerHTML); - console.log('Mock Data:', mockData); - const tableRows = fixture.nativeElement.querySelectorAll('tbody tr'); - console.log('Table Rows:', tableRows); - //TODO: check why the tbody tr length table rows is 0 when i sjhould be 3 accordingly to the mock - expect(tableRows.length).toBe(mockData.payload.page.length); - const firstRowContent = tableRows[0].textContent; - expect(firstRowContent).toContain('Service 1'); - expect(firstRowContent).toContain('Description 1'); - })); - }); + it('should set ldnServicesRD$ with mock data', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + const testData: LdnService[] = Object.assign([new LdnService()], [ + {id: 1, name: 'Service 1', description: 'Description 1', enabled: true}, + {id: 2, name: 'Service 2', description: 'Description 2', enabled: false}, + {id: 3, name: 'Service 3', description: 'Description 3', enabled: true}]); - describe('ngOnDestroy', () => { - it('should call paginationService.clearPagination and unsubscribe', () => { - spyOn(paginationService, 'clearPagination'); - spyOn(component.isProcessingSub, 'unsubscribe'); - component.ngOnDestroy(); - expect(paginationService.clearPagination).toHaveBeenCalledWith(component.pageConfig.id); - expect(component.isProcessingSub.unsubscribe).toHaveBeenCalled(); - }); - }); + const mockLdnServicesRD = createPaginatedList(testData) + component.ldnServicesRD$ = createSuccessfulRemoteDataObject$(mockLdnServicesRD); + fixture.detectChanges(); + + const tableRows = fixture.debugElement.nativeElement.querySelectorAll('tbody tr'); + expect(tableRows.length).toBe(testData.length); + const firstRowContent = tableRows[0].textContent; + expect(firstRowContent).toContain('Service 1'); + expect(firstRowContent).toContain('Description 1'); + })); + }); - describe('openDeleteModal', () => { - it('should open delete modal', () => { - spyOn(modalService, 'open'); - component.openDeleteModal(component.deleteModal); - expect(modalService.open).toHaveBeenCalledWith(component.deleteModal); - }); + describe('ngOnDestroy', () => { + it('should call paginationService.clearPagination and unsubscribe', () => { + // spyOn(paginationService, 'clearPagination'); + // spyOn(component.isProcessingSub, 'unsubscribe'); + component.ngOnDestroy(); + expect(paginationService.clearPagination).toHaveBeenCalledWith(component.pageConfig.id); + expect(component.isProcessingSub.unsubscribe).toHaveBeenCalled(); }); + }); - describe('closeModal', () => { - it('should close modal and detect changes', () => { - spyOn(component.modalRef, 'close'); - spyOn(component.cdRef, 'detectChanges'); - component.closeModal(); - expect(component.modalRef.close).toHaveBeenCalled(); - expect(component.cdRef.detectChanges).toHaveBeenCalled(); - }); + describe('openDeleteModal', () => { + it('should open delete modal', () => { + spyOn(modalService, 'open'); + component.openDeleteModal(component.deleteModal); + expect(modalService.open).toHaveBeenCalledWith(component.deleteModal); }); + }); - describe('deleteSelected', () => { - it('should delete selected service and update data', fakeAsync(() => { - const serviceId = '123'; - //TODO: finish up this test to use a mockdata containing something - const mockRemoteData = { /* insert mock data with service id 123 */}; - spyOn(component, 'setLdnServices').and.callThrough(); - const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData>)); - component.selectedServiceId = serviceId; - component.deleteSelected(serviceId, ldnServicesService); - tick(); - expect(deleteSpy).toHaveBeenCalledWith(serviceId); - })); + describe('closeModal', () => { + it('should close modal and detect changes', () => { + // spyOn(component.modalRef, 'close'); + spyOn(component.cdRef, 'detectChanges'); + component.closeModal(); + expect(component.modalRef.close).toHaveBeenCalled(); + expect(component.cdRef.detectChanges).toHaveBeenCalled(); }); + }); + + describe('deleteSelected', () => { + it('should delete selected service and update data', fakeAsync(() => { + const serviceId = '123'; + const mockRemoteData = { /* just an empty object to retrieve as as RemoteData> */}; + spyOn(component, 'setLdnServices').and.callThrough(); + const deleteSpy = ldnServicesService.delete.and.returnValue(of(mockRemoteData as RemoteData>)); + component.selectedServiceId = serviceId; + component.deleteSelected(serviceId, ldnServicesService); + tick(); + expect(deleteSpy).toHaveBeenCalledWith(serviceId); + })); + }); }); From 36778a09cbb04349dbb61e7bc4ab1b3f854ce2ba Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Mon, 20 Nov 2023 13:27:39 +0100 Subject: [PATCH 08/10] CST-12498 Added last javadocs and removed console.log and comments --- .../ldn-service-form-edit.component.ts | 133 ++++++++++++++++-- .../ldnServicesRD$-mock.ts | 61 -------- .../ldn-services-data.service.ts | 74 +++++++++- .../ldn-services-directory.component.ts | 19 +-- .../ldn-service-patterns.model.ts | 2 +- 5 files changed, 199 insertions(+), 90 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts b/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts index 002aaf68122..a9659b4cce2 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts @@ -21,6 +21,10 @@ import {FindListOptions} from '../../../core/data/find-list-options.model'; import {PaginationComponentOptions} from '../../../shared/pagination/pagination-component-options.model'; import {NotifyServicePattern} from "../ldn-services-model/ldn-service-patterns.model"; + +/** + * Component for editing LDN service through a form that allows to edit the properties of the selected service + */ @Component({ selector: 'ds-ldn-service-form-edit', templateUrl: './ldn-service-form-edit.component.html', @@ -109,12 +113,18 @@ export class LdnServiceFormEditComponent implements OnInit { this.setItemfilters(); } + /** + * Sets item filters using LDN item filters service + */ setItemfilters() { this.itemfiltersRD$ = this.ldnItemfiltersService.findAll().pipe( getFirstCompletedRemoteData()); } - + /** + * Fetches LDN service data by ID and updates the form + * @param serviceId - The ID of the LDN service + */ fetchServiceData(serviceId: string): void { this.ldnServicesService.findById(serviceId).pipe( getFirstCompletedRemoteData() @@ -139,6 +149,11 @@ export class LdnServiceFormEditComponent implements OnInit { ); } + /** + * Filters pattern objects, initializes form groups, assigns labels, and adds them to the specified form array so the correct string is shown in the dropdown.. + * @param formArrayName - The name of the form array to be populated + * @param isOutbound - A boolean indicating whether the patterns are outbound (true) or inbound (false) + */ filterPatternObjectsAndPickLabel(formArrayName: string, isOutbound: boolean) { const PatternsArray = this.formModel.get(formArrayName) as FormArray; PatternsArray.clear(); @@ -168,7 +183,10 @@ export class LdnServiceFormEditComponent implements OnInit { } - + /** + * Generates an array of patch operations based on form changes + * @returns Array of patch operations + */ generatePatchOperations(): any[] { const patchOperations: any[] = []; @@ -201,44 +219,75 @@ export class LdnServiceFormEditComponent implements OnInit { return patchOperations; } + /** + * Submits the form by opening the confirmation modal + */ onSubmit() { this.openConfirmModal(this.confirmModal); } + /** + * Adds a new inbound pattern form group to the array of inbound patterns in the form + */ addInboundPattern() { const notifyServiceInboundPatternsArray = this.formModel.get('notifyServiceInboundPatterns') as FormArray; notifyServiceInboundPatternsArray.push(this.createInboundPatternFormGroup()); } + /** + * Adds a new outbound pattern form group to the array of outbound patterns in the form + */ addOutboundPattern() { const notifyServiceOutboundPatternsArray = this.formModel.get('notifyServiceOutboundPatterns') as FormArray; notifyServiceOutboundPatternsArray.push(this.createOutboundPatternFormGroup()); } - + /** + * Selects an outbound pattern by updating its values based on the provided pattern value and index + * @param patternValue - The selected pattern value + * @param index - The index of the outbound pattern in the array + */ selectOutboundPattern(patternValue: string, index: number): void { const patternArray = (this.formModel.get('notifyServiceOutboundPatterns') as FormArray) patternArray.controls[index].patchValue({pattern: patternValue}) patternArray.controls[index].patchValue({patternLabel: this.translateService.instant('ldn-service.form.pattern.' + patternValue + '.label')}) } - + /** + * Selects an outbound item filter by updating its value based on the provided filter value and index + * @param filterValue - The selected filter value + * @param index - The index of the inbound pattern in the array + */ selectOutboundItemFilter(filterValue: string, index: number) { const filterArray = (this.formModel.get('notifyServiceOutboundPatterns') as FormArray) filterArray.controls[index].patchValue({constraint: filterValue}) } + /** + * Selects an inbound pattern by updating its values based on the provided pattern value and index + * @param patternValue - The selected pattern value + * @param index - The index of the inbound pattern in the array + */ selectInboundPattern(patternValue: string, index: number): void { const patternArray = (this.formModel.get('notifyServiceInboundPatterns') as FormArray) patternArray.controls[index].patchValue({pattern: patternValue}) patternArray.controls[index].patchValue({patternLabel: this.translateService.instant('ldn-service.form.pattern.' + patternValue + '.label')}) } + /** + * Selects an inbound item filter by updating its value based on the provided filter value and index + * @param filterValue - The selected filter value + * @param index - The index of the inbound pattern in the array + */ selectInboundItemFilter(filterValue: string, index: number): void { const filterArray = (this.formModel.get('notifyServiceInboundPatterns') as FormArray) filterArray.controls[index].patchValue({constraint: filterValue}) } + /** + * Toggles the automatic property of an inbound pattern at the specified index + * @param i - The index of the inbound pattern in the array + */ toggleAutomatic(i: number) { const automaticControl = this.formModel.get(`notifyServiceInboundPatterns.${i}.automatic`); if (automaticControl) { @@ -246,6 +295,9 @@ export class LdnServiceFormEditComponent implements OnInit { } } + /** + * Toggles the enabled status of the LDN service by sending a patch request + */ toggleEnabled() { const newStatus = !this.formModel.get('enabled').value; @@ -266,20 +318,33 @@ export class LdnServiceFormEditComponent implements OnInit { ); } - + /** + * Closes the modal + */ closeModal() { this.modalRef.close(); this.cdRef.detectChanges(); } + /** + * Opens a confirmation modal with the specified content + * @param content - The content to be displayed in the modal + */ openConfirmModal(content) { this.modalRef = this.modalService.open(content); } + /** + * Opens a reset form modal with the specified content + * @param content - The content to be displayed in the modal + */ openResetFormModal(content) { this.modalRef = this.modalService.open(content); } + /** + * Patches the LDN service by retrieving and sending patch operations geenrated in generatePatchOperations() + */ patchService() { this.deleteMarkedInboundPatterns(); this.deleteMarkedOutboundPatterns(); @@ -304,17 +369,28 @@ export class LdnServiceFormEditComponent implements OnInit { }); } + /** + * Resets the form and navigates back to the LDN services page + */ resetFormAndLeave() { this.sendBack(); this.closeModal(); } + /** + * Marks the specified inbound pattern for deletion + * @param index - The index of the inbound pattern in the array + */ markForInboundPatternDeletion(index: number) { if (!this.markedForDeletionInboundPattern.includes(index)) { this.markedForDeletionInboundPattern.push(index); } } + /** + * Unmarks the specified inbound pattern for deletion + * @param index - The index of the inbound pattern in the array + */ unmarkForInboundPatternDeletion(index: number) { const i = this.markedForDeletionInboundPattern.indexOf(index); if (i !== -1) { @@ -322,19 +398,29 @@ export class LdnServiceFormEditComponent implements OnInit { } } + /** + * Marks the specified outbound pattern for deletion + * @param index - The index of the outbound pattern in the array + */ markForOutboundPatternDeletion(index: number) { if (!this.markedForDeletionOutboundPattern.includes(index)) { this.markedForDeletionOutboundPattern.push(index); } } + /** + * Unmarks the specified outbound pattern for deletion + * @param index - The index of the outbound pattern in the array + */ unmarkForOutboundPatternDeletion(index: number) { const i = this.markedForDeletionOutboundPattern.indexOf(index); if (i !== -1) { this.markedForDeletionOutboundPattern.splice(i, 1); } } - + /** + * Deletes marked inbound patterns from the form model + */ deleteMarkedInboundPatterns() { this.markedForDeletionInboundPattern.sort((a, b) => b - a); const patternsArray = this.formModel.get('notifyServiceInboundPatterns') as FormArray; @@ -354,7 +440,9 @@ export class LdnServiceFormEditComponent implements OnInit { this.markedForDeletionInboundPattern = []; } - + /** + * Deletes marked outbound patterns from the form model + */ deleteMarkedOutboundPatterns() { this.markedForDeletionOutboundPattern.sort((a, b) => b - a); const patternsArray = this.formModel.get('notifyServiceOutboundPatterns') as FormArray; @@ -375,7 +463,12 @@ export class LdnServiceFormEditComponent implements OnInit { this.markedForDeletionOutboundPattern = []; } - + /** + * Creates a replace operation and adds it to the patch operations if the form control is dirty + * @param patchOperations - The array to store patch operations + * @param formControlName - The name of the form control + * @param path - The JSON Patch path for the operation + */ private createReplaceOperation(patchOperations: any[], formControlName: string, path: string): void { if (this.formModel.get(formControlName).dirty) { patchOperations.push({ @@ -386,6 +479,11 @@ export class LdnServiceFormEditComponent implements OnInit { } } + /** + * Handles patterns in the form array, checking if an add or replace operations is required + * @param patchOperations - The array to store patch operations + * @param formArrayName - The name of the form array + */ private handlePatterns(patchOperations: any[], formArrayName: string): void { const patternsArray = this.formModel.get(formArrayName) as FormArray @@ -416,10 +514,17 @@ export class LdnServiceFormEditComponent implements OnInit { } } + /** + * Navigates back to the LDN services page + */ private sendBack() { this.router.navigateByUrl('admin/ldn/services'); } + /** + * Creates a form group for outbound patterns + * @returns The form group for outbound patterns + */ private createOutboundPatternFormGroup(): FormGroup { return this.formBuilder.group({ pattern: '', @@ -429,6 +534,10 @@ export class LdnServiceFormEditComponent implements OnInit { }); } + /** + * Creates a form group for inbound patterns + * @returns The form group for inbound patterns + */ private createInboundPatternFormGroup(): FormGroup { return this.formBuilder.group({ pattern: '', @@ -439,6 +548,10 @@ export class LdnServiceFormEditComponent implements OnInit { }); } + /** + * Initializes an existing form group for outbound patterns + * @returns The initialized form group for outbound patterns + */ private initializeOutboundPatternFormGroup(): FormGroup { return this.formBuilder.group({ pattern: '', @@ -447,6 +560,10 @@ export class LdnServiceFormEditComponent implements OnInit { }); } + /** + * Initializes an existing form group for inbound patterns + * @returns The initialized form group for inbound patterns + */ private initializeInboundPatternFormGroup(): FormGroup { return this.formBuilder.group({ pattern: '', diff --git a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts index 5d409964c26..005bf0cf0d1 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts @@ -44,8 +44,6 @@ export const mockLdnService: LdnService = { }, }; -//export const mockLdnServiceRD$: Observable>> = of((mockLdnService as unknown) as RemoteData>); -//export const mockLdnServiceRD$ = createSuccessfulRemoteDataObject$(createPaginatedList(mockLdnService[0])as PaginatedList); export const mockLdnServiceRD$ = createSuccessfulRemoteDataObject$(mockLdnService); @@ -126,62 +124,3 @@ export const mockLdnServices: LdnService[] = [{ } ] export const mockLdnServicesRD$: Observable>> = of((mockLdnServices as unknown) as RemoteData>); - - -/*export const mockLdnServiceRD$: RemoteData> = { - errorMessage: null, - lastUpdated: 1700176600821, - msToLive: 900000, - payload: { - page: [mockLdnService], - pageInfo: { - elementsPerPage: 20, - totalPages: 1, - totalElements: 1, - currentPage: 1, - }, - type: {value: "paginated-list"}, - _links: { - self: { - href: "http://localhost:8080/server/api/ldn/ldnservices?size=20&sort=dc.title,ASC" - }, - page: [ - { - "href": "http://localhost/api/ldn/ldnservices/1" - } - ] - }, - }, - statusCode: 200, - state: 'Success', - timeCompleted: 1700176600821, -}*/ - - - -const mockLdnServices2 = { - payload: { - elementsPerPage: 20, - totalPages: 1, - totalElements: 1, - currentPage: 1, - first: undefined, - prev: undefined, - next: undefined, - last: undefined, - page: [mockLdnService], - type: LDN_SERVICE, - self: undefined, - getPageLength: function () { - return this.page.length; - }, - _links: { - self: { - href: 'http://localhost/api/ldn/ldnservices/1', - }, - page: [], - }, - }, - hasSucceeded: true, - msToLive: 0, -}; diff --git a/src/app/admin/admin-ldn-services/ldn-services-data/ldn-services-data.service.ts b/src/app/admin/admin-ldn-services/ldn-services-data/ldn-services-data.service.ts index b8d28f08869..e7c2f471591 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-data/ldn-services-data.service.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-data/ldn-services-data.service.ts @@ -35,7 +35,15 @@ import {SearchDataImpl} from '../../../core/data/base/search-data'; import {RequestParam} from '../../../core/cache/models/request-param.model'; /** - * A service responsible for fetching/sending data from/to the REST API on the ldnservices endpoint + * Injectable service responsible for fetching/sending data from/to the REST API on the ldnservices endpoint. + * + * @export + * @class LdnServicesService + * @extends {IdentifiableDataService} + * @implements {FindAllData} + * @implements {DeleteData} + * @implements {PatchData} + * @implements {CreateData} */ @Injectable() @dataService(LDN_SERVICE) @@ -65,41 +73,103 @@ export class LdnServicesService extends IdentifiableDataService impl this.createData = new CreateDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, notificationsService, this.responseMsToLive); } - + /** + * Creates an LDN service by sending a POST request to the REST API. + * + * @param {LdnService} object - The LDN service object to be created. + * @returns {Observable>} - Observable containing the result of the creation operation. + */ create(object: LdnService): Observable> { return this.createData.create(object); } + /** + * Updates an LDN service by applying a set of operations through a PATCH request to the REST API. + * + * @param {LdnService} object - The LDN service object to be updated. + * @param {Operation[]} operations - The patch operations to be applied. + * @returns {Observable>} - Observable containing the result of the update operation. + */ patch(object: LdnService, operations: Operation[]): Observable> { return this.patchData.patch(object, operations); } + /** + * Updates an LDN service by sending a PUT request to the REST API. + * + * @param {LdnService} object - The LDN service object to be updated. + * @returns {Observable>} - Observable containing the result of the update operation. + */ update(object: LdnService): Observable> { return this.patchData.update(object); } + /** + * Commits pending updates by sending a PATCH request to the REST API. + * + * @param {RestRequestMethod} [method] - The HTTP method to be used for the request. + */ commitUpdates(method?: RestRequestMethod): void { return this.patchData.commitUpdates(method); } + /** + * Creates a patch representing the changes made to the LDN service in the cache. + * + * @param {LdnService} object - The LDN service object for which to create the patch. + * @returns {Observable} - Observable containing the patch operations. + */ createPatchFromCache(object: LdnService): Observable { return this.patchData.createPatchFromCache(object); } + /** + * Retrieves all LDN services from the REST API based on the provided options. + * + * @param {FindListOptions} [options] - The options to be applied to the request. + * @param {boolean} [useCachedVersionIfAvailable] - Flag indicating whether to use cached data if available. + * @param {boolean} [reRequestOnStale] - Flag indicating whether to re-request data if it's stale. + * @param {...FollowLinkConfig[]} linksToFollow - Optional links to follow during the request. + * @returns {Observable>>} - Observable containing the result of the request. + */ findAll(options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig[]): Observable>> { return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); } + /** + * Retrieves LDN services based on the inbound pattern from the REST API. + * + * @param {string} pattern - The inbound pattern to be used in the search. + * @param {FindListOptions} [options] - The options to be applied to the request. + * @param {boolean} [useCachedVersionIfAvailable] - Flag indicating whether to use cached data if available. + * @param {boolean} [reRequestOnStale] - Flag indicating whether to re-request data if it's stale. + * @param {...FollowLinkConfig[]} linksToFollow - Optional links to follow during the request. + * @returns {Observable>>} - Observable containing the result of the request. + */ findByInboundPattern(pattern: string, options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig[]): Observable>> { const params = [new RequestParam('pattern', pattern)]; const findListOptions = Object.assign(new FindListOptions(), options, {searchParams: params}); return this.searchData.searchBy(this.findByPatternEndpoint, findListOptions, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); } + /** + * Deletes an LDN service by sending a DELETE request to the REST API. + * + * @param {string} objectId - The ID of the LDN service to be deleted. + * @param {string[]} [copyVirtualMetadata] - Optional virtual metadata to be copied during the deletion. + * @returns {Observable>} - Observable containing the result of the deletion operation. + */ public delete(objectId: string, copyVirtualMetadata?: string[]): Observable> { return this.deleteData.delete(objectId, copyVirtualMetadata); } + /** + * Deletes an LDN service by its HATEOAS link. + * + * @param {string} href - The HATEOAS link of the LDN service to be deleted. + * @param {string[]} [copyVirtualMetadata] - Optional virtual metadata to be copied during the deletion. + * @returns {Observable>} - Observable containing the result of the deletion operation. + */ public deleteByHref(href: string, copyVirtualMetadata?: string[]): Observable> { return this.deleteData.deleteByHref(href, copyVirtualMetadata); } diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts index 3d1341b4711..12d196023e1 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts @@ -22,7 +22,6 @@ import {Operation} from 'fast-json-patch'; import {getFirstCompletedRemoteData} from '../../../core/shared/operators'; import {NotificationsService} from '../../../shared/notifications/notifications.service'; import {TranslateService} from '@ngx-translate/core'; -import { mockLdnServiceRD$ } from "../ldn-service-serviceMock/ldnServicesRD$-mock"; /** @@ -42,9 +41,6 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { servicesData: any[] = []; @ViewChild('deleteModal', {static: true}) deleteModal: TemplateRef; ldnServicesRD$: Observable>>; - //TODO: remove mocks an put in test after finishing - //mockLdnServiceRD$: Observable>; - // mockLdnServicesRD$: Observable>>; config: FindListOptions = Object.assign(new FindListOptions(), { elementsPerPage: 20 }); @@ -79,20 +75,7 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { switchMap((config) => this.ldnServicesService.findAll(config, false, false).pipe( getFirstCompletedRemoteData() )) - - );/* - this.mockLdnServiceRD$ = mockLdnServiceRD$ - - this.mockLdnServicesRD$ = this.paginationService.getFindListOptions(this.pageConfig.id, this.config).pipe( - switchMap((config) => this.ldnServicesService.findAll(config, false, false).pipe( - getFirstCompletedRemoteData() - )) - - );this.paginationService.getFindListOptions(this.pageConfig.id, this.config) - this.ldnServicesRD$.subscribe((rd: RemoteData>) => {console.log('realremotedata:',rd);}) - this.mockLdnServiceRD$.subscribe((rd: RemoteData) => {console.log('mockremotedata:',rd);}) - this.mockLdnServicesRD$.subscribe((rd: RemoteData>) => {console.log('mockremotedata[ldnservice]:',rd);})*/ - + ); } ngOnDestroy(): void { diff --git a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts index 7fb8072a6f3..295426ba878 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-model/ldn-service-patterns.model.ts @@ -1,7 +1,7 @@ import {autoserialize} from 'cerialize'; /** - * notify service patterns + * A single notify service pattern and his properties */ export class NotifyServicePattern { @autoserialize From 532f4725b261ce4b60e3c3d24675f237ffe04dbd Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Mon, 20 Nov 2023 13:29:44 +0100 Subject: [PATCH 09/10] CST-12498 Final refactor --- .../ldn-service-form-edit.component.ts | 7 +++++-- .../ldn-service-form.component.ts | 3 ++- .../ldnServicesRD$-mock.ts | 2 +- .../ldn-services-directory.component.spec.ts | 5 +++-- .../ldn-services-directory.component.ts | 15 +++++++-------- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts b/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts index a9659b4cce2..b4ec221d5b6 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-form-edit/ldn-service-form-edit.component.ts @@ -183,6 +183,7 @@ export class LdnServiceFormEditComponent implements OnInit { } + /** * Generates an array of patch operations based on form changes * @returns Array of patch operations @@ -253,6 +254,7 @@ export class LdnServiceFormEditComponent implements OnInit { patternArray.controls[index].patchValue({patternLabel: this.translateService.instant('ldn-service.form.pattern.' + patternValue + '.label')}) } + /** * Selects an outbound item filter by updating its value based on the provided filter value and index * @param filterValue - The selected filter value @@ -370,8 +372,8 @@ export class LdnServiceFormEditComponent implements OnInit { } /** - * Resets the form and navigates back to the LDN services page - */ + * Resets the form and navigates back to the LDN services page + */ resetFormAndLeave() { this.sendBack(); this.closeModal(); @@ -418,6 +420,7 @@ export class LdnServiceFormEditComponent implements OnInit { this.markedForDeletionOutboundPattern.splice(i, 1); } } + /** * Deletes marked inbound patterns from the form model */ diff --git a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts index 28aa6c4ef05..d903fec63e0 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.ts @@ -137,7 +137,7 @@ export class LdnServiceFormComponent implements OnInit { } /** - * Handles the creation of an LDN service by validating form fields, + * Handles the creation of an LDN service by retrieving and validating form fields, * and submitting the form data to the LDN services endpoint. */ createService() { @@ -334,6 +334,7 @@ export class LdnServiceFormComponent implements OnInit { private sendBack() { this.router.navigateByUrl('admin/ldn/services'); } + /** * Creates a form group for an outbound pattern in the notifyServiceOutboundPatterns form array. * diff --git a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts index 005bf0cf0d1..bf40c34efe0 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts +++ b/src/app/admin/admin-ldn-services/ldn-service-serviceMock/ldnServicesRD$-mock.ts @@ -84,7 +84,7 @@ export const mockLdnServices: LdnService[] = [{ get self(): string { return ""; }, -},{ +}, { uuid: "2", enabled: false, score: 0, diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts index c5ec66c7574..35bf3d598ce 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts @@ -15,7 +15,7 @@ import {LdnServicesOverviewComponent} from './ldn-services-directory.component'; import {createSuccessfulRemoteDataObject$} from "../../../shared/remote-data.utils"; import {createPaginatedList} from "../../../shared/testing/utils.test"; -describe('LdnServicesOverviewComponent', ( ) => { +describe('LdnServicesOverviewComponent', () => { let component: LdnServicesOverviewComponent; let fixture: ComponentFixture; let ldnServicesService; @@ -45,7 +45,8 @@ describe('LdnServicesOverviewComponent', ( ) => { {provide: PaginationService, useValue: paginationService}, { provide: NgbModal, useValue: { - open: () => { /*comment*/} + open: () => { /*comment*/ + } } }, {provide: ChangeDetectorRef, useValue: {}}, diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts index 12d196023e1..826b4b4154d 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.ts @@ -27,7 +27,7 @@ import {TranslateService} from '@ngx-translate/core'; /** * The `LdnServicesOverviewComponent` is a component that provides an overview of LDN (Linked Data Notifications) services. * It displays a paginated list of LDN services, allows users to edit and delete services, - * toggle the status of each service directly form the page and allows for creation of new services redirecting the user on the creation form + * toggle the status of each service directly form the page and allows for creation of new services redirecting the user on the creation/edit form */ @Component({ selector: 'ds-ldn-services-directory', @@ -52,14 +52,13 @@ export class LdnServicesOverviewComponent implements OnInit, OnDestroy { modalRef: any; - constructor( - protected ldnServicesService: LdnServicesService, - protected paginationService: PaginationService, - protected modalService: NgbModal, - public cdRef: ChangeDetectorRef, - private notificationService: NotificationsService, - private translateService: TranslateService, + protected ldnServicesService: LdnServicesService, + protected paginationService: PaginationService, + protected modalService: NgbModal, + public cdRef: ChangeDetectorRef, + private notificationService: NotificationsService, + private translateService: TranslateService, ) { } From aa08989827a4ee9c59e7f730c0ec6d2c6a24a234 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Mon, 20 Nov 2023 15:15:58 +0100 Subject: [PATCH 10/10] CST-12498 rollback --- .../ldn-service-form/ldn-service-form.component.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html index 650fd8d3be1..bc71eda994f 100644 --- a/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html +++ b/src/app/admin/admin-ldn-services/ldn-service-form/ldn-service-form.component.html @@ -178,7 +178,7 @@

{{ 'ldn-create-service.title' | translate }}

-
+
@@ -202,7 +202,7 @@

{{ 'ldn-create-service.title' | translate }}

-
+
@@ -301,7 +301,7 @@

{{ 'ldn-create-service.title' | translate }}

-
+