/**************************************************************************************
 *
 *     Project:    kvmstoreserver
 *     File name:  testCaseRef_4_3_005_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 (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"testing"
)

// Test4_3_005_test first inserts a new key into the server. Then the testClient
// creates two more revisions. The exam is passed if the JSON inside RevNbr
// field returns the number 2 via testClient
// (the check takes place in the parser)
func Test4_3_005_test(t *testing.T) {

	// Create test data
	tests := []struct {
		name      string
		keySeqNbr int
		revNbr    int
		cliAction clientAction
		httpMeth  string
	}{
		// Test sequence 1
		{name: "Test 1_1: Try to insert entry first time (Controller /add/ | Method POST)", keySeqNbr: 500, revNbr: 0, cliAction: actionAdd, httpMeth: POST},
		{name: "Test 1_2: Try to insert entry second time (Controller /add/ | Method POST)", keySeqNbr: 500, revNbr: 1, cliAction: actionAdd, httpMeth: POST},
		{name: "Test 1_3: Try to insert entry third time (Controller /add/ | Method POST)", keySeqNbr: 500, revNbr: 2, cliAction: actionAdd, httpMeth: POST},
	}

	var got *http.Response

	// 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[2].revNbr, actionDel, DELETE, "")

	// Execute the test series
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got = testClient(tt.keySeqNbr, tt.revNbr, tt.cliAction, tt.httpMeth, "")
		})
	}
	if result, status := parseBodyRev(got); !result {
		t.Errorf("Start testClient() (Try to insert 3 entries) = %v, want %v", status, 2)
	}

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

// Here valid standard parser. Accepts the response of the testClient,
// decodes it and returns the following:
//
// @var bool = If the JSON field NewRevisionNumber from response (NewRevisionNumber != 2) == false
// @var int	 = The number from the JSON field NewRevisionNumber of the current respone
func parseBodyRev(resp *http.Response) (bool, int) {

	innerStruct := struct {
		StatusCode        int    `json:"status_code"`
		Description       string `json:"description"`
		NewRevisionNumber int    `json:"new_revision_number"`
	}{}

	body, errBody := ioutil.ReadAll(resp.Body)
	if errBody != nil {
		errMsg := fmt.Sprintf("Can not read body content (OrigErr)-> %v", errBody)
		panic(errMsg)
	}

	errJSON := json.Unmarshal(body, &innerStruct)
	if errJSON != nil {
		errMsg := fmt.Sprintf("Can not decode JSON from body (OrigErr)-> %v", errJSON)
		panic(errMsg)
	}

	if innerStruct.NewRevisionNumber != 2 {
		return false, innerStruct.NewRevisionNumber
	}

	return true, innerStruct.NewRevisionNumber
}
