func TestGetSystemInfo(t *testing.T) { mgrCfg := server.EntityManagerConfig{ TimeRanges: []time.Duration{6 * time.Hour, 1 * time.Hour}, ContentSource: mock.NewMockContentSource(), } mgr := server.NewEntityManager(mgrCfg) appCfg := AppConfig{ TimeRanges: []time.Duration{1 * time.Hour, 8 * time.Hour, 12 * time.Hour}, } systemInfoHandler := GetSystemInfo(mgr, appCfg) w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/api/system_info", nil) systemInfoHandler(w, r) assert.Equal(t, http.StatusOK, w.Code) // Spot check: just make sure the parent JSON keys are present. response := json.ParseBytes(w.Body.Bytes()) assert.Equal(t, "[1 8 12]", response.Get("Runtime").Get("TimeRangesInHours").AsString()) assert.True(t, response.Get("Deployment").Exists()) assert.True(t, response.Get("Runtime").Exists()) assert.True(t, response.Get("Runtime").Get("ItemCounts").Exists()) assert.True(t, response.Get("Runtime").Get("OldestContent").Exists()) assert.True(t, response.Get("Runtime").Get("NewestContent").Exists()) }
func TestHealthCheck(t *testing.T) { w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/api/any_ol_endpoint", nil) healthCheckHandler := HealthCheck() healthCheckHandler(w, r) assert.Equal(t, http.StatusOK, w.Code) jsonResponse := json.ParseBytes(w.Body.Bytes()) assert.Equal(t, "true", jsonResponse.Get("ok").AsString()) }
func TestGetMemStats(t *testing.T) { handler := GetMemStats() request, _ := http.NewRequest("GET", "/api/memstats", nil) mockWriter := httptest.NewRecorder() handler(mockWriter, request) assert.Equal(t, http.StatusOK, mockWriter.Code) jsonResponse := json.ParseBytes(mockWriter.Body.Bytes()) assert.True(t, jsonResponse.Get("General").Exists()) assert.True(t, jsonResponse.Get("Heap").Exists()) }
// Verify that submitting a search string that's empty or only contains // whitespace returns an empty resultset and does not actually call Find() on // the target EntitySearch component. func TestFindEntities_emptySearchString(t *testing.T) { mockEntitySearch := &MockEntitySearch{} handler := FindEntities(mockEntitySearch) mockWriter := httptest.NewRecorder() mockRequest, _ := http.NewRequest("GET", "/api/search/ ", nil) fakeUserId := -1 // not used for this test, but method signature requires it handler(mockWriter, mockRequest, fakeUserId) assert.Equal(t, http.StatusOK, mockWriter.Code) assert.False(t, mockEntitySearch.wasFindInvoked) response := json.ParseBytes(mockWriter.Body.Bytes()) assert.Equal(t, 0, len(response.Get("Person").AsList())) assert.Equal(t, 0, len(response.Get("Org").AsList())) assert.Equal(t, 0, len(response.Get("Place").AsList())) }
func TestFindEntities(t *testing.T) { mockEntitySearch := &MockEntitySearch{} handler := FindEntities(mockEntitySearch) mockWriter := httptest.NewRecorder() mockRequest, _ := http.NewRequest("GET", "/api/search/alpha", nil) fakeUserId := -1 // not used for this test, but method signature requires it handler(mockWriter, mockRequest, fakeUserId) assert.Equal(t, http.StatusOK, mockWriter.Code) assert.Equal(t, "alpha", mockEntitySearch.searchString) // The response should contain exactly 1 search result for each entity type given the // seach string "alpha". response := json.ParseBytes(mockWriter.Body.Bytes()) assert.Equal(t, 1, len(response.Get("Person").AsList())) assert.Equal(t, 1, len(response.Get("Org").AsList())) assert.Equal(t, 1, len(response.Get("Place").AsList())) }