Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
149d3d4
Add SQL and database methods for managing capability steps
jlpdeveloper Jul 9, 2026
690c93b
Add request validation for creating capability steps in `createCapabi…
jlpdeveloper Jul 9, 2026
e944eee
Extend `Querier` with methods for managing capability steps
jlpdeveloper Jul 12, 2026
c75638e
Add mock methods for managing capability steps in service tests
jlpdeveloper Jul 12, 2026
411a79b
Add unit tests for `createCapabilityStepRequest` validation and conve…
jlpdeveloper Jul 12, 2026
a9fa538
Rename `service_test.go` to `service_capability_test.go` for improved…
jlpdeveloper Jul 12, 2026
b09ccc2
Add `CreateCapabilityStep` service method with validation and databas…
jlpdeveloper Jul 12, 2026
78419bd
chore: update param name to req in CreateCapabilityStep method
jlpdeveloper Jul 12, 2026
05ea987
Add `CreateCapabilityStep` mock method to service tests
jlpdeveloper Jul 12, 2026
43e95bb
Add `CreateCapabilityStep` route and update mock service for testing
jlpdeveloper Jul 12, 2026
04a59de
Add unit tests for `CreateCapabilityStep` covering success, not found…
jlpdeveloper Jul 12, 2026
1207938
Add `CreateCapabilityStep` HTTP handler, route, and unit tests with v…
jlpdeveloper Jul 12, 2026
d3aaff0
chore: refactor tests to smaller files for readability
jlpdeveloper Jul 12, 2026
c55cceb
Add `POST /capability-steps/` route to router tests
jlpdeveloper Jul 12, 2026
60d439f
Add `GetFlowStep` query and method to retrieve flow step by ID
jlpdeveloper Jul 12, 2026
f76ddd9
Fix typo in `FlowStepId` field and update references in `createCapabi…
jlpdeveloper Jul 12, 2026
070e4e9
Fix typo in `FLowStepId` field and correct references in tests
jlpdeveloper Jul 12, 2026
91704c9
Add `GetFlowStep` mock implementation and update capability step serv…
jlpdeveloper Jul 12, 2026
cb9cdb0
Update error handling for capability step creation to return 404 on n…
jlpdeveloper Jul 12, 2026
a64749c
Add `Create Capability Step` YAML definition with HTTP request and ex…
jlpdeveloper Jul 12, 2026
cc78996
Update `Create Capability Step` docs to clarify step creation descrip…
jlpdeveloper Jul 13, 2026
19a0218
Refactor error logging in `CreateCapabilityStep` to use context-deriv…
jlpdeveloper Jul 13, 2026
e2b4710
Improve error handling in `CreateCapabilityStep` to distinguish betwe…
jlpdeveloper Jul 13, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions _bruno/Capabilities/Create Capability Step.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
info:
name: Create Capability Step
type: http
seq: 7

http:
method: POST
url: "{{baseUrl}}/capability-steps"
body:
type: json
data: |-
{
"flow_step_id": 5,
"capability_id": 1,
"target": "/api/boats",
"protocol": "HTTP"
}
auth: inherit

runtime:
assertions:
- expression: res.status
operator: eq
value: "201"

settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5

examples:
- name: example
request:
url: "{{baseUrl}}/capability-steps"
method: POST
body:
type: json
data: |-
{
"flow_step_id": 5,
"capability_id": 1,
"target": "/api/boats",
"protocol": "HTTP"
}
response:
status: 201
statusText: Created
headers:
- name: content-type
value: application/json
- name: vary
value: Origin, Accept-Encoding
- name: date
value: Sun, 12 Jul 2026 22:23:36 GMT
- name: content-length
value: "147"
body:
type: json
data: |-
{
"id": 1,
"capability_id": 1,
"flow_step_id": 5,
"protocol": "HTTP",
"target": "/api/boats",
"created_at": "2026-07-12T17:23:36.420794-05:00",
"updated_at": "2026-07-12T17:23:36.420794-05:00"
}

docs: |-
# Create Capability Step

Creates a new capability step for a capability and flow step

## Possible Status Codes
| Status Code | Notes |
| -- | --|
| 201 | Created |
| 400 | Invalid body |
| 404 | Capability or Flow Step not found |
| 500 | Internal server error |
14 changes: 14 additions & 0 deletions internal/capability/capabilities.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ SELECT name FROM flows WHERE id = @id;

-- name: GetProduct :one
SELECT name FROM products WHERE id = @id;

-- name: GetFlowStep :one
SELECT id FROM flow_steps WHERE id = @id;

