Skip to content

Commit

Permalink
Add tests for every contract endpoint. (#90)
Browse files Browse the repository at this point in the history
This adds tests for every contract HTTP endpoint on both the provider
and consumer side. These tests are based on the examples in the
[IDSA dataspace specification](https://docs.internationaldataspaces.org/ids-knowledgebase/dataspace-protocol/contract-negotiation/contract.negotiation.protocol).

The average test goes as follows:

1. Create a starting state.
2. Submit the message to the correct endpoint.
3. Inspect if the response is correct.
4. Inspect if the storage is in the desired state.
5. Inspect if the right request will be sent out to the reconciliation
   loop.

These tests also exposed some small bugs that are now fixed:

- The reconciler is now an interface to make testing possible.
- SetConsumerPID now actually sets the consumer PID.
- Unify the termination handler as they are on the same path
  for both the consumer and provider flows.
- Fix some ODRL validators.
- Fix the JSON-LD unmarshaller, as it put a nil where it shouldn't be.
  • Loading branch information
ainmosni authored Dec 11, 2024
1 parent 39546c7 commit 55abe33
Show file tree
Hide file tree
Showing 17 changed files with 704 additions and 65 deletions.
2 changes: 1 addition & 1 deletion dsp/common_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
type dspHandlers struct {
store persistence.StorageProvider
provider providerv1.ProviderServiceClient
reconciler *statemachine.Reconciler
reconciler statemachine.Reconciler
selfURL *url.URL
dataserviceID string
dataserviceEndpoint string
Expand Down
2 changes: 1 addition & 1 deletion dsp/contract/negotiation.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (cn *Negotiation) SetProviderPID(u uuid.UUID) {

func (cn *Negotiation) SetConsumerPID(u uuid.UUID) {
cn.panicRO()
cn.providerPID = u
cn.consumerPID = u
cn.modify()
}

Expand Down
23 changes: 13 additions & 10 deletions dsp/contract_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,22 @@ func (dh *dspHandlers) providerContractVerificationHandler(w http.ResponseWriter
)
}

func (dh *dspHandlers) providerContractTerminationHandler(w http.ResponseWriter, req *http.Request) error {
func (dh *dspHandlers) contractTerminationHandler(w http.ResponseWriter, req *http.Request) error {
ctx, _ := logging.InjectLabels(req.Context(), "handler", "providerContractVerificationHandler")
req = req.WithContext(ctx)
pid := req.PathValue("PID")
id, err := uuid.Parse(pid)
if err != nil {
return contractError(fmt.Sprintf("Incalid PID: %s", pid),
http.StatusBadRequest, "400", "Invalid request: PID is not a UUID", nil)
}
if _, err := dh.store.GetContractR(ctx, id, constants.DataspaceProvider); err == nil {
return progressContractState[shared.ContractNegotiationTerminationMessage](
dh, w, req, constants.DataspaceProvider, pid,
)
}
return progressContractState[shared.ContractNegotiationTerminationMessage](
dh, w, req, constants.DataspaceProvider, req.PathValue("providerPID"),
dh, w, req, constants.DataspaceConsumer, pid,
)
}

Expand Down Expand Up @@ -320,11 +331,3 @@ func (dh *dspHandlers) consumerContractEventHandler(w http.ResponseWriter, req *
dh, w, req, constants.DataspaceConsumer, req.PathValue("consumerPID"),
)
}

func (dh *dspHandlers) consumerContractTerminationHandler(w http.ResponseWriter, req *http.Request) error {
ctx, _ := logging.InjectLabels(req.Context(), "handler", "consumerContractEventHandler")
req = req.WithContext(ctx)
return progressContractState[shared.ContractNegotiationTerminationMessage](
dh, w, req, constants.DataspaceConsumer, req.PathValue("consumerPID"),
)
}
Loading

0 comments on commit 55abe33

Please sign in to comment.