Commit 0e5f3103 authored by M. Rehan Abbasi's avatar M. Rehan Abbasi
Browse files

add BWM test cases

parent 188955d0
Loading
Loading
Loading
Loading
+507 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import (
	"testing"
	"time"

	bwm "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-tm/server/bwm"
	mts "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-tm/server/mts"
	dkm "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-data-key-mgr"
	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
@@ -492,6 +493,10 @@ const testScenarioName = "testScenario"

var m *mod.Model

/*
 * MTS Test Cases
 */

func testMtsSessionPost(t *testing.T) (string, string) {

	/******************************
@@ -890,6 +895,508 @@ func TestFailMtsSession(t *testing.T) {
	terminateScenario()
}

/*
 * BWM Test Cases
 */

func testBandwidthAllocationPost(t *testing.T) (string, string) {

	/******************************
	 * expected response section
	 ******************************/

	expectedRequestType := int32(0) // Application specific MTS Session
	expectedAppInsId := "9bdd6acd-f6e4-44f6-a26c-8fd9abd338a7"
	expectedFixedAllocation := "10737418240" // 10Gbps
	expectedAllocationDirection := "00"
	expectedBandwidthAlloc := bwm.BwInfo{
		AppInsId:            expectedAppInsId,
		RequestType:         &expectedRequestType,
		FixedAllocation:     expectedFixedAllocation,
		AllocationDirection: expectedAllocationDirection,
	}
	expectedResponseStr, err := json.Marshal(expectedBandwidthAlloc)
	if err != nil {
		t.Fatalf(err.Error())
	}

	/******************************
	 * request body section
	 ******************************/

	requestType := int32(0)          // Application specific Bandwidth Allocation
	fixedAllocation := "10737418240" // 10Gbps
	requestedBandwidthAlloc := bwm.BwInfo{
		AppInsId:            "9bdd6acd-f6e4-44f6-a26c-8fd9abd338a7",
		RequestType:         &requestType,
		FixedAllocation:     fixedAllocation,
		AllocationDirection: "00",
	}
	body, err := json.Marshal(requestedBandwidthAlloc)
	if err != nil {
		t.Fatalf(err.Error())
	}
	fmt.Println("body: ", string(body))

	/******************************
	 * request execution section
	 ******************************/

	rr, err := sendRequest(http.MethodPost, "/bwm/v1/bw_allocations", bytes.NewBuffer(body), nil, nil, http.StatusCreated, bwm.BandwidthAllocationPOST)
	if err != nil {
		t.Fatalf(err.Error())
	}
	log.Info("Request sent")

	var respBody bwm.BwInfo
	err = json.Unmarshal([]byte(rr), &respBody)
	if err != nil {
		t.Fatalf(err.Error())
	}

	/******************************
	 * Comparing responses
	 ******************************/

	if expectedBandwidthAlloc.AppInsId != respBody.AppInsId {
		t.Fatalf("Failed to get expected response")
	}
	if *expectedBandwidthAlloc.RequestType != *respBody.RequestType {
		t.Fatalf("Failed to get expected response")
	}
	if expectedBandwidthAlloc.FixedAllocation != respBody.FixedAllocation {
		t.Fatalf("Failed to get expected response")
	}
	if expectedBandwidthAlloc.AllocationDirection != respBody.AllocationDirection {
		t.Fatalf("Failed to get expected response")
	}
	if respBody.AllocationId == "" {
		t.Fatalf("Failed to get expected response")
	}
	if respBody.TimeStamp == nil {
		t.Fatalf("Failed to get expected response")
	}

	return respBody.AllocationId, string(expectedResponseStr)
}

func TestBandwidthAllocationListGet(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	initializeVars()

	err := Init()
	if err != nil {
		t.Fatalf("Error initializing test basic procedure")
	}
	err = Run()
	if err != nil {
		t.Fatalf("Error running test basic procedure")
	}

	fmt.Println("Set a scenario")
	initialiseScenario(testScenario)

	// POST
	allocationId1, _ := testBandwidthAllocationPost(t)
	allocationId2, _ := testBandwidthAllocationPost(t)

	// GET list
	testBandwidthAllocationListGet(t)

	// DELETE
	testBandwidthAllocationDelete(t, allocationId1, true)
	testBandwidthAllocationDelete(t, allocationId2, true)

	/******************************
	 * back to initial state section
	 ******************************/

	terminateScenario()
}

func testBandwidthAllocationListGet(t *testing.T) {

	/******************************
	 * expected response section
	 ******************************/
	nbExpectedBandwidthAlloc := 2

	/******************************
	 * request execution section
	 ******************************/

	rr, err := sendRequest(http.MethodGet, "/bwm/v1/bw_allocations", nil, nil, nil, http.StatusOK, bwm.BandwidthAllocationListGET)
	if err != nil {
		t.Fatalf("Failed to get expected response")
	}

	var responseBody []bwm.BwInfo
	err = json.Unmarshal([]byte(rr), &responseBody)
	if err != nil {
		t.Fatalf("Failed to get expected response")
	}

	if len(responseBody) != nbExpectedBandwidthAlloc {
		t.Fatalf("Failed to get expected response, expected none")
	}
}

func testBandwidthAllocationGet(t *testing.T, allocationId string, expectedResponse string) {

	/******************************
	 * expected response section
	 ******************************/
	//passed as a parameter since a POST had to be sent first

	/******************************
	 * request vars section
	 ******************************/
	vars := make(map[string]string)
	vars["allocationId"] = allocationId

	/******************************
	 * request execution section
	 ******************************/
	var err error
	if expectedResponse == "" {
		_, err = sendRequest(http.MethodGet, "/bwm/v1/bw_allocations", nil, vars, nil, http.StatusNotFound, bwm.BandwidthAllocationGET)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}
	} else {
		var expectedResp bwm.BwInfo
		err := json.Unmarshal([]byte(expectedResponse), &expectedResp)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}

		rr, err := sendRequest(http.MethodGet, "/bwm/v1/bw_allocations", nil, vars, nil, http.StatusOK, bwm.BandwidthAllocationGET)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}
		var respBody bwm.BwInfo
		err = json.Unmarshal([]byte(rr), &respBody)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}

		if expectedResp.AppInsId != respBody.AppInsId {
			t.Fatalf("Failed to get expected response")
		}
		if *expectedResp.RequestType != *respBody.RequestType {
			t.Fatalf("Failed to get expected response")
		}
		if expectedResp.FixedAllocation != respBody.FixedAllocation {
			t.Fatalf("Failed to get expected response")
		}
		if expectedResp.AllocationDirection != respBody.AllocationDirection {
			t.Fatalf("Failed to get expected response")
		}
		if respBody.AllocationId == "" {
			t.Fatalf("Failed to get expected response")
		}
		if respBody.TimeStamp == nil {
			t.Fatalf("Failed to get expected response")
		}
	}
}