-- name: CreateCapabilityStep :one
INSERT INTO capability_steps(capability_id, flow_step_id, protocol, target, created_at, updated_at)
VALUES(@capability_id, @flow_step_id, @protocol, @target, @timestamp, @timestamp)
RETURNING *;

-- name: GetCapabilitySteps :many
SELECT * FROM capability_steps WHERE capability_id = @capability_id;

-- name: DeleteCapabilityStep :execrows
DELETE FROM capability_steps WHERE capability_id = @capability_id;
89 changes: 89 additions & 0 deletions internal/capability/db/capabilities.sql.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/capability/db/models.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/capability/db/querier.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions internal/capability/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,29 @@ func (h *handler) DeleteCapability(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusNoContent)
}

func (h *handler) CreateCapabilityStep(w http.ResponseWriter, r *http.Request) {
req := &createCapabilityStepRequest{}
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Invalid Body"}, http.StatusBadRequest)
return
}
err := req.Validate()
if err != nil {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Invalid Body"}, http.StatusBadRequest)
return
}
ctxWithTimeout, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
capStep, err := h.service.CreateCapabilityStep(ctxWithTimeout, *req)
if err != nil {
if errors.Is(err, internal.NotFoundError{}) {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: err.Error()}, http.StatusNotFound)
return
}
internal.LoggerFromContext(r.Context()).Error("Failed to create capability step", slog.String("error", err.Error()))
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Failed to create capability step"}, http.StatusInternalServerError)
return
}
internal.WriteJSONResponse(w, r, http.StatusCreated, capStep)
}
108 changes: 108 additions & 0 deletions internal/capability/handler_command_cap_step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package capability

import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"products/internal"
"products/internal/capability/db"
"testing"
)

func TestHandler_CreateCapabilityStep(t *testing.T) {
tests := []struct {
name string
requestBody any
mockSetup func(m *mockCapabilityService)
expectedStatus int
}{
{
name: "Success",
requestBody: createCapabilityStepRequest{
FlowStepId: 1,
CapabilityId: 1,
Target: "Target",
Protocol: "Protocol",
},
mockSetup: func(m *mockCapabilityService) {
m.createCapabilityStepFunc = func(ctx context.Context, req createCapabilityStepRequest) (db.CapabilityStep, error) {
return db.CapabilityStep{ID: 1, CapabilityID: req.CapabilityId, FlowStepID: req.FlowStepId}, nil
}
},
expectedStatus: http.StatusCreated,
},
{
name: "Invalid JSON",
requestBody: "invalid json",
mockSetup: func(m *mockCapabilityService) {},
expectedStatus: http.StatusBadRequest,
},
{
name: "Validation Error",
requestBody: createCapabilityStepRequest{
CapabilityId: 0, // Required
},
mockSetup: func(m *mockCapabilityService) {},
expectedStatus: http.StatusBadRequest,
},
{
name: "Capability Not Found",
requestBody: createCapabilityStepRequest{
FlowStepId: 1,
CapabilityId: 999,
Target: "Target",
Protocol: "Protocol",
},
mockSetup: func(m *mockCapabilityService) {
m.createCapabilityStepFunc = func(ctx context.Context, req createCapabilityStepRequest) (db.CapabilityStep, error) {
return db.CapabilityStep{}, internal.NewNotFoundError(999, "Capability not found")
}
},
expectedStatus: http.StatusNotFound,
},
{
name: "Service Error",
requestBody: createCapabilityStepRequest{
FlowStepId: 1,
CapabilityId: 1,
Target: "Target",
Protocol: "Protocol",
},
mockSetup: func(m *mockCapabilityService) {
m.createCapabilityStepFunc = func(ctx context.Context, req createCapabilityStepRequest) (db.CapabilityStep, error) {
return db.CapabilityStep{}, errors.New("service error")
}
},
expectedStatus: http.StatusInternalServerError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var body []byte
if s, ok := tt.requestBody.(string); ok {
body = []byte(s)
} else {
body, _ = json.Marshal(tt.requestBody)
}

req := httptest.NewRequest(http.MethodPost, "/capability-steps", bytes.NewBuffer(body))
w := httptest.NewRecorder()

mockSvc := &mockCapabilityService{}
if tt.mockSetup != nil {
tt.mockSetup(mockSvc)
}

h := &handler{service: mockSvc}
h.CreateCapabilityStep(w, req)

if w.Code != tt.expectedStatus {
t.Errorf("expected status %d, got %d", tt.expectedStatus, w.Code)
}
})
}
}
Loading
Loading