package collector

import (
	"bytes"
	"encoding/json"
	"fmt"
	"git.loafle.net/overflow/overflow_probe/discovery/discovery/types/timestamp"
	"io/ioutil"
	"net/http"
	"strconv"
	"testing"
	"time"
)

func TestTimeStampe(t *testing.T) {

	aa := "1479186033399"

	i, err := strconv.ParseInt(aa, 10, 64)
	if err != nil {
		panic(err)
	}
	tm := time.Unix(i/1000, 0)
	fmt.Println(tm)

}

func TestJPAInsert(t *testing.T) {

	c := &Collector{}
	c.Version = "2.0"
	c.ConfigPath = "/root"
	c.LicenseType = LICENSE1
	c.ProductId = "6666"
	c.InstallDate = timestamp.Now()
	c.UpdateDate = timestamp.Now()
	c.LicenseDueDate = timestamp.Date(2017, 12, 25)

	b, err := json.Marshal(c)
	if err != nil {
		fmt.Println(err)
	}

	http.Post("http://localhost:8080/collector", "application/x-spring-data-verbose+json", bytes.NewBuffer(b))
}

func TestJPASelect(t *testing.T) {
	//res, err := http.Get("http://localhost:8080/collector/1")
	res, err := http.Get("http://localhost:8080/collector/p/6666")
	if err != nil {
		fmt.Println(err)
	}

	body, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if len(body) <= 0 {
		t.Error("not found.")
		return
	}

	if err != nil {
		fmt.Println(err)
	}

	var result Collector
	json.Unmarshal(body, &result)
	t.Log(result)

	t.Log("ID : ", result.ID)
	t.Log("productID : ", result.ProductId)
	t.Log("version : ", result.Version)
}
