Skip to content

Commit

Permalink
Merge pull request #94 from nsip/napval_fields
Browse files Browse the repository at this point in the history
replace gosexy/rest with go-resty/resty, update JSON Schema field reporting
  • Loading branch information
matt-farmer authored Sep 2, 2020
2 parents 6a93ed3 + 2ea839e commit 12005e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
42 changes: 36 additions & 6 deletions app/napval/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import (
"bytes"
"encoding/json"
//"log"
"net/url"
//"net/url"
"net"
"os"
"path"
"strings"
"testing"
"time"

"github.com/gosexy/rest"
//"github.com/gosexy/rest"
"github.com/go-resty/resty/v2"
"github.com/nsip/nias2/lib"
"github.com/nsip/nias2/napval"
)

var customClient *rest.Client
var customClient *resty.Client

/*
func post_file(filename string, endpoint string) (string, error) {
var f *os.File
var err error
Expand All @@ -42,6 +46,27 @@ func post_file(filename string, endpoint string) (string, error) {
txid := dst["TxID"].(string)
return txid, nil
}
*/

func post_file(filename string, endpoint string) (string, error) {
dst := lib.NiasMessage{}
resp, err := customClient.R().
SetFiles(map[string]string{
"validationFile": filename,
}).
SetFormData(map[string]string{
"name": path.Base(filename),
}).
SetResult(&dst).
Post(endpoint)
if err != nil {
return "", err
}
out := resp.Result().(*lib.NiasMessage)
//log.Printf("%+v", out)
txid := out.TxID
return txid, nil
}

func TestRepeatPSIwithinSchool(t *testing.T) {
test_harness(t, "../../unit_test_files/5StudentsDuplicatePlatformID.csv", "PSI/ASL ID", "Platform Student ID (Student) and ASL ID (School) are potential duplicate")
Expand Down Expand Up @@ -384,16 +409,18 @@ func errcheck(t *testing.T, err error) {
/* if errfield is nil, we expect test to pass */
func test_harness(t *testing.T, filename string, errfield string, errdescription string) {
var err error
bytebuf := []byte{}
//bytebuf := []byte{}
dat := map[string]string{}

txid, err := post_file(filename, "naplan/reg/validate")
errcheck(t, err)
time.Sleep(100 * time.Millisecond)

err = customClient.Get(&bytebuf, "naplan/reg/results/"+txid, nil)
//err = customClient.Get(&bytebuf, "naplan/reg/results/"+txid, nil)
resp, err := customClient.R().Get("naplan/reg/results/" + txid)
errcheck(t, err)
//log.Println("+++" + string(bytebuf))
bytebuf := resp.Body()

// we are getting back line delimited JSON
lines := bytes.Split(bytebuf, []byte{'\n'})
Expand Down Expand Up @@ -434,6 +461,9 @@ func test_harness(t *testing.T, filename string, errfield string, errdescription

func TestMain(m *testing.M) {
config := napval.LoadNAPLANConfig()
customClient, _ = rest.New("http://localhost:" + config.WebServerPort + "/")
//customClient, _ = rest.New("http://localhost:" + config.WebServerPort + "/")
localAddress, _ := net.ResolveTCPAddr("tcp", "127.0.0.1")
customClient = resty.NewWithLocalAddr(localAddress)
customClient.SetHostURL("http://localhost:" + config.WebServerPort + "/")
os.Exit(m.Run())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/clipperhouse/jargon v1.0.7
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/go-resty/resty/v2 v2.3.0
github.com/gosexy/rest v0.0.0-20160109152940-b63cba93265b
github.com/kylelemons/godebug v1.1.0
github.com/labstack/echo v3.3.10+incompatible
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/glendc/gopher-json v0.0.0-20170414221815-dc4743023d0c h1:iRTj5SRYwbvsygdwVp+y9kZT145Y1s6xOPpeOEIeGc4=
github.com/glendc/gopher-json v0.0.0-20170414221815-dc4743023d0c/go.mod h1:Gja1A+xZ9BoviGJNA2E9vFkPjjsl+CoJxSXiQM1UXtw=
github.com/go-resty/resty v1.12.0 h1:L1P5qymrXL5H/doXe2pKUr1wxovAI5ilm2LdVLbwThc=
github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So=
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
Expand Down Expand Up @@ -259,6 +262,7 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200519113804-d87ec0cfa476/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
Expand Down
12 changes: 10 additions & 2 deletions napval/schemaservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/tidwall/sjson"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
//"log"
"log"
)

// implementaiton of the json schema validation service
Expand Down Expand Up @@ -89,8 +89,16 @@ func (ss *SchemaService) HandleMessage(req *lib.NiasMessage) ([]lib.NiasMessage,
if !result.Valid() {
seen := make(map[string]bool)
for _, desc := range result.Errors() {
//log.Printf("ERRRORRRRR: %d: %s\n", desc, req.SeqNo)
field := desc.Field()
field1 := desc.Details()["field"].(string)
field2 := desc.Details()["property"]
//log.Printf("ERRRORRRRR: %+v: [%s] [%s] [%s] %s\n", desc, field, field1, field2, req.SeqNo)
if field == "(root)" && field1 != "" {
field = field1
}
if field == "(root)" && field2 != nil && field2.(string) != "" {
field = field2.(string)
}

// Handle dependencies errors
if desc.Field() == "(root)" {
Expand Down

0 comments on commit 12005e1

Please sign in to comment.