Skip to content

Commit fe9236e

Browse files
committed
Refactor file names and remove unused code
1 parent 0551533 commit fe9236e

File tree

7 files changed

+208
-34
lines changed

7 files changed

+208
-34
lines changed

httpclient/httpclient_api_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// api_handler.go
1+
// httpclient_api_handler.go
22
package httpclient
33

44
import (
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// httpclient_api_handler.go
2+
package httpclient
3+
4+
import (
5+
"errors"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/deploymenttheory/go-api-http-client/apihandlers/graph"
10+
"github.com/deploymenttheory/go-api-http-client/apihandlers/jamfpro"
11+
"github.com/deploymenttheory/go-api-http-client/logger"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/mock"
14+
)
15+
16+
// NewMockAPIHandler creates a new instance of MockAPIHandler.
17+
func NewMockAPIHandler() *MockAPIHandler {
18+
return &MockAPIHandler{}
19+
}
20+
21+
// Implement each method of the APIHandler interface on MockAPIHandler.
22+
23+
func (m *MockAPIHandler) ConstructAPIResourceEndpoint(instanceName string, endpointPath string, log logger.Logger) string {
24+
args := m.Called(instanceName, endpointPath, log)
25+
return args.String(0)
26+
}
27+
28+
func (m *MockAPIHandler) ConstructAPIAuthEndpoint(instanceName string, endpointPath string, log logger.Logger) string {
29+
args := m.Called(instanceName, endpointPath, log)
30+
return args.String(0)
31+
}
32+
33+
func (m *MockAPIHandler) MarshalRequest(body interface{}, method string, endpoint string, log logger.Logger) ([]byte, error) {
34+
args := m.Called(body, method, endpoint, log)
35+
return args.Get(0).([]byte), args.Error(1)
36+
}
37+
38+
func (m *MockAPIHandler) MarshalMultipartRequest(fields map[string]string, files map[string]string, log logger.Logger) ([]byte, string, error) {
39+
args := m.Called(fields, files, log)
40+
return args.Get(0).([]byte), args.String(1), args.Error(2)
41+
}
42+
43+
func (m *MockAPIHandler) HandleAPISuccessResponse(resp *http.Response, out interface{}, log logger.Logger) error {
44+
args := m.Called(resp, out, log)
45+
return args.Error(0)
46+
}
47+
48+
func (m *MockAPIHandler) HandleAPIErrorResponse(resp *http.Response, out interface{}, log logger.Logger) error {
49+
args := m.Called(resp, out, log)
50+
return args.Error(0)
51+
}
52+
53+
func TestConstructAPIResourceEndpoint(t *testing.T) {
54+
mockLogger := new(logger.MockLogger)
55+
mockAPIHandler := NewMockAPIHandler()
56+
instanceName := "testInstance"
57+
endpointPath := "/testEndpoint"
58+
59+
expectedEndpoint := "https://testInstance.apiendpoint.com/testEndpoint"
60+
61+
// Setup expectations
62+
mockAPIHandler.On("ConstructAPIResourceEndpoint", instanceName, endpointPath, mockLogger).Return(expectedEndpoint)
63+
64+
// Call the method
65+
endpoint := mockAPIHandler.ConstructAPIResourceEndpoint(instanceName, endpointPath, mockLogger)
66+
67+
// Assertions
68+
assert.Equal(t, expectedEndpoint, endpoint, "The constructed API resource endpoint did not match the expected value")
69+
70+
// Verify that the expectations were met
71+
mockAPIHandler.AssertExpectations(t)
72+
}
73+
74+
// TestLoadAPIHandler verifies the functionality of the LoadAPIHandler function in the httpclient package.
75+
// This function is designed to return the appropriate APIHandler implementation based on the provided apiType argument.
76+
// The test cases cover the following scenarios:
77+
// 1. Loading a JamfPro API handler by providing "jamfpro" as the apiType.
78+
// 2. Loading a Graph API handler by providing "graph" as the apiType.
79+
// 3. Handling an unsupported API type by providing an unknown apiType, which should result in an error.
80+
func TestLoadAPIHandler(t *testing.T) {
81+
mockLogger := new(MockLogger)
82+
tests := []struct {
83+
name string
84+
apiType string
85+
wantType interface{}
86+
wantErr bool
87+
}{
88+
{"Load JamfPro Handler", "jamfpro", &jamfpro.JamfAPIHandler{}, false},
89+
{"Load Graph Handler", "graph", &graph.GraphAPIHandler{}, false},
90+
{"Unsupported API Type", "unknown", nil, true},
91+
}
92+
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
if tt.wantErr {
96+
mockLogger.On("Error", mock.Anything, mock.Anything).Return(errors.New("Unsupported API type")).Once()
97+
} else {
98+
mockLogger.On("Info", mock.Anything, mock.Anything).Once()
99+
}
100+
101+
got, err := LoadAPIHandler(tt.apiType, mockLogger)
102+
if tt.wantErr {
103+
assert.Error(t, err)
104+
} else {
105+
assert.NoError(t, err)
106+
assert.IsType(t, tt.wantType, got)
107+
}
108+
109+
mockLogger.AssertExpectations(t)
110+
})
111+
}
112+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// httpclient_mocklogger.go
2+
package httpclient
3+
4+
import (
5+
"github.com/deploymenttheory/go-api-http-client/logger"
6+
"github.com/stretchr/testify/mock"
7+
"go.uber.org/zap"
8+
)
9+
10+
// MockLogger is a mock type for the logger.Logger interface used in the httpclient package.
11+
type MockLogger struct {
12+
mock.Mock
13+
}
14+
15+
// Ensure MockLogger implements the logger.Logger interface.
16+
var _ logger.Logger = (*MockLogger)(nil)
17+
18+
// NewMockLogger creates a new instance of MockLogger.
19+
func NewMockLogger() *MockLogger {
20+
return &MockLogger{}
21+
}
22+
23+
// Define all methods from the logger.Logger interface with mock implementations.
24+
func (m *MockLogger) SetLevel(level logger.LogLevel) {
25+
m.Called(level)
26+
}
27+
28+
func (m *MockLogger) Debug(msg string, fields ...zap.Field) {
29+
m.Called(msg, fields)
30+
}
31+
32+
func (m *MockLogger) Info(msg string, fields ...zap.Field) {
33+
m.Called(msg, fields)
34+
}
35+
36+
func (m *MockLogger) Warn(msg string, fields ...zap.Field) {
37+
m.Called(msg, fields)
38+
}
39+
40+
func (m *MockLogger) Error(msg string, fields ...zap.Field) error {
41+
args := m.Called(msg, fields)
42+
return args.Error(0)
43+
}
44+
45+
func (m *MockLogger) Panic(msg string, fields ...zap.Field) {
46+
m.Called(msg, fields)
47+
}
48+
49+
func (m *MockLogger) Fatal(msg string, fields ...zap.Field) {
50+
m.Called(msg, fields)
51+
}
52+
53+
func (m *MockLogger) With(fields ...zap.Field) logger.Logger {
54+
args := m.Called(fields)
55+
return args.Get(0).(logger.Logger)
56+
}
57+
58+
func (m *MockLogger) GetLogLevel() logger.LogLevel {
59+
args := m.Called()
60+
return args.Get(0).(logger.LogLevel)
61+
}

