[RICPLT-1703] - Reset Request + unit tests

Change-Id: Id650f265e54d1cac230342395c6babb1073631f8
Signed-off-by: rh362j <ronen.hasid@intl.att.com>
diff --git a/E2Manager/controllers/controller_test.go b/E2Manager/controllers/controller_test.go
index 6bc1613..4d0774e 100644
--- a/E2Manager/controllers/controller_test.go
+++ b/E2Manager/controllers/controller_test.go
@@ -18,6 +18,7 @@
 package controllers
 
 import (
+	"bytes"
 	"e2mgr/configuration"
 	"e2mgr/e2managererrors"
 	"e2mgr/logger"
@@ -25,16 +26,21 @@
 	"e2mgr/models"
 	"e2mgr/providers"
 	"e2mgr/rNibWriter"
+	"e2mgr/rmrCgo"
 	"e2mgr/tests"
 	"encoding/json"
+	"fmt"
 	"gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
 	"gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
 	"gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
+	"github.com/julienschmidt/httprouter"
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/mock"
 	"io"
 	"io/ioutil"
 	"net/http"
 	"net/http/httptest"
+	"strings"
 	"testing"
 )
 
@@ -214,3 +220,176 @@
 	}
 	return log
 }
+
+
+func TestX2ResetHandleSuccessfulRequestedCause(t *testing.T){
+	log := initLog(t)
+
+	ranName := "test1"
+
+	readerMock := &mocks.RnibReaderMock{}
+	readerProvider := func() reader.RNibReader {
+		return readerMock
+	}
+	writerMock := &mocks.RnibWriterMock{}
+	writerProvider := func() rNibWriter.RNibWriter {
+		return writerMock
+	}
+	payload:= []byte {0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x40}
+	xaction := []byte(ranName)
+	msg:= rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
+	rmrMessengerMock := &mocks.RmrMessengerMock{}
+	rmrMessengerMock.On("SendMsg",msg,mock.Anything).Return(msg,nil)
+
+	config := configuration.ParseConfiguration()
+	rmrService:=getRmrService(rmrMessengerMock, log)
+
+	writer := httptest.NewRecorder()
+	controller := NewController(log, rmrService, readerProvider, writerProvider, config)
+
+	var nodeb = &entities.NodebInfo{ConnectionStatus:  entities.ConnectionStatus_CONNECTED }
+	readerMock.On("GetNodeb",ranName).Return(nodeb, nil)
+
+	data4Req := map[string]interface{}{"cause": "protocol:transfer-syntax-error"}
+	b := new(bytes.Buffer)
+	_ = json.NewEncoder(b).Encode(data4Req)
+	req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
+
+	param:= httprouter.Param{Key:"ranName", Value: ranName}
+	controller.X2ResetHandler(writer, req, []httprouter.Param {param})
+	assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
+
+	v, ok := rmrService.E2sessions[ranName]
+	assert.True(t, ok)
+
+	assert.Equal(t, v.Request.RanName, ranName)
+}
+
+func TestX2ResetHandleSuccessfulRequestedDefault(t *testing.T){
+	log := initLog(t)
+
+	ranName := "test1"
+
+	readerMock := &mocks.RnibReaderMock{}
+	readerProvider := func() reader.RNibReader {
+		return readerMock
+	}
+	writerMock := &mocks.RnibWriterMock{}
+	writerProvider := func() rNibWriter.RNibWriter {
+		return writerMock
+	}
+	// o&m intervention
+	payload:= []byte {0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x64}
+	xaction := []byte(ranName)
+	msg:= rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
+	rmrMessengerMock := &mocks.RmrMessengerMock{}
+	rmrMessengerMock.On("SendMsg",msg,mock.Anything).Return(msg,nil)
+
+	config := configuration.ParseConfiguration()
+	rmrService:=getRmrService(rmrMessengerMock, log)
+
+	writer := httptest.NewRecorder()
+	controller := NewController(log, rmrService, readerProvider, writerProvider, config)
+
+	var nodeb = &entities.NodebInfo{ConnectionStatus:  entities.ConnectionStatus_CONNECTED }
+	readerMock.On("GetNodeb",ranName).Return(nodeb, nil)
+
+	// no body
+	b := new(bytes.Buffer)
+	req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
+
+	param:= httprouter.Param{Key:"ranName", Value: ranName}
+	controller.X2ResetHandler(writer, req, []httprouter.Param {param})
+	assert.Equal(t, http.StatusNoContent, writer.Result().StatusCode)
+
+	v, ok := rmrService.E2sessions[ranName]
+	assert.True(t, ok)
+
+	assert.Equal(t, v.Request.RanName, ranName)
+}
+
+func TestX2ResetHandleFailureInvalidBody(t *testing.T){
+	log := initLog(t)
+
+	ranName := "test1"
+
+	readerMock := &mocks.RnibReaderMock{}
+	readerProvider := func() reader.RNibReader {
+		return readerMock
+	}
+	writerMock := &mocks.RnibWriterMock{}
+	writerProvider := func() rNibWriter.RNibWriter {
+		return writerMock
+	}
+	rmrMessengerMock := &mocks.RmrMessengerMock{}
+
+	config := configuration.ParseConfiguration()
+	rmrService:=getRmrService(rmrMessengerMock, log)
+
+	writer := httptest.NewRecorder()
+	controller := NewController(log, rmrService, readerProvider, writerProvider, config)
+
+	// Invalid json: attribute name without quotes (should be "cause":).
+	b := strings.NewReader("{cause:\"protocol:transfer-syntax-error\"")
+	req, _ := http.NewRequest("PUT", "https://localhost:3800/nodeb-reset", b)
+
+	param:= httprouter.Param{Key:"ranName", Value: ranName}
+	controller.X2ResetHandler(writer, req, []httprouter.Param {param})
+	assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
+
+	_, ok := rmrService.E2sessions[ranName]
+	assert.False(t, ok)
+
+}
+
+func TestHandleErrorResponse(t *testing.T){
+	log := initLog(t)
+
+	readerMock := &mocks.RnibReaderMock{}
+	readerProvider := func() reader.RNibReader {
+		return readerMock
+	}
+	writerMock := &mocks.RnibWriterMock{}
+	writerProvider := func() rNibWriter.RNibWriter {
+		return writerMock
+	}
+	rmrMessengerMock := &mocks.RmrMessengerMock{}
+
+	config := configuration.ParseConfiguration()
+	rmrService:=getRmrService(rmrMessengerMock, log)
+
+
+	controller := NewController(log, rmrService, readerProvider, writerProvider, config)
+
+	writer := httptest.NewRecorder()
+	controller.handleErrorResponse(e2managererrors.NewRnibDbError(),writer)
+	assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
+
+	writer = httptest.NewRecorder()
+	controller.handleErrorResponse(e2managererrors.NewCommandAlreadyInProgressError(),writer)
+	assert.Equal(t, http.StatusMethodNotAllowed, writer.Result().StatusCode)
+
+	writer = httptest.NewRecorder()
+	controller.handleErrorResponse(e2managererrors.NewHeaderValidationError(),writer)
+	assert.Equal(t, http.StatusUnsupportedMediaType, writer.Result().StatusCode)
+
+	writer = httptest.NewRecorder()
+	controller.handleErrorResponse(e2managererrors.NewWrongStateError(""),writer)
+	assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
+
+	writer = httptest.NewRecorder()
+	controller.handleErrorResponse(e2managererrors.NewRequestValidationError(),writer)
+	assert.Equal(t, http.StatusBadRequest, writer.Result().StatusCode)
+
+	writer = httptest.NewRecorder()
+	controller.handleErrorResponse(e2managererrors.NewRmrError(),writer)
+	assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
+
+	writer = httptest.NewRecorder()
+	controller.handleErrorResponse(e2managererrors.NewResourceNotFoundError(),writer)
+	assert.Equal(t, http.StatusNotFound, writer.Result().StatusCode)
+
+	writer = httptest.NewRecorder()
+	controller.handleErrorResponse(fmt.Errorf("ErrorError"),writer)
+	assert.Equal(t, http.StatusInternalServerError, writer.Result().StatusCode)
+}