Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Encho Belezirev authored and enchobelezirev committed Mar 14, 2018
1 parent f3be629 commit ab5b2b1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 66 deletions.
21 changes: 21 additions & 0 deletions clients/cfrestclient/fakes/fake_cloud_foundry_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fakes

import (
"github.com/SAP/cf-mta-plugin/clients/cfrestclient"
"github.com/SAP/cf-mta-plugin/clients/models"
)

// TODO: use counterfeiter if the client becomes more suffisticated

type FakeCloudFoundryClient struct {
domains []models.SharedDomain
err error
}

func NewFakeCloudFoundryClient(domains []models.SharedDomain, err error) cfrestclient.CloudFoundryOperationsExtended {
return FakeCloudFoundryClient{domains: domains, err: err}
}

func (f FakeCloudFoundryClient) GetSharedDomains() ([]models.SharedDomain, error) {
return f.domains, f.err
}
2 changes: 1 addition & 1 deletion clients/cfrestclient/rest_cloud_foundry_client_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c CloudFoundryRestClient) getSharedDomainsInternal(cloudFoundryUrlElements
func getPathQueryElements(response *models.CloudFoundryResponse) (CloudFoundryUrlElements, error) {
nextUrl, err := url.Parse(response.NextURL)
if err != nil {
return CloudFoundryUrlElements{}, fmt.Errorf("Could not parse next_url for getting shared domains: ", response.NextURL)
return CloudFoundryUrlElements{}, fmt.Errorf("Could not parse next_url for getting shared domains: %s", response.NextURL)
}
nextUrlQuery := nextUrl.Query()
page := nextUrlQuery.Get("page")
Expand Down
18 changes: 14 additions & 4 deletions commands/base_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ type BaseCommand struct {
// Initialize initializes the command with the specified name and CLI connection
func (c *BaseCommand) Initialize(name string, cliConnection plugin.CliConnection) {
log.Tracef("Initializing command '%s'\n", name)
c.InitializeAll(name, cliConnection, newTransport(), newCookieJar(), clients.NewDefaultClientFactory(), NewDefaultTokenFactory(cliConnection), nil)
transport := newTransport()
jar := newCookieJar()
tokenFactory := NewDefaultTokenFactory(cliConnection)
cloudFoundryClient := cfrestclient.NewCloudFoundryRestClient(getApiEndpoint(cliConnection), transport, jar, tokenFactory)
c.InitializeAll(name, cliConnection, transport, jar, clients.NewDefaultClientFactory(), tokenFactory, util.NewDeployServiceURLCalculator(cloudFoundryClient))
}

// InitializeAll initializes the command with the specified name, CLI connection, transport and cookie jar.
Expand All @@ -69,12 +73,18 @@ func (c *BaseCommand) InitializeAll(name string, cliConnection plugin.CliConnect
c.jar = jar
c.clientFactory = clientFactory
c.tokenFactory = tokenFactory
api, _ := cliConnection.ApiEndpoint()
c.deployServiceURLCalculator = deployServiceURLCalculator
}

func getApiEndpoint(cliConnection plugin.CliConnection) string {
api, err := cliConnection.ApiEndpoint()
if err != nil {
return ""
}
if strings.HasPrefix(api, "https://") {
api = strings.Replace(api, "https://", "", -1)
fmt.Println("Novoto api " + api)
}
c.deployServiceURLCalculator = util.NewDeployServiceURLCalculator(cliConnection, cfrestclient.NewCloudFoundryRestClient(api, transport, jar, tokenFactory))
return api
}

// Usage reports incorrect command usage
Expand Down
18 changes: 6 additions & 12 deletions util/deploy_service_url_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

cfrestclient "github.com/SAP/cf-mta-plugin/clients/cfrestclient"
"github.com/SAP/cf-mta-plugin/clients/models"
"github.com/cloudfoundry/cli/plugin"
)

const deployServiceHost = "deploy-service"
Expand All @@ -17,27 +16,22 @@ type DeployServiceURLCalculator interface {
}

type deployServiceURLCalculatorImpl struct {
// TODO: remove the cliConnection dependency
cliConnection plugin.CliConnection
cloudFoundryClient cfrestclient.CloudFoundryOperationsExtended
httpGetExecutor HttpSimpleGetExecutor
}

func NewDeployServiceURLCalculator(cliConnection plugin.CliConnection, cloudFoundryClient cfrestclient.CloudFoundryOperationsExtended) DeployServiceURLCalculator {
return deployServiceURLCalculatorImpl{cliConnection: cliConnection, cloudFoundryClient: cloudFoundryClient, httpGetExecutor: NewSimpleGetExecutor()}
func NewDeployServiceURLCalculator(cloudFoundryClient cfrestclient.CloudFoundryOperationsExtended) DeployServiceURLCalculator {
return deployServiceURLCalculatorImpl{cloudFoundryClient: cloudFoundryClient, httpGetExecutor: NewSimpleGetExecutor()}
}

func NewDeployServiceURLCalculatorWithHttpExecutor(cliConnection plugin.CliConnection, cloudFoundryClient cfrestclient.CloudFoundryOperationsExtended, httpGetExecutor HttpSimpleGetExecutor) DeployServiceURLCalculator {
return deployServiceURLCalculatorImpl{cliConnection: cliConnection, cloudFoundryClient: cloudFoundryClient, httpGetExecutor: httpGetExecutor}
func NewDeployServiceURLCalculatorWithHttpExecutor(cloudFoundryClient cfrestclient.CloudFoundryOperationsExtended, httpGetExecutor HttpSimpleGetExecutor) DeployServiceURLCalculator {
return deployServiceURLCalculatorImpl{cloudFoundryClient: cloudFoundryClient, httpGetExecutor: httpGetExecutor}
}

func (c deployServiceURLCalculatorImpl) ComputeDeployServiceURL() (string, error) {
result, err := c.cloudFoundryClient.GetSharedDomains()
if err != nil {
fmt.Printf("Maikooo err happened " + err.Error())
}
for _, domain := range result {
fmt.Println(domain.Guid + " " + domain.Name + " " + domain.Url)
return "", err
}

sharedDomain, err := c.findSharedDomain(result)
Expand All @@ -54,7 +48,7 @@ func (c deployServiceURLCalculatorImpl) findSharedDomain(domains []models.Shared
return domain, nil
}
}
return models.SharedDomain{}, fmt.Errorf("Could not find any shared domains in space: %s //TODO:/./")
return models.SharedDomain{}, fmt.Errorf("Could not find any shared domains")
}

func (c deployServiceURLCalculatorImpl) isCorrectDomain(domainName string) bool {
Expand Down
62 changes: 13 additions & 49 deletions util/deploy_service_url_calculator_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package util_test

import (
cli_fakes "github.com/SAP/cf-mta-plugin/cli/fakes"
cfrestclient_fakes "github.com/SAP/cf-mta-plugin/clients/cfrestclient/fakes"
"github.com/SAP/cf-mta-plugin/clients/models"
"github.com/SAP/cf-mta-plugin/util"
fakes "github.com/SAP/cf-mta-plugin/util/fakes"
plugin_models "github.com/cloudfoundry/cli/plugin/models"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -17,66 +17,30 @@ var _ = Describe("DeployServiceURLCalculator", func() {

Context("when a space is targeted and there is one shared domain", func() {
It("should return a URL constructed based on the first shared domain", func() {
cliConnection := cli_fakes.NewFakeCliConnectionBuilder().
CurrentSpace("", spaceName, nil).
GetSpace(spaceName, plugin_models.GetSpace_Model{
GetSpaces_Model: plugin_models.GetSpaces_Model{
Name: spaceName,
},
Domains: []plugin_models.GetSpace_Domains{
plugin_models.GetSpace_Domains{Name: "custom.test.ondemand.com", Shared: false},
plugin_models.GetSpace_Domains{Name: "test.ondemand.com", Shared: true},
},
}, nil).Build()
domains := []models.SharedDomain{
models.SharedDomain{Name: "test.ondemand.com"},
}
fakeHttpExecutor := fakes.NewFakeHttpGetExecutor(200, nil)
deployServiceURLCalculator := util.NewDeployServiceURLCalculatorWithHttpExecutor(cliConnection, nil, fakeHttpExecutor)
deployServiceURLCalculator := util.NewDeployServiceURLCalculatorWithHttpExecutor(cfrestclient_fakes.NewFakeCloudFoundryClient(domains, nil), fakeHttpExecutor)
Expect(deployServiceURLCalculator.ComputeDeployServiceURL()).To(Equal("deploy-service.test.ondemand.com"))
})
})
Context("when a space is targeted and there are two shared domains", func() {
It("should return a URL constructed based on the first shared domain", func() {
cliConnection := cli_fakes.NewFakeCliConnectionBuilder().
CurrentSpace("", spaceName, nil).
GetSpace(spaceName, plugin_models.GetSpace_Model{
GetSpaces_Model: plugin_models.GetSpaces_Model{
Name: spaceName,
},
Domains: []plugin_models.GetSpace_Domains{
plugin_models.GetSpace_Domains{Name: "custom.test.ondemand.com", Shared: false},
plugin_models.GetSpace_Domains{Name: "test1.ondemand.com", Shared: true},
plugin_models.GetSpace_Domains{Name: "test2.ondemand.com", Shared: true},
},
}, nil).Build()
domains := []models.SharedDomain{
models.SharedDomain{Name: "test1.ondemand.com"},
models.SharedDomain{Name: "test2.ondemand.com"},
}
fakeHttpExecutor := fakes.NewFakeHttpGetExecutor(200, nil)
deployServiceURLCalculator := util.NewDeployServiceURLCalculatorWithHttpExecutor(cliConnection, nil, fakeHttpExecutor)
deployServiceURLCalculator := util.NewDeployServiceURLCalculatorWithHttpExecutor(cfrestclient_fakes.NewFakeCloudFoundryClient(domains, nil), fakeHttpExecutor)
Expect(deployServiceURLCalculator.ComputeDeployServiceURL()).To(Equal("deploy-service.test1.ondemand.com"))
})
})
Context("when a space is targeted and there are no shared domains", func() {
It("should return an error", func() {
cliConnection := cli_fakes.NewFakeCliConnectionBuilder().
CurrentSpace("", spaceName, nil).
GetSpace(spaceName, plugin_models.GetSpace_Model{
GetSpaces_Model: plugin_models.GetSpaces_Model{
Name: spaceName,
},
Domains: []plugin_models.GetSpace_Domains{
plugin_models.GetSpace_Domains{Name: "custom.test.ondemand.com", Shared: false},
},
}, nil).Build()
deployServiceURLCalculator := util.NewDeployServiceURLCalculator(cliConnection, nil)
deployServiceURLCalculator := util.NewDeployServiceURLCalculator(cfrestclient_fakes.NewFakeCloudFoundryClient([]models.SharedDomain{}, nil))
_, err := deployServiceURLCalculator.ComputeDeployServiceURL()
Expect(err).Should(MatchError("Could not find any shared domains in space: " + spaceName))
})
})
Context("when no space is targeted", func() {
It("should return an error", func() {
cliConnection := cli_fakes.NewFakeCliConnectionBuilder().
CurrentSpace("", "", nil).
Build()
deployServiceURLCalculator := util.NewDeployServiceURLCalculator(cliConnection, nil)
_, err := deployServiceURLCalculator.ComputeDeployServiceURL()
Expect(err).Should(MatchError("No space targeted, use 'cf target -s SPACE' to target a space."))
Expect(err).Should(MatchError("Could not find any shared domains"))
})
})
})
Expand Down

0 comments on commit ab5b2b1

Please sign in to comment.