func testBandwidthAllocationPut(t *testing.T, allocationId string, expectSuccess bool) string {
	/******************************
	 * expected response section
	 ******************************/

	expectedRequestType := int32(0) // Application specific MTS Session
	expectedAppInsId := "9bdd6acd-f6e4-44f6-a26c-8fd9abd338a7"
	expectedFixedAllocation := "21474836480" // 20Gbps
	expectedAllocationDirection := "00"
	expectedBandwidthAlloc := bwm.BwInfo{
		AppInsId:            expectedAppInsId,
		RequestType:         &expectedRequestType,
		FixedAllocation:     expectedFixedAllocation,
		AllocationDirection: expectedAllocationDirection,
	}
	expectedResponseStr, err := json.Marshal(expectedBandwidthAlloc)
	if err != nil {
		t.Fatalf(err.Error())
	}

	/******************************
	 * request vars section
	 ******************************/
	vars := make(map[string]string)
	vars["allocationId"] = allocationId

	/******************************
	 * request body section
	 ******************************/

	requestType := int32(0)          // Application specific Bandwidth Allocation
	fixedAllocation := "21474836480" // 20Gbps
	requestedBandwidthAlloc := bwm.BwInfo{
		AppInsId:            "9bdd6acd-f6e4-44f6-a26c-8fd9abd338a7",
		RequestType:         &requestType,
		FixedAllocation:     fixedAllocation,
		AllocationDirection: "00",
	}
	body, err := json.Marshal(requestedBandwidthAlloc)
	if err != nil {
		t.Fatalf(err.Error())
	}
	fmt.Println("body: ", string(body))

	/******************************
	 * request queries section
	 ******************************/

	/******************************
	 * request execution section
	 ******************************/

	if expectSuccess {
		rr, err := sendRequest(http.MethodPut, "/bwm/v1/bw_allocations", bytes.NewBuffer(body), vars, nil, http.StatusOK, bwm.BandwidthAllocationPUT)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}

		var respBody bwm.BwInfo
		err = json.Unmarshal([]byte(rr), &respBody)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}

		if expectedBandwidthAlloc.AppInsId != respBody.AppInsId {
			t.Fatalf("Failed to get expected response")
		}
		if *expectedBandwidthAlloc.RequestType != *respBody.RequestType {
			t.Fatalf("Failed to get expected response")
		}
		if expectedBandwidthAlloc.FixedAllocation != respBody.FixedAllocation {
			t.Fatalf("Failed to get expected response")
		}
		if expectedBandwidthAlloc.AllocationDirection != respBody.AllocationDirection {
			t.Fatalf("Failed to get expected response")
		}
		if respBody.AllocationId == "" {
			t.Fatalf("Failed to get expected response")
		}
		if respBody.TimeStamp == nil {
			t.Fatalf("Failed to get expected response")
		}

		return string(expectedResponseStr)
	} else {
		_, err = sendRequest(http.MethodPut, "/bwm/v1/bw_allocations", bytes.NewBuffer(body), vars, nil, http.StatusNotFound, bwm.BandwidthAllocationPUT)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}
		return ""
	}
}

