-
Notifications
You must be signed in to change notification settings - Fork 54
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
Showing
3 changed files
with
115 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api_gateway_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/it-chain/engine/api_gateway" | ||
"github.com/it-chain/engine/common/event" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConnectionQueryApi_SaveConnection(t *testing.T) { | ||
repo := api_gateway.NewConnectionRepository() | ||
api := api_gateway.NewConnectionQueryApi(*repo) | ||
err := api.Save(api_gateway.Connection{ | ||
ConnectionID: "1", | ||
Address: "address", | ||
}) | ||
assert.NoError(t, err) | ||
|
||
tests := map[string]struct { | ||
Input api_gateway.Connection | ||
Output error | ||
}{ | ||
"success": { | ||
Input: api_gateway.Connection{ | ||
ConnectionID: "0", | ||
Address: "address", | ||
}, | ||
Output: nil, | ||
}, | ||
"fail": { | ||
Input: api_gateway.Connection{ | ||
ConnectionID: "1", | ||
Address: "address", | ||
}, | ||
Output: api_gateway.ErrConnectionExists, | ||
}, | ||
} | ||
|
||
for testName, test := range tests { | ||
t.Logf("Running '%s' test, caseName: %s", t.Name(), testName) | ||
//given | ||
|
||
err := api.Save(test.Input) | ||
if err != nil { | ||
assert.Equal(t, test.Output, err) | ||
continue | ||
} | ||
|
||
c, exist := repo.ConnectionTable[test.Input.ConnectionID] | ||
assert.True(t, exist) | ||
assert.Equal(t, test.Output, err) | ||
assert.Equal(t, test.Input.ConnectionID, c.ConnectionID) | ||
assert.Equal(t, test.Input.ConnectionID, c.ConnectionID) | ||
} | ||
|
||
} | ||
|
||
func TestConnectionQueryApi_RemoveConnection(t *testing.T) { | ||
repo := api_gateway.NewConnectionRepository() | ||
api := api_gateway.NewConnectionQueryApi(*repo) | ||
err := api.Save(api_gateway.Connection{ | ||
ConnectionID: "0", | ||
Address: "address", | ||
}) | ||
assert.NoError(t, err) | ||
|
||
c, exist := repo.ConnectionTable["0"] | ||
assert.True(t, exist) | ||
|
||
api.Remove(c.ConnectionID) | ||
_, exist = repo.ConnectionTable["0"] | ||
assert.False(t, exist) | ||
} | ||
|
||
func TestConnectionEventListener_HandleConnectionCreatedEvent(t *testing.T) { | ||
repo := api_gateway.NewConnectionRepository() | ||
api := api_gateway.NewConnectionQueryApi(*repo) | ||
listener := api_gateway.NewConnectionEventListener(*api) | ||
|
||
err := listener.HandleConnectionCreatedEvent(event.ConnectionCreated{ | ||
ConnectionID: "0", | ||
Address: "address", | ||
}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestConnectionEventListener_HandleConnectionClosedEvent(t *testing.T) { | ||
repo := api_gateway.NewConnectionRepository() | ||
api := api_gateway.NewConnectionQueryApi(*repo) | ||
listener := api_gateway.NewConnectionEventListener(*api) | ||
|
||
err := listener.HandleConnectionCreatedEvent(event.ConnectionCreated{ | ||
ConnectionID: "0", | ||
Address: "address", | ||
}) | ||
assert.NoError(t, err) | ||
|
||
c, exist := repo.ConnectionTable["0"] | ||
assert.True(t, exist) | ||
|
||
listener.HandleConnectionClosedEvent(event.ConnectionClosed{ | ||
ConnectionID: c.ConnectionID, | ||
}) | ||
_, exist = repo.ConnectionTable["0"] | ||
assert.False(t, exist) | ||
} |
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