mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-16 22:43:03 +00:00
Fixes after merge with master
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import { async, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { cold, getTestScheduler } from 'jasmine-marbles';
|
||||
import { getTestScheduler } from 'jasmine-marbles';
|
||||
import { TestScheduler } from 'rxjs/testing';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
import { Store, StoreModule } from '@ngrx/store';
|
||||
|
||||
import { getMockRequestService } from '../../shared/mocks/mock-request.service';
|
||||
import { ResponseCacheService } from '../cache/response-cache.service';
|
||||
import { RequestService } from '../data/request.service';
|
||||
import { SubmissionPatchRequest } from '../data/request.models';
|
||||
import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service-stub';
|
||||
@@ -23,13 +22,13 @@ import {
|
||||
StartTransactionPatchOperationsAction
|
||||
} from './json-patch-operations.actions';
|
||||
import { MockStore } from '../../shared/testing/mock-store';
|
||||
import { RequestEntry } from '../data/request.reducer';
|
||||
|
||||
class TestService extends JsonPatchOperationsService<SubmitDataResponseDefinitionObject, SubmissionPatchRequest> {
|
||||
protected linkPath = '';
|
||||
protected patchRequestConstructor = SubmissionPatchRequest;
|
||||
|
||||
constructor(
|
||||
protected responseCache: ResponseCacheService,
|
||||
protected requestService: RequestService,
|
||||
protected store: Store<CoreState>,
|
||||
protected halService: HALEndpointService) {
|
||||
@@ -41,7 +40,6 @@ class TestService extends JsonPatchOperationsService<SubmitDataResponseDefinitio
|
||||
describe('JsonPatchOperationsService test suite', () => {
|
||||
let scheduler: TestScheduler;
|
||||
let service: TestService;
|
||||
let responseCache: ResponseCacheService;
|
||||
let requestService: RequestService;
|
||||
let rdbService: RemoteDataBuildService;
|
||||
let halService: any;
|
||||
@@ -84,18 +82,14 @@ describe('JsonPatchOperationsService test suite', () => {
|
||||
value: ['test']
|
||||
}];
|
||||
|
||||
function initMockResponseCacheService(isSuccessful: boolean): ResponseCacheService {
|
||||
return jasmine.createSpyObj('responseCache', {
|
||||
get: cold('c-', {
|
||||
c: {response: {isSuccessful},
|
||||
timeAdded: timestampResponse}
|
||||
})
|
||||
});
|
||||
}
|
||||
const getRequestEntry$ = (successful: boolean) => {
|
||||
return observableOf({
|
||||
response: { isSuccessful: successful, timeAdded: timestampResponse } as any
|
||||
} as RequestEntry)
|
||||
};
|
||||
|
||||
function initTestService(): TestService {
|
||||
return new TestService(
|
||||
responseCache,
|
||||
requestService,
|
||||
store,
|
||||
halService
|
||||
@@ -116,8 +110,7 @@ describe('JsonPatchOperationsService test suite', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
store = TestBed.get(Store);
|
||||
responseCache = initMockResponseCacheService(true);
|
||||
requestService = getMockRequestService();
|
||||
requestService = getMockRequestService(getRequestEntry$(true));
|
||||
rdbService = getMockRemoteDataBuildService();
|
||||
scheduler = getTestScheduler();
|
||||
halService = new HALEndpointServiceStub(resourceEndpointURL);
|
||||
@@ -146,7 +139,7 @@ describe('JsonPatchOperationsService test suite', () => {
|
||||
scheduler.schedule(() => service.jsonPatchByResourceType(resourceEndpoint, resourceScope, testJsonPatchResourceType).subscribe());
|
||||
scheduler.flush();
|
||||
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected, true);
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
|
||||
it('should dispatch a new StartTransactionPatchOperationsAction', () => {
|
||||
@@ -170,8 +163,7 @@ describe('JsonPatchOperationsService test suite', () => {
|
||||
describe('when request is not successful', () => {
|
||||
beforeEach(() => {
|
||||
store = TestBed.get(Store);
|
||||
responseCache = initMockResponseCacheService(false);
|
||||
requestService = getMockRequestService();
|
||||
requestService = getMockRequestService(getRequestEntry$(false));
|
||||
rdbService = getMockRemoteDataBuildService();
|
||||
scheduler = getTestScheduler();
|
||||
halService = new HALEndpointServiceStub(resourceEndpointURL);
|
||||
@@ -208,7 +200,7 @@ describe('JsonPatchOperationsService test suite', () => {
|
||||
scheduler.schedule(() => service.jsonPatchByResourceID(resourceEndpoint, resourceScope, testJsonPatchResourceType, testJsonPatchResourceId).subscribe());
|
||||
scheduler.flush();
|
||||
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected, true);
|
||||
expect(requestService.configure).toHaveBeenCalledWith(expected);
|
||||
});
|
||||
|
||||
it('should dispatch a new StartTransactionPatchOperationsAction', () => {
|
||||
@@ -232,8 +224,7 @@ describe('JsonPatchOperationsService test suite', () => {
|
||||
describe('when request is not successful', () => {
|
||||
beforeEach(() => {
|
||||
store = TestBed.get(Store);
|
||||
responseCache = initMockResponseCacheService(false);
|
||||
requestService = getMockRequestService();
|
||||
requestService = getMockRequestService(getRequestEntry$(false));
|
||||
rdbService = getMockRemoteDataBuildService();
|
||||
scheduler = getTestScheduler();
|
||||
halService = new HALEndpointServiceStub(resourceEndpointURL);
|
||||
|
@@ -1,12 +1,10 @@
|
||||
import { merge as observableMerge, Observable, of as observableOf, throwError as observableThrowError } from 'rxjs';
|
||||
import { distinctUntilChanged, filter, flatMap, map, mergeMap, partition, take, tap } from 'rxjs/operators';
|
||||
import { merge as observableMerge, Observable, of as observableOf } from 'rxjs';
|
||||
import { distinctUntilChanged, filter, find, flatMap, map, partition, take, tap } from 'rxjs/operators';
|
||||
import { Store } from '@ngrx/store';
|
||||
|
||||
import { hasValue, isEmpty, isNotEmpty, isNotUndefined, isUndefined } from '../../shared/empty.util';
|
||||
import { ErrorResponse, PostPatchSuccessResponse, RestResponse } from '../cache/response-cache.models';
|
||||
import { ResponseCacheEntry } from '../cache/response-cache.reducer';
|
||||
import { ResponseCacheService } from '../cache/response-cache.service';
|
||||
import { PatchRequest, RestRequest } from '../data/request.models';
|
||||
import { ErrorResponse, PostPatchSuccessResponse, RestResponse } from '../cache/response.models';
|
||||
import { PatchRequest } from '../data/request.models';
|
||||
import { RequestService } from '../data/request.service';
|
||||
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||
import { CoreState } from '../core.reducers';
|
||||
@@ -18,31 +16,20 @@ import {
|
||||
StartTransactionPatchOperationsAction
|
||||
} from './json-patch-operations.actions';
|
||||
import { JsonPatchOperationModel } from './json-patch.model';
|
||||
import { getResponseFromEntry } from '../shared/operators';
|
||||
import { ObjectCacheEntry } from '../cache/object-cache.reducer';
|
||||
|
||||
/**
|
||||
* An abstract class that provides methods to make JSON Patch requests.
|
||||
*/
|
||||
export abstract class JsonPatchOperationsService<ResponseDefinitionDomain, PatchRequestDefinition extends PatchRequest> {
|
||||
protected abstract responseCache: ResponseCacheService;
|
||||
|
||||
protected abstract requestService: RequestService;
|
||||
protected abstract store: Store<CoreState>;
|
||||
protected abstract linkPath: string;
|
||||
protected abstract halService: HALEndpointService;
|
||||
protected abstract patchRequestConstructor: any;
|
||||
|
||||
protected submitData(request: RestRequest): Observable<ResponseDefinitionDomain> {
|
||||
const responses = this.responseCache.get(request.href).pipe(map((entry: ResponseCacheEntry) => entry.response));
|
||||
const errorResponses = responses.pipe(
|
||||
filter((response) => !response.isSuccessful),
|
||||
mergeMap(() => observableThrowError(new Error(`Couldn't send data to server`)))
|
||||
);
|
||||
const successResponses = responses.pipe(
|
||||
filter((response: PostPatchSuccessResponse) => isNotEmpty(response)),
|
||||
map((response: PostPatchSuccessResponse) => response.dataDefinition)
|
||||
);
|
||||
return observableMerge(errorResponses, successResponses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit a new JSON Patch request with all operations stored in the state that are ready to be dispatched
|
||||
*
|
||||
@@ -56,6 +43,7 @@ export abstract class JsonPatchOperationsService<ResponseDefinitionDomain, Patch
|
||||
* observable of response
|
||||
*/
|
||||
protected submitJsonPatchOperations(hrefObs: Observable<string>, resourceType: string, resourceId?: string): Observable<ResponseDefinitionDomain> {
|
||||
const requestId = this.requestService.generateRequestId();
|
||||
let startTransactionTime = null;
|
||||
const [patchRequest$, emptyRequest$] = partition((request: PatchRequestDefinition) => isNotEmpty(request.body))(hrefObs.pipe(
|
||||
flatMap((endpointURL: string) => {
|
||||
@@ -84,7 +72,7 @@ export abstract class JsonPatchOperationsService<ResponseDefinitionDomain, Patch
|
||||
})
|
||||
}
|
||||
}
|
||||
return this.getRequestInstance(this.requestService.generateRequestId(), endpointURL, body);
|
||||
return this.getRequestInstance(requestId, endpointURL, body);
|
||||
}));
|
||||
})));
|
||||
|
||||
@@ -96,12 +84,12 @@ export abstract class JsonPatchOperationsService<ResponseDefinitionDomain, Patch
|
||||
patchRequest$.pipe(
|
||||
filter((request: PatchRequestDefinition) => isNotEmpty(request.body)),
|
||||
tap(() => this.store.dispatch(new StartTransactionPatchOperationsAction(resourceType, resourceId, startTransactionTime))),
|
||||
tap((request: PatchRequestDefinition) => this.requestService.configure(request, true)),
|
||||
flatMap((request: PatchRequestDefinition) => {
|
||||
const [successResponse$, errorResponse$] = partition((response: RestResponse) => response.isSuccessful)(this.responseCache.get(request.href).pipe(
|
||||
filter((entry: ResponseCacheEntry) => startTransactionTime < entry.timeAdded),
|
||||
take(1),
|
||||
map((entry: ResponseCacheEntry) => entry.response)
|
||||
tap((request: PatchRequestDefinition) => this.requestService.configure(request)),
|
||||
flatMap(() => {
|
||||
const [successResponse$, errorResponse$] = partition((response: RestResponse) => response.isSuccessful)(this.requestService.getByUUID(requestId).pipe(
|
||||
getResponseFromEntry(),
|
||||
find((entry: ObjectCacheEntry) => startTransactionTime < entry.timeAdded),
|
||||
map((entry: ObjectCacheEntry) => entry),
|
||||
));
|
||||
return observableMerge(
|
||||
errorResponse$.pipe(
|
||||
|
Reference in New Issue
Block a user