func testBandwidthAllocationPatch(t *testing.T, allocationId string, expectSuccess bool) string {
	/******************************
	 * expected response section
	 ******************************/

	expectedRequestType := int32(0) // Application specific MTS Session
	expectedAppInsId := "9bdd6acd-f6e4-44f6-a26c-8fd9abd338a7"
	expectedFixedAllocation := "5368709120" // 5Gbps
	expectedAllocationDirection := "00"
	expectedBandwidthAlloc := bwm.BwInfo{
		AppInsId:            expectedAppInsId,
		RequestType:         &expectedRequestType,
		FixedAllocation:     expectedFixedAllocation,
		AllocationDirection: expectedAllocationDirection,
	}
	expectedResponseStr, err := json.Marshal(expectedBandwidthAlloc)
	if err != nil {
		t.Fatalf(err.Error())
	}

	/******************************
	 * request vars section
	 ******************************/
	vars := make(map[string]string)
	vars["allocationId"] = allocationId

	/******************************
	 * request body section
	 ******************************/

	requestType := int32(0)
	fixedAllocation := "5368709120" // 5Gbps
	requestedBandwidthAllocDeltas := bwm.BwInfoDeltas{
		AllocationId:    allocationId,
		AppInsId:        "9bdd6acd-f6e4-44f6-a26c-8fd9abd338a7",
		RequestType:     &requestType,
		FixedAllocation: fixedAllocation,
	}
	body, err := json.Marshal(requestedBandwidthAllocDeltas)
	if err != nil {
		t.Fatalf(err.Error())
	}
	fmt.Println("body: ", string(body))

	/******************************
	 * request queries section
	 ******************************/

	/******************************
	 * request execution section
	 ******************************/

	if expectSuccess {
		rr, err := sendRequest(http.MethodPatch, "/bwm/v1/bw_allocations", bytes.NewBuffer(body), vars, nil, http.StatusOK, bwm.BandwidthAllocationPATCH)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}

		var respBody bwm.BwInfo
		err = json.Unmarshal([]byte(rr), &respBody)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}

		if expectedBandwidthAlloc.AppInsId != respBody.AppInsId {
			t.Fatalf("Failed to get expected response")
		}
		if *expectedBandwidthAlloc.RequestType != *respBody.RequestType {
			t.Fatalf("Failed to get expected response")
		}
		if expectedBandwidthAlloc.FixedAllocation != respBody.FixedAllocation {
			t.Fatalf("Failed to get expected response")
		}
		if expectedBandwidthAlloc.AllocationDirection != respBody.AllocationDirection {
			t.Fatalf("Failed to get expected response")
		}
		if respBody.AllocationId == "" {
			t.Fatalf("Failed to get expected response")
		}
		if respBody.TimeStamp == nil {
			t.Fatalf("Failed to get expected response")
		}

		return string(expectedResponseStr)
	} else {
		_, err = sendRequest(http.MethodPatch, "/bwm/v1/bw_allocations", bytes.NewBuffer(body), vars, nil, http.StatusNotFound, bwm.BandwidthAllocationPATCH)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}
		return ""
	}
}

