-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #13 Add support for Asynchronous API mocking and testing
Signed-off-by: Laurent Broudoux <[email protected]>
- Loading branch information
Showing
7 changed files
with
408 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,142 @@ | ||
/* | ||
* Copyright The Microcks Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { AbstractStartedContainer, GenericContainer, StartedNetwork, StartedTestContainer, Wait, getContainerRuntimeClient } from "testcontainers"; | ||
import { MicrocksContainer } from "./microcks-container"; | ||
|
||
export class MicrocksAsyncMinionContainer extends GenericContainer { | ||
static readonly MICROCKS_ASYNC_MINION_HTTP_PORT = 8081; | ||
|
||
private network: StartedNetwork; | ||
private extraProtocols: string = ""; | ||
|
||
constructor(network: StartedNetwork, image = "quay.io/microcks/microcks-uber-async-minion:nightly") { | ||
super(image); | ||
this.network = network; | ||
} | ||
|
||
/** | ||
* Connect the MicrocksAsyncMinionContainer to a Kafka server to allow Kafka messages mocking. | ||
* @param {KafkaConnection} connection Connection details to a Kafka broker. | ||
* @returns this | ||
*/ | ||
public withKafkaConnection(connection: KafkaConnection): this { | ||
this.addProtocolIfNeeded('KAFKA'); | ||
this.withEnvironment({ | ||
ASYNC_PROTOCOLS: this.extraProtocols, | ||
KAFKA_BOOTSTRAP_SERVER: connection.bootstrapServers | ||
}); | ||
return this; | ||
} | ||
|
||
/** | ||
* Connect the MicrocksAsyncMinionContainer to an Amazon SQS service to allow SQS messages mocking. | ||
* @param {AmazonServiceConnection} connection Connection details to an Amazon SQS service. | ||
* @returns this | ||
*/ | ||
public withAmazonSQSConnection(connection: AmazonServiceConnection): this { | ||
this.addProtocolIfNeeded('SQS'); | ||
this.withEnvironment({ | ||
ASYNC_PROTOCOLS: this.extraProtocols, | ||
AWS_SQS_REGION: connection.region, | ||
AWS_ACCESS_KEY_ID: connection.accessKey, | ||
AWS_SECRET_ACCESS_KEY: connection.secretKey | ||
}); | ||
if (connection.endpointOverride != undefined) { | ||
this.withEnvironment({ | ||
AWS_SQS_ENDPOINT: connection.endpointOverride | ||
}); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Connect the MicrocksAsyncMinionContainer to an Amazon SNS service to allow SQS messages mocking. | ||
* @param {AmazonServiceConnection} connection Connection details to an Amazon SQS service. | ||
* @returns this | ||
*/ | ||
public withAmazonSNSConnection(connection: AmazonServiceConnection): this { | ||
this.addProtocolIfNeeded('SNS'); | ||
this.withEnvironment({ | ||
ASYNC_PROTOCOLS: this.extraProtocols, | ||
AWS_SNS_REGION: connection.region, | ||
AWS_ACCESS_KEY_ID: connection.accessKey, | ||
AWS_SECRET_ACCESS_KEY: connection.secretKey | ||
}); | ||
if (connection.endpointOverride != undefined) { | ||
this.withEnvironment({ | ||
AWS_SNS_ENDPOINT: connection.endpointOverride | ||
}); | ||
} | ||
return this; | ||
} | ||
|
||
public override async start(): Promise<StartedMicrocksAsyncMinionContainer> { | ||
this.withNetwork(this.network) | ||
.withNetworkAliases("microcks-async-minion") | ||
.withEnvironment({ | ||
MICROCKS_HOST_PORT: "microcks:" + MicrocksContainer.MICROCKS_HTTP_PORT | ||
}) | ||
.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [MicrocksAsyncMinionContainer.MICROCKS_ASYNC_MINION_HTTP_PORT])) | ||
.withWaitStrategy(Wait.forLogMessage(/.*Profile prod activated\..*/, 1)); | ||
|
||
return new StartedMicrocksAsyncMinionContainer(await super.start()); | ||
} | ||
|
||
private addProtocolIfNeeded(protocol: string): void { | ||
if (this.extraProtocols.indexOf(',' + protocol) == -1) { | ||
this.extraProtocols += ',' + protocol; | ||
} | ||
} | ||
} | ||
|
||
export class StartedMicrocksAsyncMinionContainer extends AbstractStartedContainer { | ||
|
||
constructor( | ||
startedTestContainer: StartedTestContainer | ||
) { | ||
super(startedTestContainer); | ||
} | ||
|
||
/** | ||
* Get the exposed mock endpoints for a WebSocket Service. | ||
* @param {String} service The name of Service/API | ||
* @param {String} version The version of Service/API | ||
* @param {String} operationName The name of operation to get the endpoint for | ||
* @returns A usable endpoint to interact with Microcks mocks. | ||
*/ | ||
public getWSMockEndpoint(service: string, version: string, operationName: string): string { | ||
// operationName may start with SUBSCRIBE or PUBLISH. | ||
if (operationName.indexOf(' ') != -1) { | ||
operationName = operationName.split(' ')[1]; | ||
} | ||
let endpoint = `ws://${this.getHost()}:${this.getMappedPort(MicrocksAsyncMinionContainer.MICROCKS_ASYNC_MINION_HTTP_PORT)}/api`; | ||
endpoint += `/ws/${service.replace(/\s/g, '+')}/${version.replace(/\s/g, '+')}/${operationName}`; | ||
return endpoint; | ||
} | ||
} | ||
|
||
|
||
export interface KafkaConnection { | ||
bootstrapServers: string; | ||
} | ||
|
||
export interface AmazonServiceConnection { | ||
region: string; | ||
endpointOverride?: string; | ||
accessKey: string; | ||
secretKey: string; | ||
} |
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
Oops, something went wrong.