Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

fix: issue fixed to generate the correct function.json for event hub and service bus queue and topic triggers #228

Merged
merged 5 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test cases added
  • Loading branch information
Neeraj Mandal committed Aug 6, 2019
commit 75abfe606ca54e8b6584979a6cd1548f9f59d971
43 changes: 43 additions & 0 deletions src/services/packageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe("Package Service", () => {
sls.config.servicePath = process.cwd();
sls.service["functions"] = {
hello: MockFactory.createTestAzureFunctionConfig(functionRoute),
eventhubHandler: MockFactory.createTestEventHubFunctionConfig(),
}

packageService = new PackageService(sls, MockFactory.createTestServerlessOptions());
});

Expand Down Expand Up @@ -119,6 +121,47 @@ describe("Package Service", () => {
writeFileSpy.mockRestore();
});

it("Creates Event Hub Handler bindings and function.json ", async () => {
const functionName = "eventhubHandler";
const functionMetadata: FunctionMetadata = {
entryPoint: "handler",
handlerPath: "src/handlers/eventhubHandler",
params: {
functionsJson: {
bindings: [
MockFactory.createTestEventHubBinding("in"),
]
}
},
};

const expectedFolderPath = path.join(sls.config.servicePath, functionName);
const expectedFilePath = path.join(expectedFolderPath, "function.json");
const expectedFunctionJson = {
entryPoint: "handler",
scriptFile: "src/handlers/eventhubHandler",
bindings: [
MockFactory.createTestEventHubBinding("in"),
]

}

mockFs({});

const mkdirSpy = jest.spyOn(fs, "mkdirSync");
const writeFileSpy = jest.spyOn(fs, "writeFileSync");

await packageService.createBinding(functionName, functionMetadata);

expect(mkdirSpy).toBeCalledWith(expectedFolderPath);
const call = writeFileSpy.mock.calls[0];
expect(call[0]).toEqual(expectedFilePath);
expect(JSON.parse(call[1])).toEqual(expectedFunctionJson);

mkdirSpy.mockRestore();
writeFileSpy.mockRestore();
});

it("createBinding does not need to create directory if function folder already exists", async () => {
const functionName = "hello";
const functionMetadata: FunctionMetadata = {
Expand Down
22 changes: 22 additions & 0 deletions src/test/mockFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ export class MockFactory {
}
}

public static createTestEventHubFunctionConfig(): ServerlessAzureFunctionConfig {
return {
events: [
{
http: true,
"x-azure-settings": MockFactory.createTestEventHubBinding("in"),
}
],
handler: "handler.js",
}
}

public static createTestBinding() {
// Only supporting HTTP for now, could support others
return MockFactory.createTestHttpBinding();
Expand All @@ -433,6 +445,16 @@ export class MockFactory {
}
}

public static createTestEventHubBinding(direction: string = "in") {
return {
event: "eventHubTrigger",
direction,
name: "item",
eventhubname: "hello",
consumerGroup: "$Default",
connection: "EventHubsConnection"
}
}
public static createTestBindingsObject(name: string = "index.js") {
return {
scriptFile: name,
Expand Down