Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed errors endpoints #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ script:
--deadline=4m ./spew | tee /dev/stderr)"
- make test
- make CI-Coverage
deploy:
provider: script
script: curl -X POST https://goreportcard.com/checks -F 'repo=github.com/ManageIQ-Exchange/manageiq-exchange-cli-go'
on:
branch: master
52 changes: 28 additions & 24 deletions src/manageiq-exchange/api/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package api

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -10,10 +12,8 @@ import (
user "manageiq-exchange/models/user"
"net"
"net/http"
"time"
"bufio"
"strconv"
"errors"
"time"
)

var netTransport = &http.Transport{
Expand All @@ -28,25 +28,25 @@ var netClient = &http.Client{
Transport: netTransport,
}

type Api struct {
type API struct {
Server string
Port int
Client *http.Client
Data DataApi
Data DataAPI
}

type DataApi struct {
type DataAPI struct {
Data interface{} `json:"data"`
Meta meta.Metadata `json:"meta"`
}

func (a *Api) Init(server string, port int) {
func (a *API) Init(server string, port int) {
a.Server = server
a.Port = port
a.Client = netClient
}

func (a *Api) CheckConnectionServer() bool {
func (a *API) CheckConnectionServer() bool {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", a.Server, strconv.Itoa(a.Port)))
if err != nil {
fmt.Printf("Fatal error: %s\n", err.Error())
Expand All @@ -62,56 +62,60 @@ func (a *Api) CheckConnectionServer() bool {
return true
}

func (a *Api) URL() string {
func (a *API) URL() string {
url := fmt.Sprintf("http://%s", a.Server)
if a.Port > 0 {
url += fmt.Sprintf(":%d", a.Port)
}
return url
}

func (a *Api) GetInfo() info.Info {
a.Request("GET", "", nil)
func (a *API) GetInfo() info.Info {
err := a.Request("GET", "", nil)
if err != nil {
fmt.Printf("%+v", err)
return info.Info{}
}
var info info.Info
info.Init(a.Data.Data.(map[string]interface{}))
return info
}

func (a *Api) GetUsers(expand bool) user.UserCollection {
func (a *API) GetUsers(expand bool) user.UserCollection {
var path string
if path = "/v1/users"; expand {
path = "/v1/users?expand=resources"
}
err := a.Request("GET", path, nil)
if err != nil {
fmt.Printf("%+v", err)
return user.UserCollection{}
}
var users user.UserCollection
users.Init(a.Data.Data.([]interface{}))
return users
}

func (a *Api) Request(method string, path string, data io.Reader) error {
func (a *API) Request(method string, path string, data io.Reader) error {
req, err := http.NewRequest(method, fmt.Sprintf("%s/%s", a.URL(), path), data)
if err != nil {
return err
}
resp, err := a.Client.Do(req)
defer resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(strconv.Itoa(resp.StatusCode))
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
jsonErr := json.Unmarshal(body, &a.Data)
if jsonErr != nil {
return jsonErr
}
return errors.New(fmt.Sprintf("Error status code: %v", resp.StatusCode))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
jsonErr := json.Unmarshal(body, &a.Data)
if jsonErr != nil {
return jsonErr
}
return nil
}
112 changes: 88 additions & 24 deletions src/manageiq-exchange/api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package api

import (
"fmt"
"manageiq-exchange/models/info"
meta "manageiq-exchange/models/metadata"
user "manageiq-exchange/models/user"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"testing"
"fmt"
meta "manageiq-exchange/models/metadata"
user "manageiq-exchange/models/user"
"manageiq-exchange/models/info"
)

var (
// mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux

// client is the client being tested.
client *Api
client *API

// urlTest is the url test server
urlTest *url.URL
Expand All @@ -38,7 +38,7 @@ func setup() {
// http client configured to use test server
urlTest, _ = url.Parse(server.URL + "/")
i, _ := strconv.Atoi(urlTest.Port())
client = &Api{}
client = &API{}
client.Init(urlTest.Hostname(), i)
}

Expand All @@ -48,7 +48,7 @@ func teardown() {
}

func TestApi_Init(t *testing.T) {
var server Api
var server API
inputServer := "localhost"
inputPort := 3000
wantServer := "localhost"
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestApi_URL(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.inputServer, func(t *testing.T) {
var server Api
var server API
server.Init(tt.inputServer, tt.inputPort)
gotURL := server.URL()
if !reflect.DeepEqual(gotURL, tt.wantURL) {
Expand Down Expand Up @@ -122,19 +122,42 @@ func TestApi_Request(t *testing.T) {
"A": "a",
}
wantMeta := meta.Metadata{
CurrentPage:0,
TotalPages:0,
TotalCount:0,
CurrentPage: 0,
TotalPages: 0,
TotalCount: 0,
}
if !reflect.DeepEqual(client.Data.Data, wantData){
if !reflect.DeepEqual(client.Data.Data, wantData) {
t.Errorf("client.Data.Data returned %+v want %+v", client.Data.Data, wantData)
}

if !reflect.DeepEqual(client.Data.Meta, wantMeta){
if !reflect.DeepEqual(client.Data.Meta, wantMeta) {
t.Errorf("client.Data.Meta returned %+v want %+v", client.Data.Meta, wantMeta)
}
}

func TestApi_Request_badURL(t *testing.T) {
setup()
defer teardown()

err := client.Request(http.MethodGet, "%zzzzz", nil)

if err == nil {
t.Errorf("Expected error to be returned")
}
}

func TestApi_Request_invalidDo(t *testing.T) {
setup()
defer teardown()

client.Port = 0

err := client.Request(http.MethodGet, ":", nil)
if err == nil {
t.Errorf("Expected error to be returned")
}
}

func TestApi_Request_httpError(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -168,15 +191,33 @@ func TestApi_GetInfo(t *testing.T) {
Version: "1.0",
Providers: map[string]info.Provider{
"github.com": info.Provider{
ApplicationId: "abc",
ApplicationID: "abc",
Server: "github.com",
Version: "v3",
},
},
}

if !reflect.DeepEqual(gotInfo, wantInfo){
t.Errorf("Api.GetInfo() returned %+v want %+v",gotInfo , wantInfo)
if !reflect.DeepEqual(gotInfo, wantInfo) {
t.Errorf("Api.GetInfo() returned %+v want %+v", gotInfo, wantInfo)
}
}

func TestApi_GetInfo_httpError(t *testing.T) {
setup()
defer teardown()

pathURL := "/"

mux.HandleFunc(pathURL, func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Request", http.StatusBadRequest)
})

gotInfo := client.GetInfo()
wantInfo := info.Info{}

if !reflect.DeepEqual(gotInfo, wantInfo) {
t.Errorf("Api.GetInfo() returned %+v want %+v", gotInfo, wantInfo)
}
}

Expand All @@ -191,18 +232,20 @@ func TestApi_GetUsers_withoutExpandResources(t *testing.T) {
fmt.Fprint(w, `{"data":[{"login":"aljesusg","name":"Alberto","github_id":1}]}`)
})

gotUsers := client.GetUsers(false)
expand := false

gotUsers := client.GetUsers(expand)
wantUser := user.User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
wantUsers := user.UserCollection{}
wantUsers.Users = append(wantUsers.Users, wantUser)
wantUsers.Total = len(wantUsers.Users)

if !reflect.DeepEqual(gotUsers, wantUsers){
t.Errorf("Api.GetUsers(false) returned %+v want %+v",gotUsers , wantUsers)
if !reflect.DeepEqual(gotUsers, wantUsers) {
t.Errorf("Api.GetUsers(%t) returned %+v want %+v", expand, gotUsers, wantUsers)
}
}

Expand All @@ -217,17 +260,38 @@ func TestApi_GetUsers_withExpandResources(t *testing.T) {
fmt.Fprint(w, `{"data":[{"login":"aljesusg","name":"Alberto","github_id":1}]}`)
})

gotUsers := client.GetUsers(true)
expand := true

gotUsers := client.GetUsers(expand)
wantUser := user.User{
Login: "aljesusg",
Name: "Alberto",
GithubId: 1,
GithubID: 1,
}
wantUsers := user.UserCollection{}
wantUsers.Users = append(wantUsers.Users, wantUser)
wantUsers.Total = len(wantUsers.Users)

if !reflect.DeepEqual(gotUsers, wantUsers){
t.Errorf("Api.GetUsers(true) returned %+v want %+v",gotUsers , wantUsers)
if !reflect.DeepEqual(gotUsers, wantUsers) {
t.Errorf("Api.GetUsers(%t) returned %+v want %+v", expand, gotUsers, wantUsers)
}
}

func TestApi_GetUsers_httpError(t *testing.T) {
setup()
defer teardown()

pathURL := "/v1/users"
expand := true

mux.HandleFunc(pathURL, func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Request", http.StatusBadRequest)
})

gotUsers := client.GetUsers(expand)
wantUsers := user.UserCollection{}

if !reflect.DeepEqual(gotUsers, wantUsers) {
t.Errorf("Api.GetUsers(%t) returned %+v want %+v", expand, gotUsers, wantUsers)
}
}
16 changes: 8 additions & 8 deletions src/manageiq-exchange/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ func Menu() {
if err != nil {
fmt.Print(err)
}
var miq_exchange api.Api
miq_exchange.Init(server, port)
var miqExchange api.API
miqExchange.Init(server, port)

statusConnection := miq_exchange.CheckConnectionServer()
statusConnection := miqExchange.CheckConnectionServer()

if version && statusConnection{
info := miq_exchange.GetInfo()
if version && statusConnection {
info := miqExchange.GetInfo()
fmt.Printf(info.Print())
}

if users && statusConnection{
users := miq_exchange.GetUsers(expand)
fmt.Printf(users.Print(miq_exchange.Data.Meta.TotalCount))
if users && statusConnection {
users := miqExchange.GetUsers(expand)
fmt.Printf(users.Print(miqExchange.Data.Meta.TotalCount))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/manageiq-exchange/models/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Info struct {
type Provider struct {
Type string `json:"type"`
Enabled bool `json:"enabled"`
ApplicationId string `json:"id_application"`
ApplicationID string `json:"id_application"`
Server string `json:"server"`
Version string `json:"version"`
Verify bool `json:"verify"`
Expand All @@ -36,7 +36,7 @@ func (a *Info) Print() string {
for k, v := range a.Providers {
result += fmt.Sprintf(" %s: \n", k)
result += fmt.Sprintf(" %s: %s\n", "Server", v.Server)
result += fmt.Sprintf(" %s: %s\n", "ApplicationId", v.ApplicationId)
result += fmt.Sprintf(" %s: %s\n", "ApplicationId", v.ApplicationID)
result += fmt.Sprintf(" %s: %s\n", "Version", v.Version)
}
return result
Expand Down
Loading