Skip to content

Commit

Permalink
refactors out Is response, renames NewXXXBuilder() functions to XXX()…
Browse files Browse the repository at this point in the history
… for sake of brevity
  • Loading branch information
alperkose committed Jan 31, 2016
1 parent 782b9ac commit a4e65bf
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 205 deletions.
6 changes: 3 additions & 3 deletions builders/imposters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ var _ = Describe("Imposter Builder Tests", func() {

BeforeEach(func() {
once.Do(func() {
response := responses.NewResponseBuilder().IsResponse().StatusCode(200).Body("{}").Build()
response := responses.Is().StatusCode(200).Body("{}").Build()

equals := predicates.NewEqualsBuilder().Path("/test-path").Build()
contains := predicates.NewContainsBuilder().Header("Content-Type", "application/json").Build()
equals := predicates.Equals().Path("/test-path").Build()
contains := predicates.Contains().Header("Content-Type", "application/json").Build()

stub := NewStubBuilder().Responses(response).Predicates(equals, contains).Build()

Expand Down
84 changes: 25 additions & 59 deletions builders/stub_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package builders_test

import (
"encoding/json"
"log"

. "github.com/durmaze/gobank/builders"
"github.com/durmaze/gobank/predicates"
. "github.com/durmaze/gobank/responses"
"github.com/durmaze/gobank/responses"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -18,41 +15,30 @@ var _ = Describe("Stub Builder Tests", func() {

Describe("When building a Stub with single Response", func() {
var (
actualResponse Response
expectedResponse Response
once sync.Once
stub Stub
once sync.Once
)

BeforeEach(func() {
once.Do(func() {

expectedResponse = Response{
Is: Is{
StatusCode: 200,
Body: "{ \"greeting\": \"Hello GoBank\" }",
},
}

stub := NewStubBuilder().Responses(expectedResponse).Build()
expectedResponse := responses.Is().StatusCode(200).Body("{ \"greeting\": \"Hello GoBank\" }").Build()
stub = NewStubBuilder().Responses(expectedResponse).Build()

actualResponse = stub.Responses[0]
})
})

It("should create a Stub that returns a Response with the correct StatusCode", func() {
Expect(actualResponse.Is.StatusCode).To(Equal(expectedResponse.Is.StatusCode))
It("should create a Stub that has one Predicate", func() {
Expect(stub.Responses).To(HaveLen(1))
})

It("should create a Stub that returns a Response with the correct Body", func() {
Expect(actualResponse.Is.Body).To(Equal(expectedResponse.Is.Body))
It("should create a Stub that has a Predicate with type \"Equals\"", func() {
Expect(stub.Responses[0].Type()).To(Equal(responses.Is().Build().Type()))
})
})

Describe("When building a Stub with single Response and a single Equals predicate", func() {
var (
actualResponse Response
expectedResponse Response

stub Stub

once sync.Once
Expand All @@ -61,87 +47,67 @@ var _ = Describe("Stub Builder Tests", func() {
BeforeEach(func() {
once.Do(func() {

expectedResponse = Response{
Is: Is{
StatusCode: 200,
Body: "{ \"greeting\": \"Hello GoBank\" }",
},
}
expectedPredicate := predicates.NewEqualsBuilder().Path("/test-path").Build()
expectedResponse := responses.Is().StatusCode(200).Body("{ \"greeting\": \"Hello GoBank\" }").Build()
expectedPredicate := predicates.Equals().Path("/test-path").Build()

stub = NewStubBuilder().Responses(expectedResponse).Predicates(expectedPredicate).Build()

actualResponse = stub.Responses[0]

stubsJSON, _ := json.Marshal(stub)
log.Println("stub : ", string(stubsJSON))
})
})

It("should create a Stub that returns a Response with the correct StatusCode", func() {
Expect(actualResponse.Is.StatusCode).To(Equal(expectedResponse.Is.StatusCode))
It("should create a Stub that has one Predicate", func() {
Expect(stub.Responses).To(HaveLen(1))
})

It("should create a Stub that returns a Response with the correct Body", func() {
Expect(actualResponse.Is.Body).To(Equal(expectedResponse.Is.Body))
It("should create a Stub that has a Predicate with type \"Equals\"", func() {
Expect(stub.Responses[0].Type()).To(Equal(responses.Is().Build().Type()))
})

It("should create a Stub that has one Predicate", func() {
Expect(stub.Predicates).To(HaveLen(1))
})

It("should create a Stub that has a Predicate with type \"Equals\"", func() {
Expect(stub.Predicates[0].Type()).To(Equal(predicates.Equals{}.Type()))
Expect(stub.Predicates[0].Type()).To(Equal(predicates.Equals().Build().Type()))
})
})

Describe("When building a Stub with single Response and multiple different predicates", func() {
var (
actualResponse Response
expectedResponse Response

stub Stub
once sync.Once
)

BeforeEach(func() {
once.Do(func() {

expectedResponse = Response{
Is: Is{
StatusCode: 200,
Body: "{ \"greeting\": \"Hello GoBank\" }",
},
}
expectedResponse := responses.Is().StatusCode(200).Body("{ \"greeting\": \"Hello GoBank\" }").Build()

expectedPredicate1 := predicates.NewEqualsBuilder().Path("/test-path").Build()

expectedPredicate2 := predicates.NewContainsBuilder().Method("POST").Build()
expectedPredicate1 := predicates.Equals().Path("/test-path").Build()
expectedPredicate2 := predicates.Contains().Method("POST").Build()

stub = NewStubBuilder().Responses(expectedResponse).Predicates(expectedPredicate1, expectedPredicate2).Build()

actualResponse = stub.Responses[0]
})
})

It("should create a Stub that returns a Response with the correct StatusCode", func() {
Expect(actualResponse.Is.StatusCode).To(Equal(expectedResponse.Is.StatusCode))
It("should create a Stub that has one Predicate", func() {
Expect(stub.Responses).To(HaveLen(1))
})

It("should create a Stub that returns a Response with the correct Body", func() {
Expect(actualResponse.Is.Body).To(Equal(expectedResponse.Is.Body))
It("should create a Stub that has a Predicate with type \"Equals\"", func() {
Expect(stub.Responses[0].Type()).To(Equal(responses.Is().Build().Type()))
})

It("should create a Stub that has two Predicates", func() {
Expect(stub.Predicates).To(HaveLen(2))
})

It("should create a Stub that has a Predicate with type \"Equals\"", func() {
Expect(stub.Predicates[0].Type()).To(Equal(predicates.Equals{}.Type()))
Expect(stub.Predicates[0].Type()).To(Equal(predicates.Equals().Build().Type()))
})

It("should create a Stub that has a Predicate with type \"Contains\"", func() {
Expect(stub.Predicates[1].Type()).To(Equal(predicates.Contains{}.Type()))
Expect(stub.Predicates[1].Type()).To(Equal(predicates.Contains().Build().Type()))
})

})
Expand Down
22 changes: 9 additions & 13 deletions predicates/contains.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package predicates

import (
"encoding/json"
"log"
)
import "encoding/json"

type Contains struct {
type contains struct {
req Request
}

func (p Contains) Type() string {
return "Contains"
func (p contains) Type() string {
return "contains"
}

func (p Contains) MarshalJSON() ([]byte, error) {
log.Println("marshalling ")
func (p contains) MarshalJSON() ([]byte, error) {
requestBytes, _ := json.Marshal(p.req)

requestJson := string(requestBytes)
Expand All @@ -24,11 +20,11 @@ func (p Contains) MarshalJSON() ([]byte, error) {
}

type ContainsBuilder struct {
contains Contains
contains contains
}

func NewContainsBuilder() *ContainsBuilder {
return &ContainsBuilder{contains: Contains{req: Request{}}}
func Contains() *ContainsBuilder {
return &ContainsBuilder{contains: contains{req: Request{}}}
}

func (builder *ContainsBuilder) Path(path string) *ContainsBuilder {
Expand Down Expand Up @@ -63,6 +59,6 @@ func (builder *ContainsBuilder) Body(body string) *ContainsBuilder {
return builder
}

func (builder *ContainsBuilder) Build() Contains {
func (builder *ContainsBuilder) Build() contains {
return builder.contains
}
5 changes: 1 addition & 4 deletions predicates/contains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("Predicate Builder Tests", func() {

BeforeEach(func() {
once.Do(func() {
actualPredicate := predicates.NewContainsBuilder().
actualPredicate := predicates.Contains().
Path(expectedPath).
Method(expectedMethod).
Header(expectedHeader, expectedHeaderValue).
Expand All @@ -41,9 +41,6 @@ var _ = Describe("Predicate Builder Tests", func() {
jsonBytes, _ := json.Marshal(actualPredicate)
actualPredicateAsMap = map[string]interface{}{}
json.Unmarshal(jsonBytes, &actualPredicateAsMap)

// marshalResult := NewMarshaller(actualPredicate).ToJson()
// actualPredicateAsMap = marshalResult.ToMap()
})
})

Expand Down
22 changes: 9 additions & 13 deletions predicates/equals.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package predicates

import (
"encoding/json"
"log"
)
import "encoding/json"

type Equals struct {
type equals struct {
req Request
}

func (p Equals) Type() string {
return "Equals"
func (p equals) Type() string {
return "equals"
}

func (p Equals) MarshalJSON() ([]byte, error) {
log.Println("marshalling ")
func (p equals) MarshalJSON() ([]byte, error) {
requestBytes, _ := json.Marshal(p.req)

requestJson := string(requestBytes)
Expand All @@ -24,11 +20,11 @@ func (p Equals) MarshalJSON() ([]byte, error) {
}

type EqualsBuilder struct {
equals Equals
equals equals
}

func NewEqualsBuilder() *EqualsBuilder {
return &EqualsBuilder{equals: Equals{req: Request{}}}
func Equals() *EqualsBuilder {
return &EqualsBuilder{equals: equals{req: Request{}}}
}

func (builder *EqualsBuilder) Path(path string) *EqualsBuilder {
Expand Down Expand Up @@ -63,6 +59,6 @@ func (builder *EqualsBuilder) Body(body string) *EqualsBuilder {
return builder
}

func (builder *EqualsBuilder) Build() Equals {
func (builder *EqualsBuilder) Build() equals {
return builder.equals
}
2 changes: 1 addition & 1 deletion predicates/equals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("Equals Predicate Builder Tests", func() {

BeforeEach(func() {
once.Do(func() {
actualPredicate := predicates.NewEqualsBuilder().
actualPredicate := predicates.Equals().
Path(expectedPath).
Method(expectedMethod).
Header(expectedHeader, expectedHeaderValue).
Expand Down
12 changes: 6 additions & 6 deletions predicates/or.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package predicates

type Or struct {
type or struct {
Predicates []Predicate `json:"or"`
}

type OrBuilder struct {
or Or
or or
}

func (p Or) Type() string {
return "Or"
func (p or) Type() string {
return "or"
}

func NewOrBuilder() *OrBuilder {
return &OrBuilder{or: Or{}}
return &OrBuilder{or: or{}}
}

func (builder *OrBuilder) AddPredicate(predicate Predicate) *OrBuilder {
Expand All @@ -22,6 +22,6 @@ func (builder *OrBuilder) AddPredicate(predicate Predicate) *OrBuilder {
return builder
}

func (builder *OrBuilder) Build() Or {
func (builder *OrBuilder) Build() or {
return builder.or
}
6 changes: 3 additions & 3 deletions predicates/or_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var _ = Describe("Or Predicate Builder Tests", func() {

var (
actualPredicateAsMap map[string]interface{}
predicate1 = predicates.NewEqualsBuilder().Build()
predicate2 = predicates.NewContainsBuilder().Build()
predicate3 = predicates.NewEqualsBuilder().Build()
predicate1 = predicates.Equals().Build()
predicate2 = predicates.Contains().Build()
predicate3 = predicates.Equals().Build()

once sync.Once
)
Expand Down
Loading

0 comments on commit a4e65bf

Please sign in to comment.