Skip to content

Commit

Permalink
refactor context file (#74)
Browse files Browse the repository at this point in the history
* refactor context file

* refactor context files

* fix(MAGNETO-7655): Linter quick fixes

* fix(MAGNETO-7655): Linter fix issue

* chore: golangci configure analysis on new code

* chore: exclude coverage from context.go files

Co-authored-by: Jordi Puig Bou <jordi.puigbou@telefonica.com>
Co-authored-by: Ricardo García Fernández <ricardogarfe@gmail.com>
  • Loading branch information
3 people authored Mar 15, 2022
1 parent 88960cf commit 2403aae
Show file tree
Hide file tree
Showing 18 changed files with 35 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ debug.test

# Coverage
coverage.*

# Linter
golangci-report.xml
2 changes: 1 addition & 1 deletion cfg/default.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package cfg

// DefaultConfig is the default configuration for golium.
// This default configuration can be overriden with environment variables.
// This default configuration can be overridden with environment variables.
var DefaultConfig = Config{
Suite: "golium",
Environment: "local",
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ContextKey defines a type to store the Context in context.Context.
type ContextKey string

var contextKey ContextKey = "contextKey"
const contextKey ContextKey = "contextKey"

// Context contains the context required for common utilities.
// It contains a map[string]interface{} to store global values and find them with [CTXT:xxx] tag.
Expand Down
2 changes: 1 addition & 1 deletion mock/http/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package http

import "encoding/json"

// MockRequest contains the instruction to configure the behaviour of the HTTP mock server.
// MockRequest contains the instruction to configure the behavior of the HTTP mock server.
// The document configures which request is going to be attended (e.g. the path and method)
// and the response to be generated by the mock.
type MockRequest struct {
Expand Down
1 change: 1 addition & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sonar.organization=telefonicatc2tech

sonar.go.coverage.reportPaths=coverage.txt
sonar.go.golangci-lint.reportPaths=golangci-report.xml
sonar.coverage.exclusions=**/*context.go

sonar.sources=.
sonar.exclusions=**/*_test.go
Expand Down
5 changes: 2 additions & 3 deletions steps/dns/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the Context in context.Context.
type ContextKey string

var contextKey ContextKey = "dnsSession"
const contextKey ContextKey = "dnsSession"

// InitializeContext adds the DNS Session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
session := Session{}
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the DNS session stored in context.
Expand Down
4 changes: 2 additions & 2 deletions steps/dns/session.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *Session) SendUDPQuery(ctx context.Context, qtype uint16, qdomain string
func (s *Session) SendDoHQuery(ctx context.Context, method string, qtype uint16, qdomain string, recursive bool) error {
logger := GetLogger()
corr := uuid.New().String()
//Set DNS query
// Set DNS query
m := &dns.Msg{}
m.SetQuestion(dns.Fqdn(qdomain), qtype)
m.RecursionDesired = recursive
Expand Down Expand Up @@ -177,7 +177,7 @@ func (s *Session) SendDoHQuery(ctx context.Context, method string, qtype uint16,
func (s *Session) SendDoTQuery(ctx context.Context, qtype uint16, qdomain string, recursive bool) error {
logger := GetLogger()
corr := uuid.New().String()
//Set DNS query
// Set DNS query
opts := upstream.Options{
Timeout: s.Timeout,
InsecureSkipVerify: true,
Expand Down
5 changes: 2 additions & 3 deletions steps/elasticsearch/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the elasticsearch session in context.Context.
type ContextKey string

var contextKey ContextKey = "elasticsearchSession"
const contextKey ContextKey = "elasticsearchSession"

// InitializeContext adds the elasticsearch session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
var session Session
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the elasticsearch session stored in context.
Expand Down
5 changes: 2 additions & 3 deletions steps/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the HTTP session in context.Context.
type ContextKey string

var contextKey ContextKey = "httpSession"
const contextKey ContextKey = "httpSession"

// InitializeContext adds the HTTP session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
var session Session
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the HTTP session stored in context.
Expand Down
20 changes: 8 additions & 12 deletions steps/http/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -15,7 +16,6 @@ import (

func TestGetParamFromJSON(t *testing.T) {
t.Run("Should return selected value from JSON file", func(t *testing.T) {

var JSONhttpFileValues = `
[
{
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestGetParamFromJSON(t *testing.T) {
]
}`
var expectedParam interface{}
if err := json.Unmarshal([]byte(fmt.Sprint(JSON)), &expectedParam); err != nil {
if err := json.Unmarshal([]byte(JSON), &expectedParam); err != nil {
t.Error("error Unmarshaling expected response body: %w", err)
}

Expand Down Expand Up @@ -114,12 +114,12 @@ func TestFindValueByCode(t *testing.T) {
]
}`
var expectedValue interface{}
if err := json.Unmarshal([]byte(fmt.Sprint(JSON)), &expectedValue); err != nil {
if err := json.Unmarshal([]byte(JSON), &expectedValue); err != nil {
t.Error("error Unmarshaling expected response body: %w", err)
}

dataStruct := []map[string]interface{}{}
if err := json.Unmarshal([]byte(fmt.Sprint(JSONhttpFileValues)), &dataStruct); err != nil {
if err := json.Unmarshal([]byte(JSONhttpFileValues), &dataStruct); err != nil {
t.Error("error Unmarshaling expected response body: %w", err)
}

Expand Down Expand Up @@ -162,7 +162,6 @@ func TestFindValueByCode(t *testing.T) {
}
})
}

}

func TestLoadJSONData(t *testing.T) {
Expand Down Expand Up @@ -201,7 +200,7 @@ func TestUnmarshalJSONData(t *testing.T) {
}`

var message = "error unmarshalling JSON data due to error: json: cannot unmarshal object into Go value of type []map[string]interface {}"
formatError := fmt.Errorf(message)
formatError := errors.New(message)
var expected interface{}
if err := json.Unmarshal([]byte(expectedString), &expected); err != nil {
t.Error("error Unmarshaling expected response body: %w", err)
Expand Down Expand Up @@ -237,7 +236,6 @@ func TestUnmarshalJSONData(t *testing.T) {
if JSONEquals(tc.expected, unmarshalled) {
t.Errorf("expected unmarshalled data error:\n%v", err)
}

})
}
}
Expand Down Expand Up @@ -273,17 +271,17 @@ func TestJSONEquals(t *testing.T) {
]
}`
var expected interface{}
if err := json.Unmarshal([]byte(fmt.Sprint(expectedString)), &expected); err != nil {
if err := json.Unmarshal([]byte(expectedString), &expected); err != nil {
t.Error("error Unmarshaling expected response body: %w", err)
}

var different interface{}
if err := json.Unmarshal([]byte(fmt.Sprint(differentString)), &different); err != nil {
if err := json.Unmarshal([]byte(differentString), &different); err != nil {
t.Error("error Unmarshaling expected response body: %w", err)
}

var current interface{}
if err := json.Unmarshal([]byte(fmt.Sprint(currentString)), &current); err != nil {
if err := json.Unmarshal([]byte(currentString), &current); err != nil {
t.Error("error Unmarshaling expected response body: %w", err)
}

Expand Down Expand Up @@ -312,8 +310,6 @@ func TestJSONEquals(t *testing.T) {
t.Errorf("expected JSON comparison should be %t \n%v\n vs \n%v", tc.equals, tc.expected,
tc.current)
}

})
}

}
5 changes: 2 additions & 3 deletions steps/jwt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the JWT session in context.Context.
type ContextKey string

var contextKey ContextKey = "jwtSession"
const contextKey ContextKey = "jwtSession"

// InitializeContext adds the JWT session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
var session Session
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the HTTP session stored in context.
Expand Down
5 changes: 2 additions & 3 deletions steps/rabbit/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the rabbit session in context.Context.
type ContextKey string

var contextKey ContextKey = "rabbitSession"
const contextKey ContextKey = "rabbitSession"

// InitializeContext adds the rabbit session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
var session Session
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the rabbit session stored in context.
Expand Down
2 changes: 1 addition & 1 deletion steps/rabbit/session.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (s *Session) SubscribeTopic(ctx context.Context, topic string) error {
return nil
}

// Unsubscribe unsubscribes from rabbit closing the channel asociated.
// Unsubscribe unsubscribes from rabbit closing the channel associated.
// If this method is not invoked, then the goroutine created with SubscribeTopic is never closed
// and will permanently processing messages from the topic until the program is finished.
func (s *Session) Unsubscribe(ctx context.Context) error {
Expand Down
5 changes: 2 additions & 3 deletions steps/redis/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the redis session in context.Context.
type ContextKey string

var contextKey ContextKey = "redisSession"
const contextKey ContextKey = "redisSession"

// InitializeContext adds the redis session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
var session Session
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the redis session stored in context.
Expand Down
5 changes: 2 additions & 3 deletions steps/s3/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the s3 session in context.Context.
type ContextKey string

var contextKey ContextKey = "s3Session"
const contextKey ContextKey = "s3Session"

// InitializeContext adds the s3 session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
var session Session
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the s3 session stored in context.
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/steps/aggregated/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// ContextKey defines a type to store the aggregated session in context.Context.
type ContextKey string

var contextKey ContextKey = "aggregatedSession"
const contextKey ContextKey = "aggregatedSession"

// InitializeContext adds the Aggregated session to the context.
// The new context is returned because context is immutable.
Expand Down
1 change: 0 additions & 1 deletion test/acceptance/steps/aggregated/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func TestInitializeContext(t *testing.T) {

var ctx = context.Background()
var contextKeyValue = "aggregatedSession"
sharedContext := shared.InitializeContext(ctx)
Expand Down
5 changes: 2 additions & 3 deletions test/acceptance/steps/shared/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
// ContextKey defines a type to store the shared session in context.Context.
type ContextKey string

var contextKey ContextKey = "sharedSession"
const contextKey ContextKey = "sharedSession"

// InitializeContext adds the shared session to the context.
// The new context is returned because context is immutable.
func InitializeContext(ctx context.Context) context.Context {
var session Session
return context.WithValue(ctx, contextKey, &session)
return context.WithValue(ctx, contextKey, &Session{})
}

// GetSession returns the shared session stored in context.
Expand Down

0 comments on commit 2403aae

Please sign in to comment.