func TestGet(t *testing.T) {
	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test Get when config file doesn't exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}
	_, err = get()
	if err == nil {
		t.Error("Expected to receive error trying to get client when config file does not exist")
	}

	// test GetClient with valid endpoint in config file
	endpoint := "http://localhost:9080"
	testGetEndpoint(t, endpoint, false, true)

	endpoint = "https://172.31.253.66:443"
	//test Get for a https endpoint with secure endpoint = true & skipping verify = true
	testGetEndpoint(t, endpoint, true, true)

	//test Get for a https endpoint with secure endpoint = true & skipping verify = false
	testGetEndpoint(t, endpoint, true, false)

	//Restore the original configuration
	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
func TestGetStatus(t *testing.T) {
	// test GetStatus when config file doesn't exist
	err := cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}
	set := flag.NewFlagSet("test", 0)
	cxt := cli.NewContext(nil, set, nil)
	err = getStatus(cxt, os.Stdout)
	if err == nil {
		t.Error("Expected to receive error trying to get status when config file does not exist")
	}

	// test GetStatus with mock client and mock server
	expectedStruct := photon.Status{
		Status: "READY",
		Components: []photon.Component{
			{Component: "chairman", Message: "", Status: "READY"},
			{Component: "housekeeper", Message: "", Status: "READY"},
		},
	}
	response, err := json.Marshal(expectedStruct)
	if err != nil {
		t.Error("Not expecting error serializing expected status")
	}

	server := mocks.NewTestServer()
	mocks.RegisterResponder(
		"GET",
		server.URL+"/status",
		mocks.CreateResponder(200, string(response[:])))
	defer server.Close()

	mocks.Activate(true)
	httpClient := &http.Client{Transport: mocks.DefaultMockTransport}
	client.Esxclient = photon.NewTestClient(server.URL, nil, httpClient)

	err = getStatus(cxt, os.Stdout)
	if err != nil {
		t.Error("Not expecting error getting status of mock client")
	}
}
func TestSetEndpoint(t *testing.T) {
	var endpoint string

	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test SetEndpoint when config file does not exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}

	endpoint = "endpoint"
	set := flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"endpoint"})
	if err != nil {
		t.Error("Not expecting arguments parsing to fail")
	}
	set.Bool("nocertcheck", true, "")
	cxt := cli.NewContext(nil, set, nil)
	err = setEndpoint(cxt)
	if err != nil {
		t.Error("Not expecting error when setting endpoint")
	}

	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.CloudTarget != endpoint {
		t.Error("Endpoint read from file not match what's written to file")
	}

	// test SetEndpoint when overwriting existing endpoint
	configExpected := &cf.Configuration{
		CloudTarget:       "test-setendpoint",
		Token:             "test-setendpoint",
		IgnoreCertificate: true,
	}
	err = cf.SaveConfig(configExpected)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}

	endpoint = "endpoint-overwrite"
	set = flag.NewFlagSet("test", 0)
	err = set.Parse([]string{"endpoint-overwrite"})
	if err != nil {
		t.Error("Not expecting arguments parsign to fail")
	}
	set.Bool("nocertcheck", true, "")
	cxt = cli.NewContext(nil, set, nil)
	err = setEndpoint(cxt)
	if err != nil {
		t.Error("Not expecting error when overwritting endpoint in file")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.CloudTarget != endpoint {
		t.Error("Endpoint read from file not match what's written to file")
	}

	configRead.CloudTarget = configExpected.CloudTarget
	if *configRead != *configExpected {
		t.Error("Other configurations changed when setting only cloudtarget")
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
func TestLogout(t *testing.T) {
	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test Logout when config file does not exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}
	set := flag.NewFlagSet("test", 0)
	cxt := cli.NewContext(nil, set, nil)
	err = logout(cxt)
	if err != nil {
		t.Error("Not expecting error when logging out")
	}

	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != "" {
		t.Error("Token expected to be empty after logout")
	}

	// test Logout when config file exists
	configExpected := &cf.Configuration{
		CloudTarget: "test-logout",
		Token:       "test-logout",
	}
	err = cf.SaveConfig(configExpected)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}

	err = logout(cxt)
	if err != nil {
		t.Error("Not expecting error when logging out")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != "" {
		t.Error("Token expected to be empty after logout")
	}

	configRead.Token = configExpected.Token
	if *configRead != *configExpected {
		t.Error("Other configurations changed when removing only token")
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}
func TestLogin(t *testing.T) {
	var token string

	configOri, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	// test Login when config file does not exist
	err = cf.RemoveConfigFile()
	if err != nil {
		t.Error("Not expecting error removing config file")
	}

	token = "token"
	set := flag.NewFlagSet("test", 0)
	set.String("access_token", token, "")
	cxt := cli.NewContext(nil, set, nil)
	err = login(cxt)
	if err != nil {
		t.Error("Not expecting error when logging in")
	}

	configRead, err := cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != token {
		t.Error("Token read from file not match what's written to file")
	}

	// test Login when overwriting existing endpoint
	configExpected := &cf.Configuration{
		CloudTarget: "test-login",
		Token:       "test-login",
	}
	err = cf.SaveConfig(configExpected)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}

	token = "token-overwrite"
	set = flag.NewFlagSet("test", 0)
	set.String("access_token", token, "")
	cxt = cli.NewContext(nil, set, nil)
	err = login(cxt)
	if err != nil {
		t.Error("Not expecting error when overwritting token in file")
	}

	configRead, err = cf.LoadConfig()
	if err != nil {
		t.Error("Not expecting error loading config file")
	}

	if configRead.Token != token {
		t.Error("Token read from file not match what's written to file")
	}

	configRead.Token = configExpected.Token
	if *configRead != *configExpected {
		t.Error("Other configurations changed when setting only token")
	}

	err = cf.SaveConfig(configOri)
	if err != nil {
		t.Error("Not expecting error when saving config file")
	}
}