github.com/Financial-Times/publish-availability-monitor@v1.12.0/feeds/notificationsPushFeed_test.go (about)

     1  package feeds
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/Financial-Times/go-logger/v2"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type mockPushNotificationsStream struct {
    15  	notifications []string
    16  	index         int
    17  	log           *logger.UPPLogger
    18  }
    19  
    20  func (resp *mockPushNotificationsStream) Read(p []byte) (n int, err error) {
    21  	var data []byte
    22  	if resp.index >= len(resp.notifications) {
    23  		data = []byte("data: []\n")
    24  	} else {
    25  		data = []byte("data: [" + resp.notifications[resp.index] + "]\n")
    26  		resp.index++
    27  		resp.log.Infof("data: %v", string(data))
    28  	}
    29  	actual := len(data)
    30  	for i := 0; i < actual; i++ {
    31  		p[i] = data[i]
    32  	}
    33  
    34  	return actual, nil
    35  }
    36  
    37  func (resp *mockPushNotificationsStream) Close() error {
    38  	return nil
    39  }
    40  
    41  func buildOKPushResponse(notifications []string, log *logger.UPPLogger) *mockResponse {
    42  	stream := &mockPushNotificationsStream{
    43  		notifications: notifications,
    44  		log:           log,
    45  	}
    46  	return &mockResponse{
    47  		&http.Response{
    48  			StatusCode: 200,
    49  			Body:       stream,
    50  		}, nil}
    51  }
    52  
    53  func TestPushNotificationsAreConsumed(t *testing.T) {
    54  	uuid := "1cb14245-5185-4ed5-9188-4d2a86085599"
    55  	publishRef := "tid_0123wxyz"
    56  	lastModified := time.Now()
    57  	notifications := mockNotificationFor(uuid, publishRef, lastModified)
    58  	notifications = strings.Replace(notifications, "\n", "", -1)
    59  	log := logger.NewUPPLogger("test", "INFO")
    60  
    61  	httpResponse := buildOKPushResponse([]string{notifications}, log)
    62  	httpCaller := mockHTTPCaller(t, "tid_pam_notifications_push_", httpResponse)
    63  
    64  	baseURL, _ := url.Parse("http://www.example.org")
    65  	f := NewNotificationsFeed("notifications-push", *baseURL, 10, 1, "", "", "", log)
    66  	f.(*NotificationsPushFeed).SetHTTPCaller(httpCaller)
    67  	f.Start()
    68  	defer f.Stop()
    69  
    70  	time.Sleep(time.Duration(100) * time.Millisecond)
    71  
    72  	response := f.NotificationsFor(uuid)
    73  	assert.Len(t, response, 1, "notifications for item")
    74  	assert.Equal(t, publishRef, response[0].PublishReference, "publish ref")
    75  }
    76  
    77  func TestListPushNotificationsAreConsumed(t *testing.T) {
    78  	uuid := "1cb14245-5185-4ed5-9188-4d2a86085599"
    79  	publishRef := "tid_0123wxyz"
    80  	lastModified := time.Now()
    81  	notifications := mockNotificationFor(uuid, publishRef, lastModified)
    82  	notifications = strings.Replace(notifications, "\n", "", -1)
    83  	log := logger.NewUPPLogger("test", "INFO")
    84  
    85  	httpResponse := buildOKPushResponse([]string{notifications}, log)
    86  	httpCaller := mockHTTPCaller(t, "tid_pam_notifications_push_", httpResponse)
    87  
    88  	baseURL, _ := url.Parse("http://www.example.org")
    89  	f := NewNotificationsFeed("list-notifications-push", *baseURL, 10, 1, "", "", "", log)
    90  	f.(*NotificationsPushFeed).SetHTTPCaller(httpCaller)
    91  	f.Start()
    92  	defer f.Stop()
    93  
    94  	time.Sleep(time.Duration(100) * time.Millisecond)
    95  
    96  	response := f.NotificationsFor(uuid)
    97  	assert.Len(t, response, 1, "notifications for item")
    98  	assert.Equal(t, publishRef, response[0].PublishReference, "publish ref")
    99  }
   100  
   101  func TestPushNotificationsForReturnsEmptyIfNotFound(t *testing.T) {
   102  	baseURL, _ := url.Parse("http://www.example.org")
   103  	log := logger.NewUPPLogger("test", "INFO")
   104  	f := NewNotificationsFeed("notifications-push", *baseURL, 10, 1, "", "", "", log)
   105  
   106  	response := f.NotificationsFor("1cb14245-5185-4ed5-9188-4d2a86085599")
   107  	assert.Len(t, response, 0, "notifications for item")
   108  }
   109  
   110  func TestPushNotificationsForReturnsAllMatches(t *testing.T) {
   111  	uuid := "1cb14245-5185-4ed5-9188-4d2a86085599"
   112  	publishRef1 := "tid_0123wxyz"
   113  	lastModified1 := time.Now().Add(time.Duration(-1) * time.Second)
   114  	notification1 := mockNotificationFor(uuid, publishRef1, lastModified1)
   115  
   116  	publishRef2 := "tid_0123abcd"
   117  	lastModified2 := time.Now()
   118  	notification2 := mockNotificationFor(uuid, publishRef2, lastModified2)
   119  	log := logger.NewUPPLogger("test", "INFO")
   120  
   121  	httpResponses := buildOKPushResponse([]string{
   122  		strings.Replace(notification1, "\n", "", -1),
   123  		strings.Replace(notification2, "\n", "", -1),
   124  	}, log)
   125  	httpCaller := mockHTTPCaller(t, "tid_pam_notifications_push_", httpResponses)
   126  
   127  	baseURL, _ := url.Parse("http://www.example.org")
   128  	f := NewNotificationsFeed("notifications-push", *baseURL, 10, 1, "", "", "", log)
   129  	f.(*NotificationsPushFeed).SetHTTPCaller(httpCaller)
   130  	f.Start()
   131  	defer f.Stop()
   132  	time.Sleep(time.Duration(2200) * time.Millisecond)
   133  
   134  	response := f.NotificationsFor(uuid)
   135  	assert.Len(t, response, 2, "notifications for item")
   136  	assert.Equal(t, publishRef1, response[0].PublishReference, "first publish ref")
   137  	assert.Equal(t, publishRef2, response[1].PublishReference, "second publish ref")
   138  }
   139  
   140  func TestPushNotificationsPollingContinuesAfterErrorResponse(t *testing.T) {
   141  	uuid := "1cb14245-5185-4ed5-9188-4d2a86085599"
   142  	publishRef := "tid_0123wxyz"
   143  	lastModified := time.Now()
   144  	notification := mockNotificationFor(uuid, publishRef, lastModified)
   145  	log := logger.NewUPPLogger("test", "INFO")
   146  
   147  	httpResponse := buildOKPushResponse([]string{strings.Replace(notification, "\n", "", -1)}, log)
   148  	httpCaller := mockHTTPCaller(t, "tid_pam_notifications_push_", buildResponse(500, "", nil), httpResponse)
   149  
   150  	baseURL, _ := url.Parse("http://www.example.org")
   151  	f := NewNotificationsFeed("notifications-push", *baseURL, 10, 1, "", "", "", log)
   152  	f.(*NotificationsPushFeed).SetHTTPCaller(httpCaller)
   153  	f.Start()
   154  	defer f.Stop()
   155  	time.Sleep(time.Duration(550) * time.Millisecond)
   156  
   157  	response := f.NotificationsFor(uuid)
   158  	assert.Len(t, response, 1, "notifications for item")
   159  	assert.Equal(t, publishRef, response[0].PublishReference, "publish ref")
   160  }
   161  
   162  func TestPushNotificationsArePurged(t *testing.T) {
   163  	uuid := "1cb14245-5185-4ed5-9188-4d2a86085599"
   164  	publishRef := "tid_0123wxyz"
   165  	lastModified := time.Now().Add(time.Duration(-2) * time.Second)
   166  	notifications := mockNotificationFor(uuid, publishRef, lastModified)
   167  	notifications = strings.Replace(notifications, "\n", "", -1)
   168  	log := logger.NewUPPLogger("test", "INFO")
   169  
   170  	httpResponse := buildOKPushResponse([]string{notifications}, log)
   171  	httpCaller := mockHTTPCaller(t, "tid_pam_notifications_push_", httpResponse)
   172  
   173  	baseURL, _ := url.Parse("http://www.example.org")
   174  	f := NewNotificationsFeed("notifications-push", *baseURL, 1, 1, "", "", "", log)
   175  	f.(*NotificationsPushFeed).SetHTTPCaller(httpCaller)
   176  	f.Start()
   177  	defer f.Stop()
   178  
   179  	time.Sleep(time.Duration(500) * time.Millisecond)
   180  
   181  	response := f.NotificationsFor(uuid)
   182  	assert.Len(t, response, 1, "notifications for item")
   183  	assert.Equal(t, publishRef, response[0].PublishReference, "publish ref")
   184  
   185  	time.Sleep(time.Duration(2) * time.Second)
   186  	response = f.NotificationsFor(uuid)
   187  	assert.Len(t, response, 0, "notifications for item")
   188  }
   189  
   190  func TestPushNotificationsSendsAuthentication(t *testing.T) {
   191  	uuid := "1cb14245-5185-4ed5-9188-4d2a86085599"
   192  	publishRef := "tid_0123wxyz"
   193  	lastModified := time.Now()
   194  	notifications := mockNotificationFor(uuid, publishRef, lastModified)
   195  	notifications = strings.Replace(notifications, "\n", "", -1)
   196  	log := logger.NewUPPLogger("test", "INFO")
   197  
   198  	httpResponse := buildOKPushResponse([]string{notifications}, log)
   199  
   200  	baseURL, _ := url.Parse("http://www.example.org")
   201  	f := NewNotificationsFeed("notifications-push", *baseURL, 10, 1, "someUser", "somePwd", "someApiKey", log)
   202  	httpCaller := mockAuthenticatedHTTPCaller(t, "tid_pam_notifications_push_", "someUser", "somePwd", "someApiKey", httpResponse)
   203  	f.(*NotificationsPushFeed).SetHTTPCaller(httpCaller)
   204  
   205  	f.Start()
   206  	defer f.Stop()
   207  
   208  	time.Sleep(time.Duration(500) * time.Millisecond)
   209  
   210  	response := f.NotificationsFor(uuid)
   211  	assert.Len(t, response, 1, "notifications for item")
   212  }