<commit_before>// +build integration

package loader

import (
	"io/ioutil"
	"net/http"
	"testing"

	"github.com/hellofresh/janus/pkg/api"
	"github.com/hellofresh/janus/pkg/middleware"
	"github.com/hellofresh/janus/pkg/plugin"
	"github.com/hellofresh/janus/pkg/proxy"
	"github.com/hellofresh/janus/pkg/router"
	"github.com/hellofresh/janus/pkg/test"
	"github.com/hellofresh/janus/pkg/web"
	"github.com/hellofresh/stats-go"
	log "github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

var tests = []struct {
	description     string
	method          string
	url             string
	headers         map[string]string
	expectedHeaders map[string]string
	expectedCode    int
}{
	{
		description: "Get example route",
		method:      "GET",
		url:         "/example",
		expectedHeaders: map[string]string{
			"Content-Type": "application/json",
		},
		expectedCode: http.StatusOK,
	}, {
		description: "Get invalid route",
		method:      "GET",
		url:         "/invalid-route",
		expectedHeaders: map[string]string{
			"Content-Type": "application/json",
		},
		expectedCode: http.StatusNotFound,
	},
}

func TestSuccessfulLoader(t *testing.T) {
	log.SetOutput(ioutil.Discard)

	routerInstance, err := createRegisterAndRouter()
	assert.NoError(t, err)
	ts := test.NewServer(routerInstance)
	defer ts.Close()

	for _, tc := range tests {
		res, err := ts.Do(tc.method, tc.url, tc.headers)
		assert.NoError(t, err)
		if res != nil {
			defer res.Body.Close()
		}

		for headerName, headerValue := range tc.expectedHeaders {
			assert.Equal(t, headerValue, res.Header.Get(headerName))
		}

		assert.Equal(t, tc.expectedCode, res.StatusCode, tc.description)
	}
}

func createRegisterAndRouter() (router.Router, error) {
	r := createRouter()
	r.Use(middleware.NewRecovery(web.RecoveryHandler))

	statsClient, _ := stats.NewClient("memory://", "")
	register := proxy.NewRegister(r, proxy.Params{StatsClient: statsClient})
	proxyRepo, err := api.NewFileSystemRepository("../../examples/apis")
	if err != nil {
		return nil, err
	}

	pluginLoader := plugin.NewLoader()
	loader := NewAPILoader(register, pluginLoader)
	loader.LoadDefinitions(proxyRepo)
<commit_msg>Changed the path<commit_after>// +build integration

package loader

import (
	"io/ioutil"
	"net/http"
	"testing"

	"github.com/hellofresh/janus/pkg/api"
	"github.com/hellofresh/janus/pkg/middleware"
	"github.com/hellofresh/janus/pkg/plugin"
	"github.com/hellofresh/janus/pkg/proxy"
	"github.com/hellofresh/janus/pkg/router"
	"github.com/hellofresh/janus/pkg/test"
	"github.com/hellofresh/janus/pkg/web"
	"github.com/hellofresh/stats-go"
	log "github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

var tests = []struct {
	description     string
	method          string
	url             string
	headers         map[string]string
	expectedHeaders map[string]string
	expectedCode    int
}{
	{
		description: "Get example route",
		method:      "GET",
		url:         "/example",
		expectedHeaders: map[string]string{
			"Content-Type": "application/json",
		},
		expectedCode: http.StatusOK,
	}, {
		description: "Get invalid route",
		method:      "GET",
		url:         "/invalid-route",
		expectedHeaders: map[string]string{
			"Content-Type": "application/json",
		},
		expectedCode: http.StatusNotFound,
	},
}

func TestSuccessfulLoader(t *testing.T) {
	log.SetOutput(ioutil.Discard)

	routerInstance, err := createRegisterAndRouter()
	assert.NoError(t, err)
	ts := test.NewServer(routerInstance)
	defer ts.Close()

	for _, tc := range tests {
		res, err := ts.Do(tc.method, tc.url, tc.headers)
		assert.NoError(t, err)
		if res != nil {
			defer res.Body.Close()
		}

		for headerName, headerValue := range tc.expectedHeaders {
			assert.Equal(t, headerValue, res.Header.Get(headerName))
		}

		assert.Equal(t, tc.expectedCode, res.StatusCode, tc.description)
	}
}

func createRegisterAndRouter() (router.Router, error) {
	r := createRouter()
	r.Use(middleware.NewRecovery(web.RecoveryHandler))

	statsClient, _ := stats.NewClient("memory://", "")
	register := proxy.NewRegister(r, proxy.Params{StatsClient: statsClient})
	proxyRepo, err := api.NewFileSystemRepository("../../examples/front-proxy/apis")
	if err != nil {
		return nil, err
	}

	pluginLoader := plugin.NewLoader()
	loader := NewAPILoader(register, pluginLoader)
	loader.LoadDefinitions(proxyRepo)

{'fraction_non_alphanumeric': 0.14512785072563925, 'fraction_numerical': 0.0, 'mean_word_length': 3.962285714285714, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6861583', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1520, 'n_words': 266}