X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=servicemanager%2Finternal%2Fdiscoverservice%2Fdiscoverservice_test.go;fp=servicemanager%2Finternal%2Fdiscoverservice%2Fdiscoverservice_test.go;h=7c4b9f883fca6d52793f2d7029a1089a5c7eea28;hb=40b608da2929aa753b3e87733fd0900b8e3dcbc4;hp=24246586add4fecb3da34ac03d18d3c9cd5ce4c1;hpb=edd27d1226439498fce3a14a86f74385298e8abf;p=nonrtric%2Fplt%2Fsme.git diff --git a/servicemanager/internal/discoverservice/discoverservice_test.go b/servicemanager/internal/discoverservice/discoverservice_test.go index 2424658..7c4b9f8 100644 --- a/servicemanager/internal/discoverservice/discoverservice_test.go +++ b/servicemanager/internal/discoverservice/discoverservice_test.go @@ -23,6 +23,8 @@ package discoverservice import ( "fmt" "net/http" + "net/http/httptest" + "net/url" "os" "reflect" "sort" @@ -46,13 +48,24 @@ import ( "oransc.org/nonrtric/servicemanager/internal/envreader" "oransc.org/nonrtric/servicemanager/internal/kongclear" - "oransc.org/nonrtric/servicemanager/internal/providermanagement" + "oransc.org/nonrtric/servicemanager/internal/providermanagement" provapi "oransc.org/nonrtric/servicemanager/internal/providermanagementapi" "oransc.org/nonrtric/servicemanager/internal/publishservice" + + "oransc.org/nonrtric/capifcore" + "oransc.org/nonrtric/servicemanager/mockkong" ) -var requestHandler *echo.Echo +var ( + eServiceManager *echo.Echo + eCapifWeb *echo.Echo + eKong *echo.Echo + mockConfigReader *envreader.MockConfigReader + serviceManagerServer *httptest.Server + capifServer *httptest.Server + mockKongServer *httptest.Server +) func TestMain(m *testing.M) { err := setupTest() @@ -68,22 +81,69 @@ func TestMain(m *testing.M) { } func setupTest() error { - myEnv, myPorts, err := envreader.ReadDotEnv() + // Start the mock Kong server + eKong = echo.New() + mockKong.RegisterHandlers(eKong) + mockKongServer = httptest.NewServer(eKong) + + // Parse the server URL + parsedMockKongURL, err := url.Parse(mockKongServer.URL) if err != nil { - log.Fatal("error loading environment file on setupTest") + log.Fatalf("error parsing mock Kong URL: %v", err) return err } - requestHandler, err = getEcho(myEnv, myPorts) + // Extract the host and port + mockKongHost := parsedMockKongURL.Hostname() + mockKongControlPlanePort := parsedMockKongURL.Port() + + eCapifWeb = echo.New() + capifcore.RegisterHandlers(eCapifWeb, nil, nil) + capifServer = httptest.NewServer(eCapifWeb) + + // Parse the server URL + parsedCapifURL, err := url.Parse(capifServer.URL) + if err != nil { + log.Fatalf("error parsing mock Kong URL: %v", err) + return err + } + + // Extract the host and port + capifHost := parsedCapifURL.Hostname() + capifPort := parsedCapifURL.Port() + + // Set up the mock config reader with the desired configuration for testing + mockConfigReader = &envreader.MockConfigReader{ + MockedConfig: map[string]string{ + "KONG_DOMAIN": "kong", + "KONG_PROTOCOL": "http", + "KONG_IPV4": mockKongHost, + "KONG_DATA_PLANE_PORT": "32080", + "KONG_CONTROL_PLANE_PORT": mockKongControlPlanePort, + "CAPIF_PROTOCOL": "http", + "CAPIF_IPV4": capifHost, + "CAPIF_PORT": capifPort, + "LOG_LEVEL": "Info", + "SERVICE_MANAGER_PORT": "8095", + "TEST_SERVICE_IPV4": "10.101.1.101", + "TEST_SERVICE_PORT": "30951", + }, + } + + myEnv, myPorts, err := mockConfigReader.ReadDotEnv() if err != nil { - log.Fatal("getEcho fatal error on setupTest") + log.Fatal("error loading environment file on setupTest") return err } - err = teardown() + + eServiceManager = echo.New() + err = registerHandlers(eServiceManager, myEnv, myPorts) if err != nil { - log.Fatal("getEcho fatal error on teardown") + log.Fatal("registerHandlers fatal error on setupTest") return err } + serviceManagerServer = httptest.NewServer(eServiceManager) + capifCleanUp() return err } @@ -118,16 +178,15 @@ func getProvider() provapi.APIProviderEnrolmentDetails { } } -func teardown() error { - log.Trace("entering teardown") - t := new(testing.T) // Create a new testing.T instance for teardown +func capifCleanUp() { + t := new(testing.T) // Create a new testing.T instance for capifCleanUp // Delete the invoker invokerInfo := "invoker a" invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1) - result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, requestHandler) + result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the original published service @@ -135,7 +194,7 @@ func teardown() error { apiName := "apiName" apiId := "api_id_" + apiName - result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the first published service @@ -143,22 +202,26 @@ func teardown() error { apiName = "apiName1" apiId = "api_id_" + apiName - result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the second published service apiName = "apiName2" apiId = "api_id_" + apiName - result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) // Delete the provider domainID := "domain_id_Kong" - result = testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, requestHandler) + result = testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, eServiceManager) assert.Equal(t, http.StatusNoContent, result.Code()) +} + +func teardown() error { + log.Trace("entering teardown") - myEnv, myPorts, err := envreader.ReadDotEnv() + myEnv, myPorts, err := mockConfigReader.ReadDotEnv() if err != nil { log.Fatal("error loading environment file") return err @@ -168,15 +231,20 @@ func teardown() error { if err != nil { log.Fatal("error clearing Kong on teardown") } + + mockKongServer.Close() + capifServer.Close() + serviceManagerServer.Close() + return err } func TestRegisterValidProvider(t *testing.T) { - teardown() + capifCleanUp() newProvider := getProvider() // Register a valid provider - result := testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, requestHandler) + result := testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) var resultProvider provapi.APIProviderEnrolmentDetails @@ -189,7 +257,7 @@ func TestPublishUnpublishService(t *testing.T) { apiName := "apiName1" apiId := "api_id_" + apiName - myEnv, myPorts, err := envreader.ReadDotEnv() + myEnv, myPorts, err := mockConfigReader.ReadDotEnv() assert.Nil(t, err, "error reading env file") testServiceIpv4 := common29122.Ipv4Addr(myEnv["TEST_SERVICE_IPV4"]) @@ -199,7 +267,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.NotZero(t, testServicePort, "TEST_SERVICE_PORT is required in .env file for unit testing") // Check no services published - result := testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, requestHandler) + result := testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) // Parse JSON from the response body @@ -226,7 +294,7 @@ func TestPublishUnpublishService(t *testing.T) { newServiceDescription := getServiceAPIDescription(aefId, apiName, apiCategory, apiVersion, &protocolHTTP11, &dataFormatJSON, description, testServiceIpv4, testServicePort, publishapi.CommunicationTypeREQUESTRESPONSE) // Publish a service for provider - result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler) + result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) if result.Code() != http.StatusCreated { @@ -241,7 +309,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.Equal(t, "http://example.com/published-apis/v1/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation)) // Check that the service is published for the provider - result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler) + result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) err = result.UnmarshalJsonToObject(&resultService) @@ -264,7 +332,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.Equal(t, kongDataPlanePort, resultServicePort) // Check one service published - result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, requestHandler) + result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) // Parse JSON from the response body @@ -284,7 +352,7 @@ func TestPublishUnpublishService(t *testing.T) { newServiceDescription2 := getServiceAPIDescription(aefId, apiName2, apiCategory, apiVersion, &protocolHTTP1, &dataFormatOther, description, testServiceIpv4, testServicePort, publishapi.CommunicationTypeSUBSCRIBENOTIFY) - result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription2).Go(t, requestHandler) + result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription2).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) if result.Code() != http.StatusCreated { @@ -297,7 +365,7 @@ func TestPublishUnpublishService(t *testing.T) { assert.Equal(t, apiId2, *resultService.ApiId) // Check no services published - result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, requestHandler) + result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) // Parse JSON from the response body @@ -313,7 +381,7 @@ func TestOnboardInvoker(t *testing.T) { newInvoker := getInvoker(invokerInfo) // Onboard a valid invoker - result := testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) + result := testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(newInvoker).Go(t, eServiceManager) assert.Equal(t, http.StatusCreated, result.Code()) var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails @@ -336,7 +404,7 @@ func TestGetAllServiceAPIs(t *testing.T) { apiName2 := "apiName2" // Get all APIs, without any filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -364,7 +432,7 @@ func TestGetAllServiceAPIsWhenMissingProvider(t *testing.T) { invokerId := "unregistered" // Get all APIs, without any filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId).Go(t, eServiceManager) assert.Equal(t, http.StatusNotFound, result.Code()) var problemDetails common29122.ProblemDetails @@ -383,7 +451,7 @@ func TestFilterApiName(t *testing.T) { invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1) // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-name="+apiName).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-name="+apiName).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -408,7 +476,7 @@ func TestFilterAefId(t *testing.T) { aefId := "AEF_id_rApp_Kong_as_AEF" // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&aef-id="+aefId).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&aef-id="+aefId).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -438,7 +506,7 @@ func TestFilterVersion(t *testing.T) { apiVersion := "v1" // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -463,7 +531,7 @@ func TestFilterCommType(t *testing.T) { commType := publishapi.CommunicationTypeREQUESTRESPONSE // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&comm-type="+string(commType)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&comm-type="+string(commType)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -488,7 +556,7 @@ func TestFilterVersionAndCommType(t *testing.T) { commType := publishapi.CommunicationTypeREQUESTRESPONSE // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion+"&comm-type="+string(commType)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-version="+apiVersion+"&comm-type="+string(commType)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -512,7 +580,7 @@ func TestFilterAPICategory(t *testing.T) { apiCategory := "apiCategory" // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-cat="+apiCategory).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&api-cat="+apiCategory).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -536,7 +604,7 @@ func TestFilterProtocol(t *testing.T) { var protocolHTTP11 = publishapi.ProtocolHTTP11 // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&protocol="+string(protocolHTTP11)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&protocol="+string(protocolHTTP11)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -561,7 +629,7 @@ func TestFilterDataFormat(t *testing.T) { var dataFormatJSON = publishapi.DataFormatJSON // Get APIs with filter - result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&data-format="+string(dataFormatJSON)).Go(t, requestHandler) + result := testutil.NewRequest().Get("/service-apis/v1/allServiceAPIs?api-invoker-id="+invokerId+"&data-format="+string(dataFormatJSON)).Go(t, eServiceManager) assert.Equal(t, http.StatusOK, result.Code()) var resultDiscovery discoverserviceapi.DiscoveredAPIs @@ -576,10 +644,10 @@ func TestFilterDataFormat(t *testing.T) { assert.Equal(t, apiName, (*resultDiscovery.ServiceAPIDescriptions)[0].ApiName) } } - teardown() + capifCleanUp() } -func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error) { +func registerHandlers(e *echo.Echo, myEnv map[string]string, myPorts map[string]int) (err error) { capifProtocol := myEnv["CAPIF_PROTOCOL"] capifIPv4 := common29122.Ipv4Addr(myEnv["CAPIF_IPV4"]) capifPort := common29122.Port(myPorts["CAPIF_PORT"]) @@ -589,13 +657,11 @@ func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"]) kongControlPlanePort := common29122.Port(myPorts["KONG_CONTROL_PLANE_PORT"]) - e := echo.New() - // Register ProviderManagement providerManagerSwagger, err := provapi.GetSwagger() if err != nil { log.Fatalf("error loading ProviderManagement swagger spec\n: %s", err) - return nil, err + return err } providerManagerSwagger.Servers = nil providerManager := providermanagement.NewProviderManager(capifProtocol, capifIPv4, capifPort) @@ -610,7 +676,7 @@ func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error publishServiceSwagger, err := publishapi.GetSwagger() if err != nil { fmt.Fprintf(os.Stderr, "Error loading PublishService swagger spec\n: %s", err) - return nil, err + return err } publishServiceSwagger.Servers = nil @@ -626,7 +692,7 @@ func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error invokerServiceSwagger, err := invokermanagementapi.GetSwagger() if err != nil { fmt.Fprintf(os.Stderr, "Error loading InvokerManagement swagger spec\n: %s", err) - return nil, err + return err } invokerServiceSwagger.Servers = nil @@ -654,7 +720,7 @@ func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error group.Use(middleware.OapiRequestValidator(discoverySeviceSwagger)) discoverserviceapi.RegisterHandlersWithBaseURL(e, ds, "service-apis/v1") - return e, err + return err } func getServiceAPIDescription(aefId string, apiName string, apiCategory string, apiVersion string, protocol *publishapi.Protocol, dataFormat *publishapi.DataFormat, description string, testServiceIpv4 common29122.Ipv4Addr, testServicePort common29122.Port, commType publishapi.CommunicationType) publishapi.ServiceAPIDescription {