-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from pagopa/add_support_for_native_event_hub_lib
[#IOPLT-363] Add Support for native Event Hub Library
- Loading branch information
Showing
9 changed files
with
344 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { pipe } from "fp-ts/lib/function"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import * as S from "../eventhub/service"; | ||
import { QueueType, getInternalQueueService } from "../factory"; | ||
|
||
const spiedCreateKafkaService = jest.spyOn(S, "createKafkaService"); | ||
const spiedCreateNativeEventHubService = jest.spyOn( | ||
S, | ||
"createNativeEventHubService", | ||
); | ||
|
||
const aConnectionString = | ||
"Endpoint=sb://foo.windows.net/;SharedAccessKeyName=foo;SharedAccessKey=SharedAccessKey=;EntityPath=foo"; | ||
const failTest = (msg: string) => { | ||
throw Error(msg); | ||
}; | ||
describe("getInternalQueueService", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}) | ||
it("should return Error if factory cannot get QueueService", () => { | ||
spiedCreateNativeEventHubService.mockImplementationOnce(() => | ||
E.left(Error("Cannot reach EventHub")), | ||
); | ||
pipe( | ||
getInternalQueueService(aConnectionString), | ||
E.mapLeft((e) => { | ||
expect(e).toBeDefined(); | ||
expect(e.message).toEqual("Cannot reach EventHub"); | ||
}), | ||
E.map(() => | ||
failTest( | ||
"Cannot instantiate queue service with wrong connection string", | ||
), | ||
), | ||
); | ||
}); | ||
|
||
it("should create native EventHub QueueService if no QueueType is provided", () => { | ||
spiedCreateNativeEventHubService.mockImplementationOnce(() => | ||
E.right({} as any), | ||
); | ||
pipe( | ||
getInternalQueueService(aConnectionString), | ||
E.mapLeft(() => failTest( | ||
"Should not fail", | ||
)), | ||
E.map(service =>{ | ||
expect(service).toBeDefined(); | ||
expect(spiedCreateNativeEventHubService).toHaveBeenCalled(); | ||
expect(spiedCreateNativeEventHubService).toHaveBeenCalledWith(aConnectionString); | ||
} | ||
), | ||
); | ||
}); | ||
it("should create Kafka QueueService if QueueType is defined as Kafka", () => { | ||
spiedCreateNativeEventHubService.mockImplementationOnce(() => | ||
E.right({} as any), | ||
); | ||
spiedCreateKafkaService.mockImplementationOnce(() => | ||
E.right({} as any), | ||
); | ||
pipe( | ||
getInternalQueueService(aConnectionString, QueueType.Kafka), | ||
E.mapLeft(() => failTest( | ||
"Should not fail", | ||
)), | ||
E.map(service =>{ | ||
expect(service).toBeDefined(); | ||
expect(spiedCreateNativeEventHubService).not.toHaveBeenCalled(); | ||
expect(spiedCreateKafkaService).toHaveBeenCalled(); | ||
expect(spiedCreateKafkaService).toHaveBeenCalledWith(aConnectionString); | ||
} | ||
), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as TE from "fp-ts/lib/TaskEither"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import * as O from "fp-ts/lib/Option"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { | ||
createKafkaService, | ||
createNativeEventHubService, | ||
} from "./eventhub/service"; | ||
|
||
export interface IQueueService { | ||
readonly produce: <T>( | ||
messages: ReadonlyArray<T>, | ||
) => TE.TaskEither<Error, void>; | ||
} | ||
export enum QueueType { | ||
EventHub, | ||
Kafka, | ||
} | ||
|
||
export const notSupportedError = "Queue type still not supported"; | ||
|
||
export const createInternalQueueService = ( | ||
type: QueueType, | ||
connectionString: string, | ||
): E.Either<Error, IQueueService> => { | ||
switch (type) { | ||
case QueueType.Kafka: | ||
return createKafkaService(connectionString); | ||
case QueueType.EventHub: | ||
return createNativeEventHubService(connectionString); | ||
default: | ||
E.left(new Error(notSupportedError)); | ||
} | ||
}; | ||
|
||
export const getInternalQueueService = ( | ||
connectionString: string, | ||
queueType?: QueueType, | ||
): E.Either<Error, IQueueService> => | ||
pipe( | ||
queueType, | ||
O.fromNullable, | ||
O.map((type) => createInternalQueueService(type, connectionString)), | ||
O.getOrElse(() => createNativeEventHubService(connectionString)), | ||
); |
Oops, something went wrong.