func testBandwidthAllocationDelete(t *testing.T, allocationId string, expectSuccess bool) {

	/******************************
	 * request vars section
	 ******************************/
	vars := make(map[string]string)
	vars["allocationId"] = allocationId

	/******************************
	 * request execution section
	 ******************************/

	if expectSuccess {
		_, err := sendRequest(http.MethodDelete, "/bwm/v1/bw_allocations", nil, vars, nil, http.StatusNoContent, bwm.BandwidthAllocationDELETE)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}
	} else {
		_, err := sendRequest(http.MethodDelete, "/bwm/v1/bw_allocations", nil, vars, nil, http.StatusNotFound, bwm.BandwidthAllocationDELETE)
		if err != nil {
			t.Fatalf("Failed to get expected response")
		}
	}
}

func TestSuccessBandwidthAllocation(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	initializeVars()

	err := Init()
	if err != nil {
		t.Fatalf("Error initializing test basic procedure")
	}
	err = Run()
	if err != nil {
		t.Fatalf("Error running test basic procedure")
	}

	fmt.Println("Set a scenario")
	initialiseScenario(testScenario)

	time.Sleep(1000 * time.Millisecond)
	updateScenario("mobility1")

	// POST
	allocationId, expectedGetResp := testBandwidthAllocationPost(t)
	// GET
	testBandwidthAllocationGet(t, allocationId, expectedGetResp)
	// PUT
	expectedGetResp = testBandwidthAllocationPut(t, allocationId, true)
	// GET
	testBandwidthAllocationGet(t, allocationId, expectedGetResp)
	// PATCH
	expectedGetResp = testBandwidthAllocationPatch(t, allocationId, true)
	// GET
	testBandwidthAllocationGet(t, allocationId, expectedGetResp)
	// DELETE
	testBandwidthAllocationDelete(t, allocationId, true)

	/******************************
	 * back to initial state section
	 ******************************/

	terminateScenario()
}

func TestFailBandwidthAllocation(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	initializeVars()

	err := Init()
	if err != nil {
		t.Fatalf("Error initializing test basic procedure")
	}
	err = Run()
	if err != nil {
		t.Fatalf("Error running test basic procedure")
	}

	fmt.Println("Set a scenario")
	initialiseScenario(testScenario)

	time.Sleep(1000 * time.Millisecond)
	updateScenario("mobility1")

	// GET
	testBandwidthAllocationGet(t, "invalidAllocationId", "")

	// PUT
	_ = testBandwidthAllocationPut(t, "invalidAllocationId", false)

	// PATCH
	_ = testBandwidthAllocationPatch(t, "invalidAllocationId", false)

	// DELETE
	testBandwidthAllocationDelete(t, "invalidAllocationId", false)

	/******************************
	 * back to initial state section
	 ******************************/

	terminateScenario()
}

func terminateScenario() {
	if mqLocal != nil {
		_ = Stop()