From: srinivasyanamadala Date: Wed, 8 Jan 2025 10:23:02 +0000 (+0100) Subject: Code clean up and csit robot test error fix X-Git-Tag: 1.0.0~4 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=29b8ea826337f8132023b1d0c9ea60b62512fa61;p=policy%2Fopa-pdp.git Code clean up and csit robot test error fix Issue-ID: POLICY-5220 Change-Id: I8fcdd65119c7508e40a48ee65dfc8c43d7a3a784 Signed-off-by: srinivasyanamadala --- diff --git a/cmd/opa-pdp/opa-pdp.go b/cmd/opa-pdp/opa-pdp.go index f2b2e0b..b48bd27 100644 --- a/cmd/opa-pdp/opa-pdp.go +++ b/cmd/opa-pdp/opa-pdp.go @@ -1,6 +1,6 @@ // - // ========================LICENSE_START================================= -// Copyright (C) 2024: Deutsche Telekom +// Copyright (C) 2024-2025: Deutsche Telekom // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -94,7 +94,7 @@ func main() { } defer producer.Close() - sender := &publisher.RealPdpStatusSender{} + sender := &publisher.RealPdpStatusSender{Producer: producer} // pdp registration isRegistered := registerPDPFunc(sender) if !isRegistered { diff --git a/pkg/kafkacomm/publisher/1 b/pkg/kafkacomm/publisher/1 deleted file mode 100644 index bfd5272..0000000 --- a/pkg/kafkacomm/publisher/1 +++ /dev/null @@ -1,148 +0,0 @@ -// - -// ========================LICENSE_START================================= -// Copyright (C) 2024: Deutsche Telekom -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// SPDX-License-Identifier: Apache-2.0 -// ========================LICENSE_END=================================== -// - -package publisher - -import ( - /* "fmt" - "policy-opa-pdp/cfg" - "policy-opa-pdp/consts" - "policy-opa-pdp/pkg/log" - "policy-opa-pdp/pkg/model" - "policy-opa-pdp/pkg/pdpstate"*/ - "errors" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" - "policy-opa-pdp/pkg/kafkacomm/publisher/mocks" - "testing" - // "time" - /* "github.com/google/uuid"*/) - -var ( -// ticker *time.Ticker -// stopChan chan bool -// currentInterval int64 -) - -/* -Success Case 1 -TestStartHeartbeatIntervalTimer_ValidInterval -Description: Test starting the heartbeat interval timer with a valid interval. -Input: intervalMs = 1000 -Expected Output: The ticker starts with an interval of 1000 milliseconds, and heartbeat messages are sent at this interval. -*/ -func TestStartHeartbeatIntervalTimer_ValidInterval(t *testing.T) { - - intervalMs := int64(1000) - mockSender := new(mocks.PdpStatusSender) - mockSender.On("SendPdpStatus", mock.Anything).Return(nil) - - StartHeartbeatIntervalTimer(intervalMs, mockSender) - mu.Lock() - defer mu.Unlock() - if ticker == nil { - t.Errorf("Expected ticker to be initialized") - } - if currentInterval != intervalMs { - t.Errorf("Expected currentInterval to be %d, got %d", intervalMs, currentInterval) - } -} - -/* -Failure Case 1 -TestStartHeartbeatIntervalTimer_InvalidInterval -Description: Test starting the heartbeat interval timer with an invalid interval. -Input: intervalMs = -1000 -Expected Output: The function should handle the invalid interval gracefully, possibly by logging an error message and not starting the ticker. -*/ -func TestStartHeartbeatIntervalTimer_InvalidInterval(t *testing.T) { - intervalMs := int64(-1000) - mockSender := new(mocks.PdpStatusSender) - mockSender.On("SendPdpStatus", mock.Anything).Return(nil) - - StartHeartbeatIntervalTimer(intervalMs, mockSender) - mu.Lock() - defer mu.Unlock() - - if ticker != nil { - t.Log("Expected ticker to be nil for invalid interval") - } -} - -/* -TestSendPDPHeartBeat_Success 2 -Description: Test sending a heartbeat successfully. -Input: Valid pdpStatus object -Expected Output: Heartbeat message is sent successfully, and a debug log "Message sent successfully" is generated. -*/ -/* -func TestSendPDPHeartBeat_Success(t *testing.T) { - - mockSender := new(mocks.PdpStatusSender) - mockSender.On("SendPdpStatus", mock.Anything).Return(nil) - err := sendPDPHeartBeat(mockSender) - assert.NoError(t, err) -} -*/ -/* -TestSendPDPHeartBeat_Failure 2 -Description: Test failing to send a heartbeat. -Input: Invalid pdpStatus object or network failure -Expected Output: An error occurs while sending the heartbeat, and a warning log "Error producing message: ..." is generated. -*/ -/* -func TestSendPDPHeartBeat_Failure(t *testing.T) { - // Mock SendPdpStatus to return an error - mockSender := new(mocks.PdpStatusSender) - mockSender.On("SendPdpStatus", mock.Anything).Return(errors.New("Error producing message")) - err := sendPDPHeartBeat(mockSender) - assert.Error(t, err) -} -*/ - -/* -TestStopTicker_Success 3 -Description: Test stopping the ticker. -Input: Ticker is running -Expected Output: The ticker stops, and the stop channel is closed. -*/ -func TestStopTicker_Success(t *testing.T) { - mockSender := new(mocks.PdpStatusSender) - mockSender.On("SendPdpStatus", mock.Anything).Return(nil) - StartHeartbeatIntervalTimer(1000, mockSender) - - StopTicker() - mu.Lock() - defer mu.Unlock() - if ticker != nil { - t.Errorf("Expected ticker to be nil") - } -} - -/* -TestStopTicker_NotRunning 3 -Description: Test stopping the ticker when it is not running. -Input: Ticker is not running -Expected Output: The function should handle this case gracefully, possibly by logging a debug message indicating that the ticker is not running. -*/ -func TestStopTicker_NotRunning(t *testing.T) { - StopTicker() - mu.Lock() - defer mu.Unlock() -} diff --git a/pkg/kafkacomm/publisher/pdp-pap-registration.go b/pkg/kafkacomm/publisher/pdp-pap-registration.go index 34fb44a..5525f61 100644 --- a/pkg/kafkacomm/publisher/pdp-pap-registration.go +++ b/pkg/kafkacomm/publisher/pdp-pap-registration.go @@ -45,7 +45,6 @@ type RealPdpStatusSender struct { func (s *RealPdpStatusSender) SendPdpStatus(pdpStatus model.PdpStatus) error { var topic string - // bootstrapServers := cfg.BootstrapServer topic = cfg.Topic pdpStatus.RequestID = uuid.New().String() pdpStatus.TimestampMs = fmt.Sprintf("%d", time.Now().UnixMilli()) @@ -55,12 +54,6 @@ func (s *RealPdpStatusSender) SendPdpStatus(pdpStatus model.PdpStatus) error { log.Warnf("failed to marshal PdpStatus to JSON: %v", err) return err } - /* producer, err := kafkacomm.GetKafkaProducer(bootstrapServers, topic) - if err != nil { - log.Warnf("Error creating Kafka producer: %v\n", err) - return err - }*/ - // s.Producer = producer log.Debugf("Producer saved in RealPdp StatusSender") kafkaMessage := &kafka.Message{