<commit_before>package route_test

import (
	"cf"
	. "cf/commands/route"
	"cf/configuration"
	"github.com/stretchr/testify/assert"
	testapi "testhelpers/api"
	testcmd "testhelpers/commands"
	testconfig "testhelpers/configuration"
	testreq "testhelpers/requirements"
	testterm "testhelpers/terminal"
	"testing"
)

func TestListingRoutes(t *testing.T) {
	domain := cf.DomainFields{}
	domain.Name = "example.com"
	domain2 := cf.DomainFields{}
	domain2.Name = "cfapps.com"
	domain3 := cf.DomainFields{}
	domain3.Name = "another-example.com"

	app1 := cf.ApplicationFields{}
	app1.Name = "dora"
	app2 := cf.ApplicationFields{}
	app2.Name = "dora2"

	app3 := cf.ApplicationFields{}
	app3.Name = "my-app"
	app4 := cf.ApplicationFields{}
	app4.Name = "my-app2"

	app5 := cf.ApplicationFields{}
	app5.Name = "july"

	route := cf.Route{}
	route.Host = "hostname-1"
	route.Domain = domain
	route.Apps = []cf.ApplicationFields{app1, app2}
	route2 := cf.Route{}
	route2.Host = "hostname-2"
	route2.Domain = domain2
	route2.Apps = []cf.ApplicationFields{app3, app4}
	route3 := cf.Route{}
	route3.Host = "hostname-3"
	route3.Domain = domain3
	route3.Apps = []cf.ApplicationFields{app5}
	routes := []cf.Route{route, route2, route3}

	routeRepo := &testapi.FakeRouteRepository{Routes: routes}

	ui := callListRoutes(t, []string{}, &testreq.FakeReqFactory{}, routeRepo)

	assert.Contains(t, ui.Outputs[0], "Getting routes")
	assert.Contains(t, ui.Outputs[0], "my-user")

	assert.Contains(t, ui.Outputs[1], "host")
	assert.Contains(t, ui.Outputs[1], "domain")
	assert.Contains(t, ui.Outputs[1], "apps")

	assert.Contains(t, ui.Outputs[2], "hostname-1")
	assert.Contains(t, ui.Outputs[2], "example.com")
	assert.Contains(t, ui.Outputs[2], "dora, dora2")

	assert.Contains(t, ui.Outputs[3], "hostname-2")
	assert.Contains(t, ui.Outputs[3], "cfapps.com")
	assert.Contains(t, ui.Outputs[3], "my-app, my-app2")

	assert.Contains(t, ui.Outputs[4], "hostname-3")
	assert.Contains(t, ui.Outputs[4], "another-example.com")
	assert.Contains(t, ui.Outputs[4], "july")
}

func TestListingRoutesWhenNoneExist(t *testing.T) {
	routes := []cf.Route{}
	routeRepo := &testapi.FakeRouteRepository{Routes: routes}

	ui := callListRoutes(t, []string{}, &testreq.FakeReqFactory{}, routeRepo)

	assert.Contains(t, ui.Outputs[0], "Getting routes")
	assert.Contains(t, ui.Outputs[0], "my-user")
	assert.Contains(t, ui.Outputs[1], "No routes found")
}

func TestListingRoutesWhenFindFails(t *testing.T) {
	routeRepo := &testapi.FakeRouteRepository{ListErr: true}

	ui := callListRoutes(t, []string{}, &testreq.FakeReqFactory{}, routeRepo)

	assert.Contains(t, ui.Outputs[0], "Getting routes")
	assert.Contains(t, ui.Outputs[1], "FAILED")
}

