Skip to content

Commit

Permalink
add O-RAN v3.0 API compliance tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Ding <jackding@gmail.com>
  • Loading branch information
jzding committed Sep 6, 2024
1 parent 1a7aa4c commit 8d7c07f
Showing 1 changed file with 100 additions and 25 deletions.
125 changes: 100 additions & 25 deletions v2/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ var (
port = 8990
apHost = "localhost"
apPath = "/api/ocloudNotifications/v2/"
resource = "/test/test"
resourceNoneSubscribed = "/test/nonesubscribed"
resource = "/cluster/node/mynode.example.com/example-status"
resourceNoneSubscribed = "/cluster/node/mynode.example.com/nonesubscribed"
storePath = "."
ObjSub pubsub.PubSub
ObjPub pubsub.PubSub
)

func onReceiveOverrideFn(_ cloudevents.Event, d *channel.DataChan) error {
func onReceiveOverrideFn(e cloudevents.Event, d *channel.DataChan) error {
if e.Source() != resource {
return fmt.Errorf("could not find any events for requested resource type %s", e.Source())
}

data := &event.Data{
Version: event.APISchemaVersion,
Values: []event.DataValue{},
Expand Down Expand Up @@ -97,9 +101,9 @@ func TestMain(m *testing.M) {
cneEvent.SetTime(types.Timestamp{Time: time.Now().UTC()}.Time)
cneEvent.SetDataContentType(event.ApplicationJSON)
data := event.Data{
Version: "event",
Version: "v1.0",
Values: []event.DataValue{{
Resource: "test",
Resource: resource,
DataType: event.NOTIFICATION,
ValueType: event.ENUMERATION,
Value: ptp.ACQUIRING_SYNC,
Expand Down Expand Up @@ -167,6 +171,55 @@ func TestServer_Health(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.1 Create a subscription resource 5.3.1.5 (2)
func TestServer_CreateSubscription_BadReq(t *testing.T) {
/// create new subscription without message body
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("http://localhost:%d%s%s", port, apPath, "subscriptions"), bytes.NewBuffer(nil))
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := server.HTTPClient.Do(req)
assert.Nil(t, err)
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
bodyString := string(bodyBytes)
log.Print(bodyString)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.1 Create a subscription resource 5.3.1.5 (3)
func TestServer_CreateSubscription_Resource_Not_Avail(t *testing.T) {
// create subscription
sub := api.NewPubSub(
&types.URI{URL: url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port), Path: fmt.Sprintf("%s%s", apPath, "dummy")}},
"resourceNotExist", "2.0")
data, err := json.Marshal(&sub)
assert.Nil(t, err)
assert.NotNil(t, data)
/// create new subscription
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("http://localhost:%d%s%s", port, apPath, "subscriptions"), bytes.NewBuffer(data))
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := server.HTTPClient.Do(req)
assert.Nil(t, err)
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
bodyString := string(bodyBytes)
log.Print(bodyString)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.1 Create a subscription resource 5.3.1.5 (1)
func TestServer_CreateSubscription(t *testing.T) {
// create subscription
sub := api.NewPubSub(
Expand Down Expand Up @@ -200,6 +253,8 @@ func TestServer_CreateSubscription(t *testing.T) {
log.Infof("Subscription:\n%s", ObjSub.String())
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.1 Create a subscription resource 5.3.1.5 (4)
func TestServer_CreateSubscriptionConflict(t *testing.T) {
// create subscription
sub := api.NewPubSub(
Expand All @@ -225,13 +280,39 @@ func TestServer_CreateSubscriptionConflict(t *testing.T) {
assert.Equal(t, http.StatusConflict, resp.StatusCode)
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.2 Get a list of subscription resources
func TestServer_ListSubscriptions(t *testing.T) {
// Get All Subscriptions
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://localhost:%d%s%s", port, apPath, "subscriptions"), nil)
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := server.HTTPClient.Do(req)
assert.Nil(t, err)
defer resp.Body.Close() // Close body only if response non-nil
bodyBytes, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
var subList []pubsub.PubSub
log.Printf("TestServer_ListSubscriptions :%s\n", string(bodyBytes))
err = json.Unmarshal(bodyBytes, &subList)
assert.Nil(t, err)
assert.Greater(t, len(subList), 0)
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.3 Get Detail of individual subscription resource 5.3.3.5 (1)
func TestServer_GetSubscription(t *testing.T) {
// Get Just Created Subscription
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d%s%s/%s", port, apPath, "subscriptions", ObjSub.ID), nil)
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := server.HTTPClient.Do(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
Expand All @@ -244,6 +325,18 @@ func TestServer_GetSubscription(t *testing.T) {
assert.Equal(t, rSub.ID, ObjSub.ID)
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.3 Get Detail of individual subscription resource 5.3.3.5 (2)
func TestServer_GetSubscription_Not_Avail(t *testing.T) {
// Get Just Created Subscription
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d%s%s/%s", port, apPath, "subscriptions", "InvalidId"), nil)
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := server.HTTPClient.Do(req)

Check failure on line 335 in v2/server_test.go

View workflow job for this annotation

GitHub Actions / Linting

response body must be closed (bodyclose)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}

func TestServer_CreatePublisher(t *testing.T) {
pub := pubsub.PubSub{
ID: "",
Expand Down Expand Up @@ -293,26 +386,6 @@ func TestServer_GetPublisher(t *testing.T) {
assert.Equal(t, ObjPub.ID, rPub.ID)
}

func TestServer_ListSubscriptions(t *testing.T) {
// Get All Subscriptions
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://localhost:%d%s%s", port, apPath, "subscriptions"), nil)
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
resp, err := server.HTTPClient.Do(req)
assert.Nil(t, err)
defer resp.Body.Close() // Close body only if response non-nil
bodyBytes, err := io.ReadAll(resp.Body)
assert.Nil(t, err)
var subList []pubsub.PubSub
log.Printf("TestServer_ListSubscriptions :%s\n", string(bodyBytes))
err = json.Unmarshal(bodyBytes, &subList)
assert.Nil(t, err)
assert.Greater(t, len(subList), 0)
}

func TestServer_ListPublishers(t *testing.T) {
// Get All Publisher
ctx := context.Background()
Expand Down Expand Up @@ -376,6 +449,8 @@ func TestServer_TestPingStatusStatusCode(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}

// O-RAN.WG6.O-CLOUD-CONF-Test-R003-v02.00
// TC5.3.4 Delete individual subscription resources
func TestServer_DeleteSubscription(t *testing.T) {
clientIDs := server.GetSubscriberAPI().GetClientIDByResource(ObjSub.Resource)
ctx := context.Background()
Expand Down

0 comments on commit 8d7c07f

Please sign in to comment.