Skip to content

Commit

Permalink
feat: #88 Adding MQTT support for mocking
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Broudoux <laurent.broudoux@gmail.com>
  • Loading branch information
lbroudoux committed Jan 6, 2025
1 parent 2738e6c commit 530b2f9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ensemble/async/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"microcks.io/testcontainers-go/ensemble/async/connection/generic"
"microcks.io/testcontainers-go/ensemble/async/connection/kafka"
)

Expand Down Expand Up @@ -136,6 +137,21 @@ func WithKafkaConnection(connection kafka.Connection) testcontainers.CustomizeRe
}
}

// WithKafkaConnection connects the MicrocksAsyncMinionContainer to a MQTT broker to allow MQTT messages mocking.
func WithMQTTConnection(connection generic.Connection) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
if req.Env == nil {
req.Env = make(map[string]string)
}
req.Env["MQTT_SERVER"] = connection.Server
req.Env["MQTT_USERNAME"] = connection.Username
req.Env["MQTT_PASSWORD"] = connection.Password
addProtocol(req, "MQTT")

return nil
}
}

// WSMockEndpoint gets the exposed mock endpoints for a WebSocket Service.
func (container *MicrocksAsyncMinionContainer) WSMockEndpoint(ctx context.Context, service, version, operationName string) (string, error) {
// Get the container host.
Expand Down
26 changes: 26 additions & 0 deletions ensemble/async/connection/generic/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/
package generic

// Connection represents generic message broker connection
type Connection struct {
// Server represents the hostname + port address.
Server string
// Username for connecting to remote broker.
Username string
// Password for connecting to remote broker.
Password string
}
9 changes: 9 additions & 0 deletions ensemble/ensemble.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"microcks.io/go-client"
microcks "microcks.io/testcontainers-go"
"microcks.io/testcontainers-go/ensemble/async"
"microcks.io/testcontainers-go/ensemble/async/connection/generic"
"microcks.io/testcontainers-go/ensemble/async/connection/kafka"
"microcks.io/testcontainers-go/ensemble/postman"
)
Expand Down Expand Up @@ -284,6 +285,14 @@ func WithKafkaConnection(connection kafka.Connection) Option {
}
}

// WithMQTTConnection configures a connection to a MQTT Broker.
func WithMQTTConnection(connection generic.Connection) Option {
return func(e *MicrocksContainersEnsemble) error {
e.asyncMinionContainerOptions.Add(async.WithMQTTConnection(connection))
return nil
}
}

// WithSecret creates a new secret.
func WithSecret(s client.Secret) Option {
return func(e *MicrocksContainersEnsemble) error {
Expand Down

0 comments on commit 530b2f9

Please sign in to comment.