Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 75 additions & 32 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package context

import (
"encoding/json"
"io/ioutil"
"strconv"
)

Expand All @@ -16,79 +17,121 @@ type Context interface {
GetBool(k string) (bool, bool)
MarshalJSON() ([]byte, error)
UnmarshalJSON(b []byte) error
Clone() contextMap
Clone() Context
SetUsers(userpath string) error
}

type contextMap map[string]interface{}
type user []string
type contextMap struct {
c map[string]interface{}
users []user
index int
}

func New() Context {
var c contextMap = make(contextMap)
var c = contextMap{
c: make(map[string]interface{}),
users: []user{},
index: 0,
}
return &c
}

func (c contextMap) PutString(k string, v string) {
c[k] = v
func (c *contextMap) PutString(k string, v string) {
c.c[k] = v
}

func (c contextMap) GetString(k string) (string, bool) {
if c[k] == nil {
func (c *contextMap) GetString(k string) (string, bool) {
if c.c[k] == nil {
return "", false
} else {
return c[k].(string), true
return c.c[k].(string), true
}
}

func (c contextMap) PutInt(k string, v int) {
func (c *contextMap) PutInt(k string, v int) {
// saves as string, avoids json.Marshal turning int into json.numbers(float64)
c[k] = strconv.Itoa(v)
c.c[k] = strconv.Itoa(v)
}

func (c contextMap) GetInt(k string) (int, bool) {
if c[k] == nil {
func (c *contextMap) GetInt(k string) (int, bool) {
if c.c[k] == nil {
return 0, false
} else {
value, _ := strconv.ParseInt(c[k].(string), 0, 0)
value, _ := strconv.ParseInt(c.c[k].(string), 0, 0)
return int(value), true
}
}

func (c contextMap) PutFloat64(k string, v float64) {
c[k] = v
func (c *contextMap) PutFloat64(k string, v float64) {
c.c[k] = v
}

func (c contextMap) GetFloat64(k string) (float64, bool) {
if c[k] == nil {
func (c *contextMap) GetFloat64(k string) (float64, bool) {
if c.c[k] == nil {
return 0, false
} else {
return c[k].(float64), true
return c.c[k].(float64), true
}
}

func (c contextMap) PutBool(k string, v bool) {
c[k] = v
func (c *contextMap) PutBool(k string, v bool) {
c.c[k] = v
}

func (c contextMap) GetBool(k string) (bool, bool) {
if c[k] == nil {
func (c *contextMap) GetBool(k string) (bool, bool) {
if c.c[k] == nil {
return false, false
} else {
return c[k].(bool), true
return c.c[k].(bool), true
}
}

func (c contextMap) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}(c))
func (c *contextMap) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}(c.c))
}

func (c contextMap) UnmarshalJSON(b []byte) error {
var m map[string]interface{} = c
func (c *contextMap) UnmarshalJSON(b []byte) error {
var m map[string]interface{} = c.c
return json.Unmarshal(b, &m)
}

func (c contextMap) Clone() contextMap {
var clone = make(contextMap)
for k, v := range c {
clone[k] = v
func (c *contextMap) Clone() Context {
var clone = contextMap{
c: make(map[string]interface{}),
}
return clone
for k, v := range c.c {
clone.c[k] = v
}

users := c.users
userslen := len(users)
if userslen > 0 {
curindex := c.index % userslen
clone.PutString("rest:username", users[curindex][0])
clone.PutString("rest:password", users[curindex][1])
c.index++
}
return &clone
}

func ParseUser(path string) ([]user, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

var users []user
err = json.Unmarshal(file, &users)
if err != nil {
return nil, err
}

return users, nil
}

func (c *contextMap) SetUsers(userpath string) error {
users, err := ParseUser(userpath)
c.users = users
return err
}
27 changes: 27 additions & 0 deletions context/context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package context_test

import (
"io/ioutil"

"github.com/cloudfoundry-incubator/pat/context"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -76,4 +78,29 @@ var _ = Describe("Context map", func() {
})
})

Context("When multiusers is set", func() {
JustBeforeEach(func() {
ioutil.WriteFile("/tmp/users.json", []byte(`[["user1", "password1"], ["user2", "password2"]]`), 0755)
err := localContext.SetUsers("/tmp/users.json")
Ω(err).ShouldNot(HaveOccurred())

})
Context("Cloning map many times", func() {
It("get user sequence and loop", func() {
cloned := localContext.Clone()
result, _ := cloned.GetString("rest:username")
Ω(result).Should(Equal("user1"))

cloned = localContext.Clone()
result, _ = cloned.GetString("rest:username")
Ω(result).Should(Equal("user2"))

cloned = localContext.Clone()
result, _ = cloned.GetString("rest:username")
Ω(result).Should(Equal("user1"))

})
})
})

})