Skip to content

Commit

Permalink
Add logic to join given parameters and default parameters in manipulator
Browse files Browse the repository at this point in the history
  • Loading branch information
baskarap committed Sep 18, 2019
1 parent 2826222 commit abca2dd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/service/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Dependencies struct {
// Currently, it supports only one Manipulator
func NewDependencies() *Dependencies {
s := config.DataSource()
deps := &Dependencies{Manipulator: NewManipulator(native.NewBildProcessor(), []string{})}
deps := &Dependencies{Manipulator: NewManipulator(native.NewBildProcessor(), map[string]string{})}
if regex.WebFolderMatcher.MatchString(s.Kind) {
deps.Storage = NewWebFolderStorage(s.Value.(config.WebFolder), s.HystrixCommand)
} else if regex.S3Matcher.MatchString(s.Kind) {
Expand Down
20 changes: 18 additions & 2 deletions pkg/service/manipulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ type Manipulator interface {

type manipulator struct {
processor processor.Processor
defaultParams []string
defaultParams map[string]string
}

// Process takes ProcessSpec as an argument and returns []byte, error
// This manipulator uses bild to do the actual image manipulations
func (m *manipulator) Process(spec processSpec) ([]byte, error) {
params := spec.Params
params = joinParams(params, m.defaultParams)
var err error
t := time.Now()
data, f, err := m.processor.Decode(spec.ImageData)
Expand Down Expand Up @@ -118,6 +119,21 @@ func (m *manipulator) Process(spec processSpec) ([]byte, error) {
return src, err
}

func joinParams(params map[string]string, defaultParams map[string]string) map[string]string {
fp := make(map[string]string)
for p := range defaultParams {
fp[p] = defaultParams[p]
}
for p := range params {
if fp[p] != "" {
fp[p] = fmt.Sprintf("%s,%s", defaultParams[p], params[p])
} else {
fp[p] = params[p]
}
}
return fp
}

// CleanInt takes a string and return an int not greater than 9999
func CleanInt(input string) int {
val, _ := strconv.Atoi(input)
Expand Down Expand Up @@ -173,7 +189,7 @@ func trackDuration(name string, start time.Time, spec processSpec) *metrics.Upda
}

// NewManipulator takes in a Processor interface and returns a new Manipulator
func NewManipulator(processor processor.Processor, defaultParams []string) Manipulator {
func NewManipulator(processor processor.Processor, defaultParams map[string]string) Manipulator {
return &manipulator{
processor: processor,
defaultParams: defaultParams,
Expand Down
48 changes: 43 additions & 5 deletions pkg/service/manipulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ import (
"testing"
"time"

"github.com/gojek/darkroom/pkg/config"

"github.com/gojek/darkroom/pkg/processor"
"github.com/gojek/darkroom/pkg/processor/native"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestNewManipulator(t *testing.T) {
m := NewManipulator(native.NewBildProcessor(), []string{})
m := NewManipulator(native.NewBildProcessor(), map[string]string{})
assert.NotNil(t, m)
}

// Integration test to verify the flow of WebP image is requested without having support of WebP on client's side
func TestManipulator_Process_ReturnsImageAsPNGIfCallerDoesNOTSupportWebP(t *testing.T) {
// Use real processor to ensure that right encoder is being used
p := native.NewBildProcessor()
m := NewManipulator(p, []string{})
m := NewManipulator(p, map[string]string{})

img, _ := ioutil.ReadFile("../processor/native/_testdata/test.webp")
expectedImg, _ := ioutil.ReadFile("../processor/native/_testdata/test_webp_to_png.png")
Expand All @@ -41,7 +43,7 @@ func TestManipulator_Process_ReturnsImageAsPNGIfCallerDoesNOTSupportWebP(t *test
func TestManipulator_Process_ReturnsImageAsWebPIfCallerSupportsWebP(t *testing.T) {
// Use real processor to ensure that right encoder is being used
p := native.NewBildProcessor()
m := NewManipulator(p, []string{})
m := NewManipulator(p, map[string]string{})

img, _ := ioutil.ReadFile("../processor/native/_testdata/test.png")
expectedImg, _ := ioutil.ReadFile("../processor/native/_testdata/test_png_to_webp.webp")
Expand All @@ -58,7 +60,7 @@ func TestManipulator_Process_ReturnsImageAsWebPIfCallerSupportsWebP(t *testing.T

func TestManipulator_Process(t *testing.T) {
mp := &mockProcessor{}
m := NewManipulator(mp, []string{})
m := NewManipulator(mp, map[string]string{})
params := make(map[string]string)

input := []byte("inputData")
Expand All @@ -71,7 +73,7 @@ func TestManipulator_Process(t *testing.T) {

// Create new struct for asserting expectations
mp = &mockProcessor{}
m = NewManipulator(mp, []string{})
m = NewManipulator(mp, map[string]string{})
mp.On("Decode", input).Return(decoded, "png", nil)
mp.On("Encode", decoded, "png").Return(input, nil)
mp.On("Crop", decoded, 100, 100, processor.CropCenter).Return(decoded, nil)
Expand Down Expand Up @@ -117,6 +119,42 @@ func TestManipulator_Process(t *testing.T) {
mp.AssertExpectations(t)
}

func TestGetParams(t *testing.T) {
cases := []struct {
params map[string]string
defaultParams map[string]string
expectedRes map[string]string
}{
{
params: map[string]string{"foo": "bar"},
defaultParams: map[string]string{"bar": "foo"},
expectedRes: map[string]string{"foo": "bar", "bar": "foo"},
},
{
params: map[string]string{},
defaultParams: map[string]string{"bar": "foo"},
expectedRes: map[string]string{"bar": "foo"},
},
{
params: map[string]string{"foo": "bar"},
defaultParams: map[string]string{"foo": "foo"},
expectedRes: map[string]string{"foo": "foo,bar"},
},
{
params: map[string]string{"foo": "bar"},
defaultParams: map[string]string{},
expectedRes: map[string]string{"foo": "bar"},
},
}
for _, c := range cases {
v := config.Viper()
v.Set("defaultParams", c.defaultParams)
config.Update()

assert.Equal(t, c.expectedRes, joinParams(c.params, c.defaultParams))
}
}

func TestGetCropPoint(t *testing.T) {
assert.Equal(t, processor.CropCenter, GetCropPoint(""))
assert.Equal(t, processor.CropTop, GetCropPoint("top"))
Expand Down

0 comments on commit abca2dd

Please sign in to comment.