Skip to content

Commit

Permalink
Merge pull request #3 from datadrivers/feature/script
Browse files Browse the repository at this point in the history
Add script implementation
  • Loading branch information
Nosmoht authored Jan 31, 2020
2 parents c34127e + 2f5f51c commit 1f1bfad
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ type Client interface {
UserUpdate(string, User) error
UserDelete(string) error
UserChangePassword(string, string) error
ScriptLists() (*[]Script, error)
ScriptRead(string) (*Script, error)
ScriptCreate(*Script) error
ScriptUpdate(*Script) error
ScriptDelete(string) error
ScriptRun(string) error
}

type client struct {
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/datadrivers/go-nexus-client

go 1.13

require github.com/stretchr/testify v1.4.0
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
110 changes: 110 additions & 0 deletions script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package client

import (
"encoding/json"
"fmt"
"net/http"
)

const (
scriptsAPIEndpoint = "service/rest/v1/script"
)

// Script describe a groovy script object that can be run on the nexus server
type Script struct {
Name string `json:"name"`
Content string `json:"content"`
Type string `json:"type"`
}

func (c *client) ScriptLists() (*[]Script, error) {
body, resp, err := c.Get(scriptsAPIEndpoint, nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s", string(body))
}

var scripts []Script
if err := json.Unmarshal(body, &scripts); err != nil {
return nil, fmt.Errorf("could not unmarschal scripts: %v", err)
}
return &scripts, nil
}

func (c *client) ScriptRead(name string) (*Script, error) {
body, resp, err := c.Get(fmt.Sprintf("%s/%s", scriptsAPIEndpoint, name), nil)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s", string(body))
}
var script Script
if err := json.Unmarshal(body, &script); err != nil {
return nil, fmt.Errorf("could not unmarschal scripts: %v", err)
}
return &script, nil
}

func (c *client) ScriptCreate(script *Script) error {
ioReader, err := jsonMarshalInterfaceToIOReader(script)
if err != nil {
return err
}
body, resp, err := c.Post(scriptsAPIEndpoint, ioReader)
if err != nil {
return err
}

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s", string(body))
}

return nil
}

func (c *client) ScriptUpdate(script *Script) error {
ioReader, err := jsonMarshalInterfaceToIOReader(script)
if err != nil {
return err
}

body, resp, err := c.Put(fmt.Sprintf("%s/%s", scriptsAPIEndpoint, script.Name), ioReader)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s", string(body))
}

return nil
}

func (c *client) ScriptDelete(name string) error {
body, resp, err := c.Delete(fmt.Sprintf("%s/%s", scriptsAPIEndpoint, name))
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s", string(body))
}
return err
}

func (c *client) ScriptRun(name string) error {
body, resp, err := c.Post(fmt.Sprintf("%s/%s/run", scriptsAPIEndpoint, name), nil)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s", string(body))
}
return err
}
200 changes: 200 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package client

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

const (
testScriptsAPIEndpoint = "/service/rest/v1/script"
)

func testScript(name string) *Script {
return &Script{
Name: name,
Content: fmt.Sprintf("log.info('Hello, %s!')", name),
Type: "groovy",
}
}

func testHTTPHeader(t *testing.T, method string, path string, req *http.Request) {
assert.Equal(t, req.Method, method)
assert.Equal(t, path, req.URL.String())
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
user, pass, ok := req.BasicAuth()
assert.Equal(t, "admin", user)
assert.Equal(t, "admin123", pass)
assert.True(t, ok)
}

func TestScriptList(t *testing.T) {
// Start a local HTTP server
testserver := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
testHTTPHeader(t, "GET", testScriptsAPIEndpoint, req)

// Send response
rw.Write([]byte(`[ {
"name" : "helloWorld",
"content" : "log.info('Hello, World!')",
"type" : "groovy"
},
{
"name" : "HelloTest",
"content" : "log.info('Hello, Test!')",
"type" : "groovy"
}]`))
}))
// Close the server when test finishes
defer testserver.Close()

