func (suite *OcppJTestSuite) TestClientEnqueueRequest() {
	t := suite.T()
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	err := suite.chargePoint.Start("someUrl")
	require.Nil(t, err)
	req := newMockRequest("somevalue")
	err = suite.chargePoint.SendRequest(req)
	require.Nil(t, err)
	time.Sleep(500 * time.Millisecond)
	require.False(t, suite.clientRequestQueue.IsEmpty())
	assert.Equal(t, 1, suite.clientRequestQueue.Size())
	peeked := suite.clientRequestQueue.Peek()
	require.NotNil(t, peeked)
	bundle, ok := peeked.(ocppj.RequestBundle)
	require.True(t, ok)
	require.NotNil(t, bundle)
	assert.Equal(t, req.GetFeatureName(), bundle.Call.Action)
	marshaled, err := bundle.Call.MarshalJSON()
	require.Nil(t, err)
	assert.Equal(t, marshaled, bundle.Data)
}
----------------- Queue processing tests -----------------
package ocppj_test
import (
	"encoding/json"
	"fmt"
	"strconv"
	"sync"
	"time"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
	"github.com/lorenzodonini/ocpp-go/ocpp"
	"github.com/lorenzodonini/ocpp-go/ocppj"
)
func (suite *OcppJTestSuite) TestNewClient() {
	clientID := "mock_id"
	c := ocppj.NewClient(clientID, nil, nil, nil)
	assert.NotNil(suite.T(), c)
	assert.Equal(suite.T(), clientID, c.Id)
}
func (suite *OcppJTestSuite) TestChargePointStart() {
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	err := suite.chargePoint.Start("someUrl")
	assert.Nil(suite.T(), err)
	assert.True(suite.T(), suite.clientDispatcher.IsRunning())
}
func (suite *OcppJTestSuite) TestChargePointStartFailed() {
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(fmt.Errorf("startError"))
	err := suite.chargePoint.Start("someUrl")
	assert.NotNil(suite.T(), err)
}
func (suite *OcppJTestSuite) TestClientNotStartedError() {
	t := suite.T()
	req := newMockRequest("somevalue")
	err := suite.chargePoint.SendRequest(req)
	require.NotNil(t, err)
	assert.Equal(t, "ocppj client is not started, couldn't send request", err.Error())
	require.True(t, suite.clientRequestQueue.IsEmpty())
}
func (suite *OcppJTestSuite) TestClientStoppedError() {
	t := suite.T()
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	suite.mockClient.On("Stop").Return(nil)
	err := suite.chargePoint.Start("someUrl")
	require.NoError(t, err)
	suite.chargePoint.Stop()
	time.Sleep(20 * time.Millisecond)
	assert.False(t, suite.clientDispatcher.IsRunning())
	req := newMockRequest("somevalue")
	err = suite.chargePoint.SendRequest(req)
	assert.Error(t, err, "ocppj client is not started, couldn't send request")
}
func (suite *OcppJTestSuite) TestChargePointSendRequest() {
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	_ = suite.chargePoint.Start("someUrl")
	mockRequest := newMockRequest("mockValue")
	err := suite.chargePoint.SendRequest(mockRequest)
	assert.Nil(suite.T(), err)
}
func (suite *OcppJTestSuite) TestChargePointSendInvalidRequest() {
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	_ = suite.chargePoint.Start("someUrl")
	mockRequest := newMockRequest("")
	err := suite.chargePoint.SendRequest(mockRequest)
	assert.NotNil(suite.T(), err)
}
func (suite *OcppJTestSuite) TestChargePointSendInvalidJsonRequest() {
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	_ = suite.chargePoint.Start("someUrl")
	mockRequest := newMockRequest("somevalue")
	mockRequest.MockAny = make(chan int)
	err := suite.chargePoint.SendRequest(mockRequest)
	require.Error(suite.T(), err)
	assert.IsType(suite.T(), &json.UnsupportedTypeError{}, err)
}
func (suite *OcppJTestSuite) TestChargePointSendInvalidCall() {
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	_ = suite.chargePoint.Start("someUrl")
	mockRequest := newMockRequest("somevalue")
	suite.chargePoint.Profiles = []*ocpp.Profile{}
	err := suite.chargePoint.SendRequest(mockRequest)
	assert.Error(suite.T(), err, fmt.Sprintf("Couldn't create Call for unsupported action %v", mockRequest.GetFeatureName()))
}
func (suite *OcppJTestSuite) TestChargePointSendRequestFailed() {
	t := suite.T()
	var callID string
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	suite.mockClient.On("Write", mock.Anything).Return(fmt.Errorf("networkError")).Run(func(args mock.Arguments) {
		require.False(t, suite.clientRequestQueue.IsEmpty())
		req := suite.clientRequestQueue.Peek().(ocppj.RequestBundle)
		callID = req.Call.GetUniqueId()
		_, ok := suite.chargePoint.RequestState.GetPendingRequest(callID)
		assert.True(t, ok)
	})
	_ = suite.chargePoint.Start("someUrl")
	mockRequest := newMockRequest("mockValue")
	err := suite.chargePoint.SendRequest(mockRequest)
	assert.Nil(t, err)
	time.Sleep(500 * time.Millisecond)
	_, ok := suite.chargePoint.RequestState.GetPendingRequest(callID)
	assert.False(t, ok)
}
func (suite *OcppJTestSuite) TestChargePointSendConfirmation() {
	t := suite.T()
	mockUniqueId := "1234"
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	_ = suite.chargePoint.Start("someUrl")
	mockConfirmation := newMockConfirmation("mockValue")
	err := suite.chargePoint.SendResponse(mockUniqueId, mockConfirmation)
	assert.Nil(t, err)
}
func (suite *OcppJTestSuite) TestChargePointSendInvalidConfirmation() {
	t := suite.T()
	mockUniqueId := "6789"
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	_ = suite.chargePoint.Start("someUrl")
	mockConfirmation := newMockConfirmation("")
	err := suite.chargePoint.SendResponse(mockUniqueId, mockConfirmation)
	assert.NotNil(t, err)
}
func (suite *OcppJTestSuite) TestChargePointSendConfirmationFailed() {
	t := suite.T()
	mockUniqueId := "1234"
	suite.mockClient.On("Write", mock.Anything).Return(fmt.Errorf("networkError"))
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	_ = suite.chargePoint.Start("someUrl")
	mockConfirmation := newMockConfirmation("mockValue")
	err := suite.chargePoint.SendResponse(mockUniqueId, mockConfirmation)
	assert.NotNil(t, err)
	assert.Equal(t, "networkError", err.Error())
}
func (suite *OcppJTestSuite) TestChargePointSendError() {
	t := suite.T()
	mockUniqueId := "1234"
	mockDescription := "mockDescription"
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	err := suite.chargePoint.SendError(mockUniqueId, ocppj.GenericError, mockDescription, nil)
	assert.Nil(t, err)
}
func (suite *OcppJTestSuite) TestChargePointSendInvalidError() {
	t := suite.T()
	mockUniqueId := "6789"
	mockDescription := "mockDescription"
	suite.mockClient.On("Write", mock.Anything).Return(nil)
	err := suite.chargePoint.SendError(mockUniqueId, "InvalidErrorCode", mockDescription, nil)
	assert.NotNil(t, err)
}
func (suite *OcppJTestSuite) TestChargePointSendErrorFailed() {
	t := suite.T()
	mockUniqueId := "1234"
	suite.mockClient.On("Write", mock.Anything).Return(fmt.Errorf("networkError"))
	mockConfirmation := newMockConfirmation("mockValue")
	err := suite.chargePoint.SendResponse(mockUniqueId, mockConfirmation)
	assert.NotNil(t, err)
	assert.Equal(t, "networkError", err.Error())
}
func (suite *OcppJTestSuite) TestChargePointCallHandler() {
	t := suite.T()
	mockUniqueId := "5678"
	mockValue := "someValue"
	mockRequest := fmt.Sprintf(`[2,"%v","%v",{"mockValue":"%v"}]`, mockUniqueId, MockFeatureName, mockValue)
	suite.chargePoint.SetRequestHandler(func(request ocpp.Request, requestId string, action string) {
		assert.Equal(t, mockUniqueId, requestId)
		assert.Equal(t, MockFeatureName, action)
		assert.NotNil(t, request)
	})
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil).Run(func(args mock.Arguments) {
		err := suite.mockClient.MessageHandler([]byte(mockRequest))
		assert.Nil(t, err)
	})
	err := suite.chargePoint.Start("somePath")
	assert.Nil(t, err)
}
func (suite *OcppJTestSuite) TestChargePointCallResultHandler() {
	t := suite.T()
	mockUniqueId := "5678"
	mockValue := "someValue"
	mockRequest := newMockRequest("testValue")
	mockConfirmation := fmt.Sprintf(`[3,"%v",{"mockValue":"%v"}]`, mockUniqueId, mockValue)
	suite.chargePoint.SetResponseHandler(func(confirmation ocpp.Response, requestId string) {
		assert.Equal(t, mockUniqueId, requestId)
		assert.NotNil(t, confirmation)
	})
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	suite.chargePoint.RequestState.AddPendingRequest(mockUniqueId, mockRequest)  
	err := suite.chargePoint.Start("somePath")
	assert.Nil(t, err)
	err = suite.mockClient.MessageHandler([]byte(mockConfirmation))
	assert.Nil(t, err)
}
func (suite *OcppJTestSuite) TestChargePointCallErrorHandler() {
	t := suite.T()
	mockUniqueId := "5678"
	mockErrorCode := ocppj.GenericError
	mockErrorDescription := "Mock Description"
	mockValue := "someValue"
	mockErrorDetails := make(map[string]interface{})
	mockErrorDetails["details"] = "someValue"
	mockRequest := newMockRequest("testValue")
	mockError := fmt.Sprintf(`[4,"%v","%v","%v",{"details":"%v"}]`, mockUniqueId, mockErrorCode, mockErrorDescription, mockValue)
	suite.chargePoint.SetErrorHandler(func(err *ocpp.Error, details interface{}) {
		assert.Equal(t, mockUniqueId, err.MessageId)
		assert.Equal(t, mockErrorCode, err.Code)
		assert.Equal(t, mockErrorDescription, err.Description)
		assert.Equal(t, mockErrorDetails, details)
	})
	suite.mockClient.On("Start", mock.AnythingOfType("string")).Return(nil)
	suite.chargePoint.RequestState.AddPendingRequest(mockUniqueId, mockRequest)  
	err := suite.chargePoint.Start("someUrl")
	assert.Nil(t, err)
	err = suite.mockClient.MessageHandler([]byte(mockError))
	assert.Nil(t, err)
}