Skip to content

Commit

Permalink
Refactor: File service call api directly(no sdk) (#1462)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Jun 22, 2018
1 parent 60f8fda commit d481282
Show file tree
Hide file tree
Showing 13 changed files with 505 additions and 416 deletions.
8 changes: 4 additions & 4 deletions app/models/file-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Model, Prop, Record } from "@batch-flask/core";
export interface FilePropertiesAttributes {
contentLength: number;
contentType: string;
creationTime: Date;
lastModified: Date;
creationTime: Date | string;
lastModified: Date | string;
}

/**
Expand All @@ -14,6 +14,6 @@ export interface FilePropertiesAttributes {
export class FileProperties extends Record<FilePropertiesAttributes> {
@Prop() public contentLength: number;
@Prop() public contentType: string;
@Prop() public creationTime: Date;
@Prop() public lastModified: Date;
@Prop(Date) public creationTime: Date;
@Prop(Date) public lastModified: Date;
}
16 changes: 14 additions & 2 deletions app/services/azure-batch/core/batch-entity-getter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { HttpResponse } from "@angular/common/http";
import { Type } from "@angular/core";

import { EntityGetter, EntityGetterConfig } from "app/services/core/data/entity-getter";
import { Observable } from "rxjs";
import { map, share } from "rxjs/operators";
import { AzureBatchHttpService } from "./batch-http.service";

export interface BatchEntityGetterConfig<TEntity, TParams> extends EntityGetterConfig<TEntity, TParams> {
/**
* Get function(usually call the client proxy)
*/
uri: string | ((params: TParams) => string);

processResponse?: (response: HttpResponse<any>, params: TParams) => any;
}
export class BatchEntityGetter<TEntity, TParams> extends EntityGetter<TEntity, TParams> {
private _provideUri: string | ((params: TParams) => string);
private _processResponse: (response: HttpResponse<any>, params: TParams) => any;

constructor(
type: Type<TEntity>,
Expand All @@ -20,10 +24,18 @@ export class BatchEntityGetter<TEntity, TParams> extends EntityGetter<TEntity, T

super(type, config);
this._provideUri = config.uri;
this._processResponse = config.processResponse;
}

protected getData(params: TParams): Observable<any> {
return this.http.get<TEntity>(this._computeURI(params));
const uri = this._computeURI(params);
if (this._processResponse) {
return this.http.get(uri, { observe: "response" }).pipe(
map(x => this._processResponse(x, params)),
share(),
);
}
return this.http.get<TEntity>(uri);
}

private _computeURI(params: TParams): string {
Expand Down
4 changes: 2 additions & 2 deletions app/services/azure-batch/core/batch-list-getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export class BatchListGetter<TEntity, TParams> extends ListGetter<TEntity, TPara
return this.http.get<any>(token.nextLink).map(x => this._processResponse(x)).share();
}

private _processResponse(response: { value: TEntity[], "@odata.nextLink": string }) {
private _processResponse(response: { value: TEntity[], "odata.nextLink": string }) {
const data = response.value;

return {
data,
nextLink: response["@odata.nextLink"],
nextLink: response["odata.nextLink"],
};
}

Expand Down
139 changes: 139 additions & 0 deletions app/services/azure-batch/file/file.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { HttpClient } from "@angular/common/http";
import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing";
import { TestBed } from "@angular/core/testing";
import { File } from "app/models";
import { FileService } from "./file.service";

const creationTime = new Date(2018, 4, 20);
const lastModified = new Date(2018, 6, 24);

describe("FileService", () => {
let fileService: FileService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
],
providers: [
FileService,
],
});
fileService = new FileService(TestBed.get(HttpClient), null);
httpMock = TestBed.get(HttpTestingController);
});

it("get a node file", (done) => {

fileService.getFilePropertiesFromComputeNode("pool-1", "node-2", "file-1.txt").subscribe((file: File) => {
expect(file instanceof File).toBe(true);
expect(file.name).toEqual("file-1.txt");
expect(file.isDirectory).toBe(false);
expect(file.properties.contentLength).toEqual(260);
expect(file.properties.contentType).toEqual("application/octet-stream");
expect(file.properties.creationTime).toEqual(creationTime);
expect(file.properties.lastModified).toEqual(lastModified);
done();
});

const req = httpMock.expectOne({
url: "/pools/pool-1/nodes/node-2/files/file-1.txt",
method: "HEAD",
});
expect(req.request.body).toBe(null);
req.flush(null, {
headers: {
"ocp-batch-file-isdirectory": "False",
"ocp-batch-file-url": "/pools/pool-1/nodes/node-2/files/file-1.txt",
"content-length": "260",
"ocp-creation-time": creationTime.toString(),
"last-modified": lastModified.toString(),
"content-type": "application/octet-stream",
},
});
httpMock.verify();
});
describe("listFiles", () => {
function expectList() {
return httpMock.expectOne(req => req.method === "GET" && req.url === "/pools/pool-1/nodes/node-2/files");
}

it("list file non recursively", (done) => {
fileService.listFromNode("pool-1", "node-2").subscribe((response) => {
const files = response.items;
expect(files.size).toBe(2);
const dir = files.get(0);
expect(dir instanceof File).toBe(true);
expect(dir.name).toBe("shared");
expect(dir.isDirectory).toBe(true);

const file = files.get(1);
expect(file instanceof File).toBe(true);
expect(file.name).toEqual("startup/ProcessEnv.cmd");
expect(file.isDirectory).toBe(false);
expect(file.properties.contentLength).toEqual(1813);
expect(file.properties.contentType).toEqual("application/octet-stream");
expect(file.properties.creationTime).toEqual(creationTime);
expect(file.properties.lastModified).toEqual(lastModified);
done();
});

const req = expectList();
expect(req.request.params.get("recursive").toString()).toBe("false");
expect(req.request.params.has("$filter")).toBe(false);
expect(req.request.body).toBe(null);
req.flush({
value: [{
name: "shared",
url: "/pools/pool-1/nodes/node-2/files/shared",
isDirectory: true,
},
{
name: "startup/ProcessEnv.cmd",
url: "/pools/pool-1/nodes/node-2/files/startup/ProcessEnv.cmd",
isDirectory: false,
properties: {
creationTime: creationTime.toString(),
lastModified: lastModified.toString(),
contentLength: 1813,
contentType: "application/octet-stream",
},
}],
});
httpMock.verify();
});

it("list file recursively", (done) => {
const options = {
recursive: true,
};
fileService.listFromNode("pool-1", "node-2", options).subscribe((response) => {
done();
});

const req = expectList();
expect(req.request.params.get("recursive").toString()).toBe("true");
expect(req.request.params.has("$filter")).toBe(false);
expect(req.request.body).toBe(null);
req.flush({ value: []});
httpMock.verify();
});

it("list file with folder", (done) => {
const options = {
recursive: true,
folder: "abc/",
};
fileService.listFromNode("pool-1", "node-2", options).subscribe((response) => {
done();
});

const req = expectList();
expect(req.request.params.get("recursive").toString()).toBe("true");
expect(req.request.params.get("$filter")).toBe("startswith(name, 'abc/')");
expect(req.request.body).toBe(null);
req.flush({ value: []});
httpMock.verify();
});
});
});
Loading

0 comments on commit d481282

Please sign in to comment.