client := NewClient(Config{
URL: testserver.URL,
Username: "admin",
Password: "admin123",
})
scripts, err := client.ScriptLists()
assert.Nil(t, err)
expectedScripts := []Script{
Script{
Name: "helloWorld",
Content: "log.info('Hello, World!')",
Type: "groovy",
},
Script{
Name: "HelloTest",
Content: "log.info('Hello, Test!')",
Type: "groovy",
},
}
assert.Equal(t, &expectedScripts, scripts)
}

func TestScriptRead(t *testing.T) {
// Start a local HTTP server
testserver := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
testHTTPHeader(t, "GET", fmt.Sprintf("%s/helloWorld", testScriptsAPIEndpoint), req)

// Send response
rw.Write([]byte(`{
"name" : "helloWorld",
"content" : "log.info('Hello, World!')",
"type" : "groovy"
}`))
}))
// Close the server when test finishes
defer testserver.Close()

client := NewClient(Config{
URL: testserver.URL,
Username: "admin",
Password: "admin123",
})
script, err := client.ScriptRead("helloWorld")
assert.Nil(t, err)
expectedScript := Script{
Name: "helloWorld",
Content: "log.info('Hello, World!')",
Type: "groovy",
}
assert.Equal(t, &expectedScript, script)
}

func TestScriptCreate(t *testing.T) {
// Start a local HTTP server
testserver := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
testHTTPHeader(t, "POST", testScriptsAPIEndpoint, req)

expectedBody := `{"name":"test-script-create","content":"log.info('Hello, test-script-create!')","type":"groovy"}`
bodyBytes, err := ioutil.ReadAll(req.Body)
assert.Nil(t, err)
assert.Equal(t, expectedBody, string(bodyBytes))

// Send response
rw.WriteHeader(204)
}))
// Close the server when test finishes
defer testserver.Close()

client := NewClient(Config{
URL: testserver.URL,
Username: "admin",
Password: "admin123",
})
testScript := testScript("test-script-create")
err := client.ScriptCreate(testScript)
assert.Nil(t, err)
}

func TestScriptUpdate(t *testing.T) {
// Start a local HTTP server
testserver := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
testHTTPHeader(t, "PUT", fmt.Sprintf("%s/test-script-update", testScriptsAPIEndpoint), req)

expectedBody := `{"name":"test-script-update","content":"log.info('Hello, test-script-update!')","type":"groovy"}`
bodyBytes, err := ioutil.ReadAll(req.Body)
assert.Nil(t, err)
assert.Equal(t, expectedBody, string(bodyBytes))

// Send response
rw.WriteHeader(204)
}))
// Close the server when test finishes
defer testserver.Close()

client := NewClient(Config{
URL: testserver.URL,
Username: "admin",
Password: "admin123",
})
testScript := testScript("test-script-update")
err := client.ScriptUpdate(testScript)
assert.Nil(t, err)
}

func TestScriptRUN(t *testing.T) {
// Start a local HTTP server
testserver := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
testHTTPHeader(t, "POST", fmt.Sprintf("%s/test-script-update/run", testScriptsAPIEndpoint), req)

// Send response
rw.Write([]byte(`{
"name" : "test-script-update",
"result" : "null"
}`))
}))
// Close the server when test finishes
defer testserver.Close()

client := NewClient(Config{
URL: testserver.URL,
Username: "admin",
Password: "admin123",
})
err := client.ScriptRun("test-script-update")
assert.Nil(t, err)
}

func TestScriptDelete(t *testing.T) {
// Start a local HTTP server
testserver := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
testHTTPHeader(t, "DELETE", fmt.Sprintf("%s/test-script-delete", testScriptsAPIEndpoint), req)

// Send response
rw.WriteHeader(204)
}))
// Close the server when test finishes
defer testserver.Close()

client := NewClient(Config{
URL: testserver.URL,
Username: "admin",
Password: "admin123",
})
err := client.ScriptDelete("test-script-delete")
assert.Nil(t, err)
}

0 comments on commit 1f1bfad

Please sign in to comment.