httpclient/httpclient_rate_handler_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/deploymenttheory/go-api-http-client/logger"
1110
"github.com/stretchr/testify/assert"
1211
)
1312

14-
// mockLogger is a minimal implementation of the logger.Logger interface for testing
15-
type mockLogger struct{}
16-
17-
func (m *mockLogger) Debug(msg string, fields ...interface{}) {}
18-
1913
// TestCalculateBackoff tests the backoff calculation for various retry counts
2014
func TestCalculateBackoff(t *testing.T) {
2115
tests := []struct {
@@ -85,7 +79,7 @@ func TestParseRateLimitHeaders(t *testing.T) {
8579
resp.Header.Add(k, v)
8680
}
8781

88-
wait := parseRateLimitHeaders(resp, logger.NewMockLogger())
82+
wait := parseRateLimitHeaders(resp, NewMockLogger())
8983

9084
// Allow a small margin of error due to processing time
9185
assert.InDelta(t, tt.expectedWait, wait, float64(1*time.Second), "Wait duration should be within expected range")

logger/zaplogger_logger.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ import (
1010
"go.uber.org/zap/zapcore"
1111
)
1212

13-
// Logger interface with structured logging capabilities at various levels.
14-
type Logger interface {
15-
SetLevel(level LogLevel)
16-
Debug(msg string, fields ...zapcore.Field)
17-
Info(msg string, fields ...zapcore.Field)
18-
Warn(msg string, fields ...zapcore.Field)
19-
Error(msg string, fields ...zapcore.Field) error
20-
Panic(msg string, fields ...zapcore.Field)
21-
Fatal(msg string, fields ...zapcore.Field)
22-
With(fields ...zapcore.Field) Logger
23-
GetLogLevel() LogLevel
24-
}
25-
2613
// defaultLogger is an implementation of the Logger interface using Uber's zap logging library.
2714
// It provides structured, leveled logging capabilities. The logLevel field controls the verbosity
2815
// of the logs that this logger will produce, allowing filtering of logs based on their importance.
@@ -37,6 +24,19 @@ func (d *defaultLogger) SetLevel(level LogLevel) {
3724
d.logLevel = level
3825
}
3926

27+
// Logger interface with structured logging capabilities at various levels.
28+
type Logger interface {
29+
SetLevel(level LogLevel)
30+
Debug(msg string, fields ...zapcore.Field)
31+
Info(msg string, fields ...zapcore.Field)
32+
Warn(msg string, fields ...zapcore.Field)
33+
Error(msg string, fields ...zapcore.Field) error
34+
Panic(msg string, fields ...zapcore.Field)
35+
Fatal(msg string, fields ...zapcore.Field)
36+
With(fields ...zapcore.Field) Logger
37+
GetLogLevel() LogLevel
38+
}
39+
4040
// With adds contextual key-value pairs to the logger, returning a new logger instance with the context.
4141
// This is useful for creating a logger with common fields that should be included in all subsequent log entries.
4242
func (d *defaultLogger) With(fields ...zapcore.Field) Logger {

logger/zaplogger_logger_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@ import (
1616
// It allows overriding os.Exit in tests to prevent exiting the test runner.
1717
var osExit = os.Exit
1818

19-
// MockLogger is a mock type for the Logger interface, embedding a *zap.Logger to satisfy the type requirement.
20-
type MockLogger struct {
21-
mock.Mock
22-
*zap.Logger
23-
}
24-
25-
// NewMockLogger creates a new instance of MockLogger with an embedded no-op *zap.Logger.
26-
func NewMockLogger() *MockLogger {
27-
return &MockLogger{
28-
Logger: zap.NewNop(),
29-
}
30-
}
31-
3219
// TestDefaultLogger_SetLevel tests the SetLevel method of defaultLogger
3320
func TestDefaultLogger_SetLevel(t *testing.T) {
3421
logger := zap.NewNop()

logger/zaplogger_mocklogger.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// zaplogger_mocklogger.go
2+
package logger
3+
4+
import (
5+
"github.com/stretchr/testify/mock"
6+
"go.uber.org/zap"
7+
)
8+
9+
// MockLogger is a mock type for the Logger interface, embedding a *zap.Logger to satisfy the type requirement.
10+
type MockLogger struct {
11+
mock.Mock
12+
*zap.Logger
13+
}
14+
15+
// NewMockLogger creates a new instance of MockLogger with an embedded no-op *zap.Logger.
16+
func NewMockLogger() *MockLogger {
17+
return &MockLogger{
18+
Logger: zap.NewNop(),
19+
}
20+
}

0 commit comments

Comments
 (0)