2017-05-03 06:52:14 +02:00
|
|
|
// Package that contains all api routes of this microservice
|
2017-03-30 18:43:18 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-04-04 23:21:05 +02:00
|
|
|
"net/http/httptest"
|
2017-03-30 18:43:18 +02:00
|
|
|
"testing"
|
2017-04-04 23:21:05 +02:00
|
|
|
"time"
|
2017-03-30 18:43:18 +02:00
|
|
|
|
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/database"
|
2017-03-30 19:05:33 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/models"
|
2017-04-07 11:56:28 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/runtime"
|
2017-03-30 18:43:18 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/test"
|
|
|
|
)
|
|
|
|
|
2017-05-03 06:52:14 +02:00
|
|
|
// Function to test listGoods()
|
2017-04-04 23:21:05 +02:00
|
|
|
func TestListGood(t *testing.T) {
|
2017-03-30 18:43:18 +02:00
|
|
|
assertion, router := test.Init(t)
|
2017-03-30 19:05:33 +02:00
|
|
|
|
2017-03-30 18:43:18 +02:00
|
|
|
BindAPI(router)
|
|
|
|
session := test.NewSession(router)
|
|
|
|
|
2017-04-03 14:59:43 +02:00
|
|
|
result, w := session.JSONRequest("GET", "/api/good/a", nil)
|
2017-03-30 19:05:33 +02:00
|
|
|
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
|
|
|
|
|
2017-04-03 14:59:43 +02:00
|
|
|
result, w = session.JSONRequest("GET", "/api/good/1", nil)
|
2017-03-30 19:05:33 +02:00
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
2017-04-03 14:59:43 +02:00
|
|
|
database.Write.Create(&models.Good{
|
|
|
|
ProductID: 3,
|
|
|
|
Comment: "blub",
|
2017-03-30 19:05:33 +02:00
|
|
|
})
|
|
|
|
|
2017-04-03 14:59:43 +02:00
|
|
|
result, w = session.JSONRequest("GET", "/api/good/3", nil)
|
2017-03-30 18:43:18 +02:00
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
2017-03-30 19:05:33 +02:00
|
|
|
assertion.Len(result, 1)
|
|
|
|
|
|
|
|
test.Close()
|
2017-03-30 18:43:18 +02:00
|
|
|
}
|
2017-04-04 23:21:05 +02:00
|
|
|
|
2017-06-21 15:25:18 +02:00
|
|
|
// Function to test getGoodAvailability() and getGoodAvailabilityCount()
|
2017-04-04 23:21:05 +02:00
|
|
|
func TestGetGoodAvailable(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
assertion, router := test.Init(t)
|
|
|
|
|
2017-04-07 13:13:37 +02:00
|
|
|
runtime.ProductURL = "http://localhost:8080/api-test/product/%d/"
|
|
|
|
|
2017-04-04 23:21:05 +02:00
|
|
|
BindAPI(router)
|
|
|
|
session := test.NewSession(router)
|
|
|
|
|
|
|
|
result, w := session.JSONRequest("GET", "/api/good/availablity/a", nil)
|
|
|
|
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("GET", "/api/good/availablity/1", nil)
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
assertion.Equal(float64(0), result)
|
|
|
|
|
|
|
|
database.Write.Create(&models.Good{
|
|
|
|
ProductID: 3,
|
|
|
|
Comment: "blub",
|
|
|
|
LockedAt: &now,
|
|
|
|
LockedSecret: "hidden",
|
|
|
|
})
|
|
|
|
database.Write.Create(&models.Good{
|
|
|
|
ProductID: 3,
|
|
|
|
Comment: "blub",
|
|
|
|
})
|
|
|
|
database.Write.Create(&models.Good{
|
|
|
|
ProductID: 3,
|
|
|
|
Comment: "blub",
|
|
|
|
})
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("GET", "/api/good/availablity/3", nil)
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
assertion.Equal(float64(2), result)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/api/good/availablity/3", nil)
|
|
|
|
req.Header.Set("Content-Type", "image/svg+xml")
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
|
2017-04-07 11:32:49 +02:00
|
|
|
database.Write.Create(&models.Good{
|
|
|
|
ProductID: 4,
|
|
|
|
Comment: "blub",
|
|
|
|
})
|
|
|
|
|
2017-05-07 15:38:08 +02:00
|
|
|
result, w = session.JSONRequest("GET", "/api/good/availablity/7", nil)
|
2017-04-07 11:32:49 +02:00
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
|
|
|
test.CloseServer()
|
2017-04-07 11:56:28 +02:00
|
|
|
runtime.CacheConfig.After = models.Duration{Duration: time.Duration(5) * time.Millisecond}
|
2017-04-07 11:32:49 +02:00
|
|
|
time.Sleep(time.Duration(10) * time.Millisecond)
|
2017-04-07 11:56:28 +02:00
|
|
|
runtime.CleanCache()
|
2017-04-07 11:32:49 +02:00
|
|
|
|
|
|
|
result, w = session.JSONRequest("GET", "/api/good/availablity/3", nil)
|
|
|
|
assertion.Equal(http.StatusGatewayTimeout, w.StatusCode)
|
2017-04-04 23:21:05 +02:00
|
|
|
test.Close()
|
2017-04-07 11:32:49 +02:00
|
|
|
|
2017-04-04 23:21:05 +02:00
|
|
|
}
|
2017-05-18 00:29:49 +02:00
|
|
|
|
2017-06-21 15:25:18 +02:00
|
|
|
// Function to test getGoodFreshness()
|
2017-05-18 00:29:49 +02:00
|
|
|
func TestGetGoodFreshness(t *testing.T) {
|
|
|
|
now := time.Now().Add(36 * time.Hour)
|
|
|
|
assertion, router := test.Init(t)
|
|
|
|
|
|
|
|
runtime.ProductURL = "http://localhost:8080/api-test/product/%d/"
|
|
|
|
|
|
|
|
BindAPI(router)
|
|
|
|
session := test.NewSession(router)
|
|
|
|
|
|
|
|
result, w := session.JSONRequest("GET", "/api/good/freshness/a", nil)
|
|
|
|
assertion.Equal(http.StatusNotAcceptable, w.StatusCode)
|
|
|
|
|
|
|
|
database.Write.Create(&models.Good{
|
|
|
|
ID: 3,
|
|
|
|
ProductID: -2,
|
|
|
|
})
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("GET", "/api/good/freshness/3", nil)
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
assertion.Equal(false, result)
|
|
|
|
|
|
|
|
database.Write.Save(&models.Good{
|
|
|
|
ID: 3,
|
|
|
|
ProductID: -2,
|
|
|
|
FouledAt: &now,
|
|
|
|
})
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("GET", "/api/good/freshness/3", nil)
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
assertion.Equal(true, result)
|
|
|
|
|
|
|
|
fouled := now.Add(-72 * time.Hour)
|
|
|
|
database.Write.Save(&models.Good{
|
|
|
|
ID: 3,
|
|
|
|
ProductID: -2,
|
|
|
|
FouledAt: &fouled,
|
|
|
|
})
|
|
|
|
result, w = session.JSONRequest("GET", "/api/good/freshness/3", nil)
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
assertion.Equal(false, result)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/api/good/freshness/3", nil)
|
|
|
|
req.Header.Set("Content-Type", "image/svg+xml")
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(rec, req)
|
|
|
|
assertion.Equal(http.StatusOK, w.StatusCode)
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("GET", "/api/good/freshness/7", nil)
|
|
|
|
assertion.Equal(http.StatusNotFound, w.StatusCode)
|
|
|
|
|
|
|
|
test.Close()
|
|
|
|
|
|
|
|
}
|