func callListRoutes(t *testing.T, args []string, reqFactory *testreq.FakeReqFactory, routeRepo *testapi.FakeRouteRepository) (ui *testterm.FakeUI) {

	ui = &testterm.FakeUI{}

	ctxt := testcmd.NewContext("list-routes", args)

	token, err := testconfig.CreateAccessTokenWithTokenInfo(configuration.TokenInfo{
		Username: "my-user",
	})
	assert.NoError(t, err)
	space := cf.SpaceFields{}
	space.Name = "my-space"
	org := cf.OrganizationFields{}
	org.Name = "my-org"<commit_msg>correct test context for routes command in test helper<commit_after>package route_test

import (
	"cf"
	. "cf/commands/route"
	"cf/configuration"
	"github.com/stretchr/testify/assert"
	testapi "testhelpers/api"
	testcmd "testhelpers/commands"
	testconfig "testhelpers/configuration"
	testreq "testhelpers/requirements"
	testterm "testhelpers/terminal"
	"testing"
)

func TestListingRoutes(t *testing.T) {
	domain := cf.DomainFields{}
	domain.Name = "example.com"
	domain2 := cf.DomainFields{}
	domain2.Name = "cfapps.com"
	domain3 := cf.DomainFields{}
	domain3.Name = "another-example.com"

	app1 := cf.ApplicationFields{}
	app1.Name = "dora"
	app2 := cf.ApplicationFields{}
	app2.Name = "dora2"

	app3 := cf.ApplicationFields{}
	app3.Name = "my-app"
	app4 := cf.ApplicationFields{}
	app4.Name = "my-app2"

	app5 := cf.ApplicationFields{}
	app5.Name = "july"

	route := cf.Route{}
	route.Host = "hostname-1"
	route.Domain = domain
	route.Apps = []cf.ApplicationFields{app1, app2}
	route2 := cf.Route{}
	route2.Host = "hostname-2"
	route2.Domain = domain2
	route2.Apps = []cf.ApplicationFields{app3, app4}
	route3 := cf.Route{}
	route3.Host = "hostname-3"
	route3.Domain = domain3
	route3.Apps = []cf.ApplicationFields{app5}
	routes := []cf.Route{route, route2, route3}

	routeRepo := &testapi.FakeRouteRepository{Routes: routes}

	ui := callListRoutes(t, []string{}, &testreq.FakeReqFactory{}, routeRepo)

	assert.Contains(t, ui.Outputs[0], "Getting routes")
	assert.Contains(t, ui.Outputs[0], "my-user")

	assert.Contains(t, ui.Outputs[1], "host")
	assert.Contains(t, ui.Outputs[1], "domain")
	assert.Contains(t, ui.Outputs[1], "apps")

	assert.Contains(t, ui.Outputs[2], "hostname-1")
	assert.Contains(t, ui.Outputs[2], "example.com")
	assert.Contains(t, ui.Outputs[2], "dora, dora2")

	assert.Contains(t, ui.Outputs[3], "hostname-2")
	assert.Contains(t, ui.Outputs[3], "cfapps.com")
	assert.Contains(t, ui.Outputs[3], "my-app, my-app2")

	assert.Contains(t, ui.Outputs[4], "hostname-3")
	assert.Contains(t, ui.Outputs[4], "another-example.com")
	assert.Contains(t, ui.Outputs[4], "july")
}

func TestListingRoutesWhenNoneExist(t *testing.T) {
	routes := []cf.Route{}
	routeRepo := &testapi.FakeRouteRepository{Routes: routes}

	ui := callListRoutes(t, []string{}, &testreq.FakeReqFactory{}, routeRepo)

	assert.Contains(t, ui.Outputs[0], "Getting routes")
	assert.Contains(t, ui.Outputs[0], "my-user")
	assert.Contains(t, ui.Outputs[1], "No routes found")
}

func TestListingRoutesWhenFindFails(t *testing.T) {
	routeRepo := &testapi.FakeRouteRepository{ListErr: true}

	ui := callListRoutes(t, []string{}, &testreq.FakeReqFactory{}, routeRepo)

	assert.Contains(t, ui.Outputs[0], "Getting routes")
	assert.Contains(t, ui.Outputs[1], "FAILED")
}

func callListRoutes(t *testing.T, args []string, reqFactory *testreq.FakeReqFactory, routeRepo *testapi.FakeRouteRepository) (ui *testterm.FakeUI) {

	ui = &testterm.FakeUI{}

	ctxt := testcmd.NewContext("routes", args)

	token, err := testconfig.CreateAccessTokenWithTokenInfo(configuration.TokenInfo{
		Username: "my-user",
	})
	assert.NoError(t, err)
	space := cf.SpaceFields{}
	space.Name = "my-space"
	org := cf.OrganizationFields{}
	org.Name = "my-org"
{'fraction_non_alphanumeric': 0.17163009404388715, 'fraction_numerical': 0.018808777429467086, 'mean_word_length': 5.432459677419355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5514282', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2424, 'n_words': 427}