/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  testCaseRef_4_3_001_test.go
 *     Version:    1.0.0
 *     Date:       2018-01-12
 *
 *     Autor:      Bogdanovic Theodor
 *     Contact:    t.bogdanovic@hotmail.com
 *
 *     License:    GPLv3
 *
 **************************************************************************************/
/**************************************************************************************
 * Copyright 2018 Bogdanovic Theodor
 *
 * This file is part of kvmstoreserver.
 *
 * kvmstoreserver is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * kvmstoreserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kvmstoreserver.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************************/
/**************************************************************************************
 *  Further explanations:  This source code is under the license specified by the
 *                         Autor above. Core components or other packages are still
 *                         in source code form, under the license specified by the
 *                         respective author.
 **************************************************************************************/

package main

import (
	"testing"
)

// Test4_3_001_test tests the middleware, which should intercept wrong HTTP methods.
// Here are the following tests:
//
//	- Try using Add with POST 	(Must work)
//	- Try using Add with DELETE	(Error message expected)
//	- Try using Add with GET	(Error message expected)
//
//	- Try using Get with GET 	(Must work)
//	- Try using Get with DELETE	(Error message expected)
//	- Try using Get with POST 	(Error message expected)
//
//	- Try using Del with DELETE	(Must work)
//	- Try using Del with POST	(Error message expected)
//	- Try using Del with GET 	(Error message expected)
func Test4_3_001_test(t *testing.T) {

	// Create test data
	tests := []struct {
		name             string
		keySeqNbr        int
		revNbr           int
		cliAction        clientAction
		httpMeth         string
		wantedStatusCode int
	}{
		// Test sequence 1
		{name: "Test 1_1: Try to insert entry (Controller /add/ | Method POST)", keySeqNbr: 100, revNbr: 0, cliAction: actionAdd, httpMeth: POST, wantedStatusCode: 201},
		{name: "Test 1_2: Try to insert entry (Controller /add/ | Method DELETE)", keySeqNbr: 100, revNbr: 0, cliAction: actionAdd, httpMeth: DELETE, wantedStatusCode: 405},
		{name: "Test 1_3: Try to insert entry (Controller /add/ | Method GET)", keySeqNbr: 100, revNbr: 0, cliAction: actionAdd, httpMeth: GET, wantedStatusCode: 405},
		// Test sequence 2
		{name: "Test 2_1: Try to get entry (Controller /get/ | Method GET)", keySeqNbr: 100, revNbr: 0, cliAction: actionGet, httpMeth: GET, wantedStatusCode: 200},
		{name: "Test 2_2: Try to get entry (Controller /get/ | Method DELETE)", keySeqNbr: 100, revNbr: 0, cliAction: actionGet, httpMeth: DELETE, wantedStatusCode: 405},
		{name: "Test 2_3: Try to get entry (Controller /get/ | Method POST)", keySeqNbr: 100, revNbr: 0, cliAction: actionGet, httpMeth: POST, wantedStatusCode: 405},
		// Test sequence 3
		{name: "Test 3_1: Try to delete entry (Controller /del/ | Method DELETE)", keySeqNbr: 100, revNbr: 0, cliAction: actionDel, httpMeth: DELETE, wantedStatusCode: 200},
		{name: "Test 3_2: Try to delete entry (Controller /del/ | Method POST)", keySeqNbr: 100, revNbr: 0, cliAction: actionDel, httpMeth: POST, wantedStatusCode: 405},
		{name: "Test 3_3: Try to delete entry (Controller /del/ | Method GET)", keySeqNbr: 100, revNbr: 0, cliAction: actionDel, httpMeth: GET, wantedStatusCode: 405},
	}

	// Start a test server in a separate gourutine
	runKvmssTestInstance()

	// Clean up the environment (if there are remnants of the last test)
	testClient(tests[0].keySeqNbr, tests[0].revNbr, actionDel, DELETE, "")

	// Execute the test series
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := testClient(tt.keySeqNbr, tt.revNbr, tt.cliAction, tt.httpMeth, ""); got.StatusCode != tt.wantedStatusCode {
				t.Errorf("Start testClient() (Client Test Desc.: HTTP Method allowed?) = %v, want %v", got.StatusCode, tt.wantedStatusCode)
			}
		})
	}

	// Post job: Clean up the environment
	testClient(tests[0].keySeqNbr, tests[0].revNbr, actionDel, DELETE, "")
}
