Skip to content
Permalink
Browse files
add tests for Display
  • Loading branch information
geomacy committed Aug 9, 2019
1 parent 3f58ebd commit 9a5fa7d7ad653778b66778df4bbf6f005d392cec
Showing 28 changed files with 6,302 additions and 43 deletions.

Some generated files are not rendered by default. Learn more.

@@ -23,6 +23,10 @@
name = "github.com/urfave/cli"
version = "1.12.0"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.3.0"

[prune]
go-tests = true
unused-packages = true
@@ -19,15 +19,16 @@
package commands

import (
"errors"
"strings"

"github.com/apache/brooklyn-client/cli/api/catalog"
"github.com/apache/brooklyn-client/cli/command_metadata"
"github.com/apache/brooklyn-client/cli/error_handler"
"github.com/apache/brooklyn-client/cli/models"
"github.com/apache/brooklyn-client/cli/net"
"github.com/apache/brooklyn-client/cli/scope"
"github.com/urfave/cli"
"github.com/apache/brooklyn-client/cli/models"
"errors"
"strings"
)

type CatalogShow struct {
@@ -60,12 +61,12 @@ func (cmd *CatalogShow) Run(scope scope.Scope, c *cli.Context) {
}
}

func (cmd *CatalogShow) show(c *cli.Context) (error) {
func (cmd *CatalogShow) show(c *cli.Context) error {
if len(c.Args()) != 2 {
return errors.New(c.App.Name + " " + showCommandName + catalogItemTypesUsage + " ITEM[:VERSION]")
}
catalogType, err := GetCatalogType(c)
if err != nil {
if err != nil {
return err
}
item := c.Args().Get(1)
@@ -88,7 +89,7 @@ func (cmd *CatalogShow) show(c *cli.Context) (error) {
return errors.New("Unrecognised argument")
}

func (cmd *CatalogShow) showPolicy(c *cli.Context, item string, version string) (error) {
func (cmd *CatalogShow) showPolicy(c *cli.Context, item string, version string) error {
var summary models.CatalogItemSummary
var err error
if version == "" {
@@ -103,7 +104,7 @@ func (cmd *CatalogShow) showPolicy(c *cli.Context, item string, version string)
return summary.Display(c)
}

func (cmd *CatalogShow) showCatalogLocation(c *cli.Context, item string, version string) (error) {
func (cmd *CatalogShow) showCatalogLocation(c *cli.Context, item string, version string) error {
var summary models.CatalogItemSummary
var err error
if version == "" {
@@ -117,7 +118,7 @@ func (cmd *CatalogShow) showCatalogLocation(c *cli.Context, item string, version
return summary.Display(c)
}

func (cmd *CatalogShow) showCatalogEntity(c *cli.Context, item string, version string) (error) {
func (cmd *CatalogShow) showCatalogEntity(c *cli.Context, item string, version string) error {
var summary models.CatalogEntitySummary
var err error
if version == "" {
@@ -131,7 +132,7 @@ func (cmd *CatalogShow) showCatalogEntity(c *cli.Context, item string, version s
return summary.Display(c)
}

func (cmd *CatalogShow) showCatalogApplication(c *cli.Context, item string, version string) (error) {
func (cmd *CatalogShow) showCatalogApplication(c *cli.Context, item string, version string) error {
var summary models.CatalogEntitySummary
var err error
if version == "" {
@@ -0,0 +1,134 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package commands

import (
"encoding/json"
"fmt"
"testing"

"github.com/apache/brooklyn-client/cli/models"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)

func testCatalogEntitySummary() models.CatalogEntitySummary {
summary := models.CatalogEntitySummary{
CatalogItemSummary: models.CatalogItemSummary{
IdentityDetails: models.IdentityDetails{
Id: "server:1.0.0-SNAPSHOT",
Name: "Server",
Version: "1.0.0-SNAPSHOT",
},
JavaType: "org.apache.brooklyn.entity.software.base.EmptySoftwareProcess",
Config: []models.ConfigSummary{
{
Name: "testConfigKey",
Pinned: true,
},
},
},
IconUrl: "/v1/catalog/icon/server/1.0.0-SNAPSHOT",
}
return summary
}

func assertItemSummaryFields(t *testing.T, expected models.CatalogItemSummary, actual models.CatalogItemSummary) {
assert.Equal(t, expected.Id, actual.Id, "id")
assert.Equal(t, expected.Name, actual.Name, "name")
assert.Equal(t, expected.Version, actual.Version, "version")
assert.Equal(t, expected.JavaType, actual.JavaType, "javaType")
assert.Equal(t, expected.Config[0].Name, actual.Config[0].Name, "Config[0].Name")
assert.Equal(t, expected.Config[0].Pinned, actual.Config[0].Pinned, "Config[0].Pinned")
}

func testDisplay(t *testing.T, fn func(c *cli.Context) error, testPath string) string {

testApp := cli.NewApp()
testApp.Flags = []cli.Flag{
cli.StringFlag{
Name: "json, j",
Usage: "Render value as json with json path selector as described at https://github.com/NodePrime/jsonpath. (Experimental, not supported on all commands at present) ",
},
}
var out string
var err error
testApp.Action = func(c *cli.Context) {
out, err = divertStdoutToString(func() error {
return fn(c)
})
}

err = testApp.Run([]string{"", "--json", testPath})
if err != nil {
t.Fatalf("Error from Run: %s\n", err)
}

return out
}

func TestCatalogItemSummaryDisplay(t *testing.T) {
expected := testCatalogEntitySummary()
displayOutput := testDisplay(t, func(c *cli.Context) error {
return expected.Display(c)
}, "$")
var actual models.CatalogItemSummary
actualBytes := []byte(displayOutput)
unmarshalErr := json.Unmarshal(actualBytes, &actual)
assert.Nil(t, unmarshalErr, "result is not JSON: %s", actualBytes)

assertItemSummaryFields(t, expected.CatalogItemSummary, actual)
}

func TestCatalogEntitySummaryDisplay(t *testing.T) {

expected := testCatalogEntitySummary()
displayOutput := testDisplay(t, func(c *cli.Context) error {
return expected.Display(c)
}, "$")

var actual models.CatalogEntitySummary
actualBytes := []byte(displayOutput)
unmarshalErr := json.Unmarshal(actualBytes, &actual)
assert.Nil(t, unmarshalErr, "result is not JSON: %s", actualBytes)

assertItemSummaryFields(t, expected.CatalogItemSummary, actual.CatalogItemSummary)
assert.Equal(t, expected.IconUrl, actual.IconUrl, "iconUrl")
}

func TestPaths(t *testing.T) {
type pathTest struct {
testPath string
expected string
}

testObject := testCatalogEntitySummary()

tests := []pathTest{
{"$.name+", testObject.Name},
{"$.id+", testObject.Id},
}

for _, test := range tests {
actual := testDisplay(t, func(c *cli.Context) error {
return testObject.Display(c)
}, test.testPath)
assert.Equal(t, test.expected, actual, fmt.Sprintf("path %s", test.testPath))
}
}
@@ -19,7 +19,11 @@
package commands

import (
"bytes"
"encoding/json"
"io"
"os"
"strings"
)

func stringRepresentation(value interface{}) (string, error) {
@@ -36,3 +40,30 @@ func stringRepresentation(value interface{}) (string, error) {
}
return result, nil
}

func divertStdoutToString(fn func() error) (string, error) {
previous := os.Stdout
defer func() {
os.Stdout = previous
}()

r, w, _ := os.Pipe()
os.Stdout = w

err := fn()
if err != nil {
return "", err
}

errc := make(chan error)
var buf bytes.Buffer

go func() {
_, err := io.Copy(&buf, r)
errc <- err
}()
w.Close()
err = <-errc
output := strings.TrimSpace(buf.String())
return output, err
}
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package commands

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestDivertStdoutToString(t *testing.T) {
result, _ := divertStdoutToString(func() error {
fmt.Println("Hello Brooklyn")
return nil
})
if result != "Hello Brooklyn" {
t.Fatalf("Result was %s\n", result)
}
assert.Equal(t, "Hello Brooklyn", result, "Divert failed")
}

0 comments on commit 9a5fa7d

Please sign in to comment.