Commit aa53523c authored by Yann Garcia's avatar Yann Garcia
Browse files

Add first test on PredictedQos

parent 9cfa5ea4
Loading
Loading
Loading
Loading
+118 −3
Original line number Diff line number Diff line
package server

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"strconv"
	//"strconv"
	"testing"
	"time"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	met "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metrics"
	//	met "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metrics"
	mod "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-model"
	mq "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-mq"

@@ -480,9 +482,122 @@ func TestNotImplemented(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	//s1_bearer_info
	_, err := sendRequest(http.MethodGet, "/queries/uu_unicast_provisioning_info", nil, nil, nil, http.StatusNotImplemented, ProvInfoUuUnicastGET)
	if err != nil {
		t.Fatalf("Failed to get expected response")
	}
}

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

	/******************************
	 * expected response section
	 ******************************/
	// Initialize the data structure for the POST request
	// MEC-030 Clause 6.2.5
	// MEC-030 Clause 7.6.3.4
	expected_pointA := LocationInfoGeoArea{7.413917, 43.733505}
	expected_locationInfoA := LocationInfo{nil, &expected_pointA}
	expected_pointB := LocationInfoGeoArea{7.413916, 43.733515}
	expected_locationInfoB := LocationInfo{nil, &expected_pointB}
	// Fill PredictedQosRoutesRouteInfo with LocationInfo list
	expected_routeInfo := make([]PredictedQosRoutesRouteInfo, 2)
	expected_routeInfo[0] = PredictedQosRoutesRouteInfo{&expected_locationInfoA, 0, 0, nil}
	expected_routeInfo[1] = PredictedQosRoutesRouteInfo{&expected_locationInfoB, 0, 0, nil}
	// PredictedQosRoutes with PredictedQosRoutesRouteInfo list
	expected_predictedQosRoutes := PredictedQosRoutes{expected_routeInfo}
	// Fill PredictedQos with PredictedQosRoutes list
	expected_routes := make([]PredictedQosRoutes, 1)
	expected_routes[0] = expected_predictedQosRoutes
	expected_predictedQos := PredictedQos{"100", expected_routes, nil}
	fmt.Println("expected_predictedQos: ", expected_predictedQos)
	expected_predictedQos_str, err := json.Marshal(expected_predictedQos)
	if err != nil {
		t.Fatalf(err.Error())
	}
	fmt.Println("expected_predictedQos_str: ", string(expected_predictedQos_str))

	/******************************
	 * request body section
	 ******************************/
	// Initialize the data structure for the POST request
	// MEC-030 Clause 6.2.5
	// MEC-030 Clause 7.6.3.4
	pointA := LocationInfoGeoArea{7.413917, 43.733505}
	locationInfoA := LocationInfo{nil, &pointA}
	tsA := TimeStamp{0, 45}
	pointB := LocationInfoGeoArea{7.413916, 43.733515}
	locationInfoB := LocationInfo{nil, &pointB}
	tsB := TimeStamp{0, 45}
	// Fill PredictedQosRoutesRouteInfo with LocationInfo list
	routeInfo := make([]PredictedQosRoutesRouteInfo, 2)
	routeInfo[0] = PredictedQosRoutesRouteInfo{&locationInfoA, 0, 0, &tsA}
	routeInfo[1] = PredictedQosRoutesRouteInfo{&locationInfoB, 0, 0, &tsB}
	// PredictedQosRoutes with PredictedQosRoutesRouteInfo list
	predictedQosRoutes := PredictedQosRoutes{routeInfo}
	// Fill PredictedQos with PredictedQosRoutes list
	routes := make([]PredictedQosRoutes, 1)
	routes[0] = predictedQosRoutes
	testPredictedQos := PredictedQos{"100", routes, nil}
	fmt.Println("testPredictedQos: ", testPredictedQos)
	body, err := json.Marshal(testPredictedQos)
	if err != nil {
		t.Fatalf(err.Error())
	}
	fmt.Println("body: ", string(body))

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

	rr, err := sendRequest(http.MethodPost, "/provide_predicted_qos", bytes.NewBuffer(body), nil, nil, http.StatusOK, PredictedQosPOST)
	if err != nil {
		t.Fatalf("Failed to get expected expected")
	}

	var respBody PredictedQos
	err = json.Unmarshal([]byte(rr), &respBody)
	if err != nil {
		t.Fatalf("Failed to get expected response")
	}
	fmt.Println("respBody: ", respBody)
	if rr != string(expected_predictedQos_str) {
		t.Fatalf("Failed to get expected response")
	}
}

func sendRequest(method string, url string, body io.Reader, vars map[string]string, query map[string]string, code int, f http.HandlerFunc) (string, error) {
	req, err := http.NewRequest(method, url, body)
	if err != nil || req == nil {
		return "", err
	}
	if vars != nil {
		req = mux.SetURLVars(req, vars)
	}
	if query != nil {
		q := req.URL.Query()
		for k, v := range query {
			q.Add(k, v)
		}
		req.URL.RawQuery = q.Encode()
	}

	// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
	rr := httptest.NewRecorder()
	handler := http.HandlerFunc(f)

	// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
	// directly and pass in our Request and ResponseRecorder.
	handler.ServeHTTP(rr, req)

	time.Sleep(50 * time.Millisecond)

	// Check the status code is what we expect.
	if status := rr.Code; status != code {
		s := fmt.Sprintf("Wrong status code - got %v want %v", status, code)
		return "", errors.New(s)
	}
	return string(rr.Body.String()), nil
}