-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3416542
commit b379368
Showing
2 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
package test | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/goledgerdev/cc-tools/mock" | ||
) | ||
|
||
// TODO: Lista de assets com referencia | ||
|
||
func TestCreateAssetType(t *testing.T) { | ||
stub := mock.NewMockStub("org1MSP", new(testCC)) | ||
newType := map[string]interface{}{ | ||
"tag": "magazine", | ||
"label": "Magazine", | ||
"description": "Magazine definition", | ||
"props": []map[string]interface{}{ | ||
{ | ||
"tag": "name", | ||
"label": "Name", | ||
"dataType": "string", | ||
"required": true, | ||
"writers": []string{"org1MSP"}, | ||
"isKey": true, | ||
}, | ||
{ | ||
"tag": "images", | ||
"label": "Images", | ||
"dataType": "[]string", | ||
}, | ||
}, | ||
} | ||
req := map[string]interface{}{ | ||
"assetTypes": []map[string]interface{}{newType}, | ||
} | ||
reqBytes, err := json.Marshal(req) | ||
if err != nil { | ||
t.FailNow() | ||
} | ||
|
||
res := stub.MockInvoke("createAssetType", [][]byte{ | ||
[]byte("createAssetType"), | ||
reqBytes, | ||
}) | ||
expectedResponse := map[string]interface{}{ | ||
"description": "Magazine definition", | ||
"dynamic": true, | ||
"label": "Magazine", | ||
"props": []interface{}{ | ||
map[string]interface{}{ | ||
"dataType": "string", | ||
"description": "", | ||
"isKey": true, | ||
"label": "Name", | ||
"readOnly": false, | ||
"required": true, | ||
"tag": "name", | ||
"writers": []interface{}{"org1MSP"}, | ||
}, | ||
map[string]interface{}{ | ||
"dataType": "[]string", | ||
"description": "", | ||
"isKey": false, | ||
"label": "Images", | ||
"readOnly": false, | ||
"required": false, | ||
"tag": "images", | ||
"writers": nil, | ||
}, | ||
}, | ||
"tag": "magazine", | ||
} | ||
|
||
if res.GetStatus() != 200 { | ||
log.Println(res) | ||
t.FailNow() | ||
} | ||
|
||
var resPayload []map[string]interface{} | ||
err = json.Unmarshal(res.GetPayload(), &resPayload) | ||
if err != nil { | ||
log.Println(err) | ||
t.FailNow() | ||
} | ||
|
||
if len(resPayload) != 1 { | ||
log.Println("response length should be 1") | ||
t.FailNow() | ||
} | ||
|
||
if !reflect.DeepEqual(resPayload[0], expectedResponse) { | ||
log.Println("these should be equal") | ||
log.Printf("%#v\n", resPayload[0]) | ||
log.Printf("%#v\n", expectedResponse) | ||
t.FailNow() | ||
} | ||
|
||
// Create Asset | ||
asset := map[string]interface{}{ | ||
"@assetType": "magazine", | ||
"name": "MAG", | ||
"images": []string{"url.com/1", "url.com/2"}, | ||
} | ||
req = map[string]interface{}{ | ||
"asset": []map[string]interface{}{asset}, | ||
} | ||
reqBytes, err = json.Marshal(req) | ||
if err != nil { | ||
t.FailNow() | ||
} | ||
|
||
res = stub.MockInvoke("createAsset", [][]byte{ | ||
[]byte("createAsset"), | ||
reqBytes, | ||
}) | ||
lastUpdated, _ := stub.GetTxTimestamp() | ||
expectedResponse = map[string]interface{}{ | ||
"@key": "magazine:236a29db-f53c-59e1-ac6d-a4f264dbc477", | ||
"@lastTouchBy": "org1MSP", | ||
"@lastTx": "createAsset", | ||
"@lastUpdated": lastUpdated.AsTime().Format(time.RFC3339), | ||
"@assetType": "magazine", | ||
"name": "MAG", | ||
"images": []interface{}{ | ||
"url.com/1", | ||
"url.com/2", | ||
}, | ||
} | ||
|
||
if res.GetStatus() != 200 { | ||
log.Println(res) | ||
t.FailNow() | ||
} | ||
|
||
var resAssetPayload []map[string]interface{} | ||
err = json.Unmarshal(res.GetPayload(), &resAssetPayload) | ||
if err != nil { | ||
log.Println(err) | ||
t.FailNow() | ||
} | ||
|
||
if len(resAssetPayload) != 1 { | ||
log.Println("response length should be 1") | ||
t.FailNow() | ||
} | ||
|
||
if !reflect.DeepEqual(resAssetPayload[0], expectedResponse) { | ||
log.Println("these should be equal") | ||
log.Printf("%#v\n", resAssetPayload[0]) | ||
log.Printf("%#v\n", expectedResponse) | ||
t.FailNow() | ||
} | ||
|
||
var state map[string]interface{} | ||
stateBytes := stub.State["magazine:236a29db-f53c-59e1-ac6d-a4f264dbc477"] | ||
err = json.Unmarshal(stateBytes, &state) | ||
if err != nil { | ||
log.Println(err) | ||
t.FailNow() | ||
} | ||
|
||
if !reflect.DeepEqual(state, expectedResponse) { | ||
log.Println("these should be equal") | ||
log.Printf("%#v\n", state) | ||
log.Printf("%#v\n", expectedResponse) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// func TestCreateAssetTypeEmptyList(t *testing.T) { | ||
// stub := mock.NewMockStub("org1MSP", new(testCC)) | ||
|
||
// req := map[string]interface{}{ | ||
// "asset": []map[string]interface{}{}, | ||
// } | ||
// reqBytes, err := json.Marshal(req) | ||
// if err != nil { | ||
// t.FailNow() | ||
// } | ||
|
||
// res := stub.MockInvoke("CreateAsset", [][]byte{ | ||
// []byte("createAsset"), | ||
// reqBytes, | ||
// }) | ||
|
||
// if res.GetStatus() != 400 { | ||
// log.Println(res) | ||
// t.FailNow() | ||
// } | ||
|
||
// if res.GetMessage() != "unable to get args: required argument 'asset' must be non-empty" { | ||
// log.Printf("error message different from expected: %s", res.GetMessage()) | ||
// t.FailNow() | ||
// } | ||
// } | ||
|
||
// func TestCreatePrivateType(t *testing.T) { | ||
// stub := mock.NewMockStub("org2MSP", new(testCC)) | ||
// secret := map[string]interface{}{ | ||
// "@assetType": "secret", | ||
// "secretName": "testSecret", | ||
// "secret": "this is very secret", | ||
// } | ||
// expectedResponse := map[string]interface{}{ | ||
// "@key": "secret:73a3f9a7-eb91-5f4d-b1bb-c0487e90f40b", | ||
// "@assetType": "secret", | ||
// } | ||
// req := map[string]interface{}{ | ||
// "asset": []map[string]interface{}{secret}, | ||
// } | ||
// reqBytes, err := json.Marshal(req) | ||
// if err != nil { | ||
// t.FailNow() | ||
// } | ||
|
||
// res := stub.MockInvoke("createAsset", [][]byte{ | ||
// []byte("createAsset"), | ||
// reqBytes, | ||
// }) | ||
// lastUpdated, _ := stub.GetTxTimestamp() | ||
// expectedState := map[string]interface{}{ | ||
// "@key": "secret:73a3f9a7-eb91-5f4d-b1bb-c0487e90f40b", | ||
// "@assetType": "secret", | ||
// "@lastTouchBy": "org2MSP", | ||
// "@lastTx": "createAsset", | ||
// "@lastUpdated": lastUpdated.AsTime().Format(time.RFC3339), | ||
// "secretName": "testSecret", | ||
// "secret": "this is very secret", | ||
// } | ||
|
||
// if res.GetStatus() != 200 { | ||
// log.Println(res) | ||
// t.FailNow() | ||
// } | ||
|
||
// var resPayload []map[string]interface{} | ||
// err = json.Unmarshal(res.GetPayload(), &resPayload) | ||
// if err != nil { | ||
// log.Println(err) | ||
// t.FailNow() | ||
// } | ||
|
||
// if len(resPayload) != 1 { | ||
// log.Println("response length should be 1") | ||
// t.FailNow() | ||
// } | ||
|
||
// if !reflect.DeepEqual(resPayload[0], expectedResponse) { | ||
// log.Println("these should be equal") | ||
// log.Printf("%#v\n", resPayload[0]) | ||
// log.Printf("%#v\n", expectedResponse) | ||
// t.FailNow() | ||
// } | ||
|
||
// var state map[string]interface{} | ||
// stateBytes := stub.PvtState["secret"]["secret:73a3f9a7-eb91-5f4d-b1bb-c0487e90f40b"] | ||
// err = json.Unmarshal(stateBytes, &state) | ||
// if err != nil { | ||
// log.Println(err) | ||
// t.FailNow() | ||
// } | ||
|
||
// if !reflect.DeepEqual(state, expectedState) { | ||
// log.Println("these should be equal") | ||
// log.Printf("%#v\n", state) | ||
// log.Printf("%#v\n", expectedState) | ||
// t.FailNow() | ||
// } | ||
// } |