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

     1  package checks
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/Financial-Times/go-logger/v2"
     8  	"github.com/Financial-Times/publish-availability-monitor/feeds"
     9  	"github.com/google/uuid"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  const (
    14  	testEnv  = "testEnv"
    15  	feedName = "testFeed"
    16  )
    17  
    18  type testFeed struct {
    19  	feedName      string
    20  	feedType      string
    21  	uuid          string
    22  	notifications []*feeds.Notification
    23  }
    24  
    25  func (f testFeed) Start() {}
    26  func (f testFeed) Stop()  {}
    27  func (f testFeed) FeedName() string {
    28  	return f.feedName
    29  }
    30  func (f testFeed) FeedType() string {
    31  	return f.feedType
    32  }
    33  func (f testFeed) FeedURL() string {
    34  	return f.feedName
    35  }
    36  func (f testFeed) SetCredentials(string, string) {}
    37  func (f testFeed) NotificationsFor(uuid string) []*feeds.Notification {
    38  	return f.notifications
    39  }
    40  
    41  func mockFeed(name string, uuid string, notifications []*feeds.Notification) testFeed {
    42  	return testFeed{name, feeds.NotificationsPull, uuid, notifications}
    43  }
    44  
    45  func TestFeedContainsMatchingNotification(t *testing.T) {
    46  	testUUID := uuid.NewString()
    47  	testTID := "tid_0123wxyz"
    48  	testLastModified := "2016-10-28T14:00:00.000Z"
    49  
    50  	n := feeds.Notification{ID: testUUID, PublishReference: testTID, LastModified: testLastModified}
    51  	notifications := []*feeds.Notification{&n}
    52  	f := mockFeed(feedName, testUUID, notifications)
    53  	subscribedFeeds := make(map[string][]feeds.Feed)
    54  	subscribedFeeds[testEnv] = []feeds.Feed{f}
    55  
    56  	notificationsCheck := &NotificationsCheck{
    57  		mockHTTPCaller(t, "", nil),
    58  		subscribedFeeds,
    59  		feedName,
    60  	}
    61  	log := logger.NewUPPLogger("test", "PANIC")
    62  
    63  	pc := NewPublishCheck(
    64  		newPublishMetricBuilder().withUUID(testUUID).withPlatform(testEnv).withTID(testTID).build(),
    65  		"",
    66  		"",
    67  		0,
    68  		0,
    69  		nil,
    70  		nil,
    71  		log,
    72  	)
    73  	finished, _ := notificationsCheck.isCurrentOperationFinished(pc)
    74  	assert.True(t, finished, "Operation should be considered finished")
    75  }
    76  
    77  func TestFeedMissingNotification(t *testing.T) {
    78  	testUUID := uuid.NewString()
    79  	testTID := "tid_0123wxyz"
    80  
    81  	f := mockFeed(feedName, uuid.NewString(), []*feeds.Notification{})
    82  	subscribedFeeds := make(map[string][]feeds.Feed)
    83  	subscribedFeeds[testEnv] = []feeds.Feed{f}
    84  
    85  	notificationsCheck := &NotificationsCheck{
    86  		mockHTTPCaller(t, "", nil),
    87  		subscribedFeeds,
    88  		feedName,
    89  	}
    90  	log := logger.NewUPPLogger("test", "PANIC")
    91  
    92  	pc := NewPublishCheck(
    93  		newPublishMetricBuilder().withUUID(testUUID).withPlatform(testEnv).withTID(testTID).build(),
    94  		"",
    95  		"",
    96  		0,
    97  		0,
    98  		nil,
    99  		nil,
   100  		log,
   101  	)
   102  	finished, _ := notificationsCheck.isCurrentOperationFinished(pc)
   103  	assert.False(t, finished, "Operation should not be considered finished")
   104  }
   105  
   106  func TestFeedContainsEarlierNotification(t *testing.T) {
   107  	testUUID := uuid.NewString()
   108  	testTID1 := "tid_0123abcd"
   109  	testLastModified1 := "2016-10-28T13:59:00.000Z"
   110  	testTID2 := "tid_0123wxyz"
   111  	testLastModified2, _ := time.Parse(DateLayout, "2016-10-28T14:00:00.000Z")
   112  
   113  	n := feeds.Notification{ID: testUUID, PublishReference: testTID1, LastModified: testLastModified1}
   114  	notifications := []*feeds.Notification{&n}
   115  	f := mockFeed(feedName, testUUID, notifications)
   116  	subscribedFeeds := make(map[string][]feeds.Feed)
   117  	subscribedFeeds[testEnv] = []feeds.Feed{f}
   118  
   119  	notificationsCheck := &NotificationsCheck{
   120  		mockHTTPCaller(t, "", nil),
   121  		subscribedFeeds,
   122  		feedName,
   123  	}
   124  	log := logger.NewUPPLogger("test", "PANIC")
   125  
   126  	pc := NewPublishCheck(
   127  		newPublishMetricBuilder().withUUID(testUUID).withPlatform(testEnv).withTID(testTID2).withPublishDate(testLastModified2).build(),
   128  		"",
   129  		"",
   130  		0,
   131  		0,
   132  		nil,
   133  		nil,
   134  		log,
   135  	)
   136  	finished, ignore := notificationsCheck.isCurrentOperationFinished(pc)
   137  	assert.False(t, finished, "Operation should not be considered finished")
   138  	assert.False(t, ignore, "Operation should not be skipped")
   139  }
   140  
   141  func TestFeedContainsLaterNotification(t *testing.T) {
   142  	testUUID := uuid.NewString()
   143  	testTID1 := "tid_0123abcd"
   144  	testLastModified1 := "2016-10-28T14:00:00.000Z"
   145  	testTID2 := "tid_0123wxyz"
   146  	testLastModified2, _ := time.Parse(DateLayout, "2016-10-28T13:59:00.000Z")
   147  
   148  	n := feeds.Notification{ID: testUUID, PublishReference: testTID1, LastModified: testLastModified1}
   149  	notifications := []*feeds.Notification{&n}
   150  	f := mockFeed(feedName, testUUID, notifications)
   151  	subscribedFeeds := make(map[string][]feeds.Feed)
   152  	subscribedFeeds[testEnv] = []feeds.Feed{f}
   153  
   154  	notificationsCheck := &NotificationsCheck{
   155  		mockHTTPCaller(t, "", nil),
   156  		subscribedFeeds,
   157  		feedName,
   158  	}
   159  	log := logger.NewUPPLogger("test", "PANIC")
   160  
   161  	pc := NewPublishCheck(
   162  		newPublishMetricBuilder().withUUID(testUUID).withPlatform(testEnv).withTID(testTID2).withPublishDate(testLastModified2).build(),
   163  		"",
   164  		"",
   165  		0,
   166  		0,
   167  		nil,
   168  		nil,
   169  		log,
   170  	)
   171  	_, ignore := notificationsCheck.isCurrentOperationFinished(pc)
   172  	assert.True(t, ignore, "Operation should be skipped")
   173  }
   174  
   175  func TestFeedContainsUnparseableNotification(t *testing.T) {
   176  	testUUID := uuid.NewString()
   177  	testTID1 := "tid_0123abcd"
   178  	testLastModified1 := "foo-bar-baz"
   179  	testTID2 := "tid_0123wxyz"
   180  	testLastModified2, _ := time.Parse(DateLayout, "2016-10-28T13:59:00.000Z")
   181  
   182  	n := feeds.Notification{ID: testUUID, PublishReference: testTID1, LastModified: testLastModified1}
   183  	notifications := []*feeds.Notification{&n}
   184  	f := mockFeed(feedName, testUUID, notifications)
   185  	subscribedFeeds := make(map[string][]feeds.Feed)
   186  	subscribedFeeds[testEnv] = []feeds.Feed{f}
   187  
   188  	notificationsCheck := &NotificationsCheck{
   189  		mockHTTPCaller(t, "", nil),
   190  		subscribedFeeds,
   191  		feedName,
   192  	}
   193  	log := logger.NewUPPLogger("test", "PANIC")
   194  
   195  	pc := NewPublishCheck(
   196  		newPublishMetricBuilder().withUUID(testUUID).withPlatform(testEnv).withTID(testTID2).withPublishDate(testLastModified2).build(),
   197  		"",
   198  		"",
   199  		0,
   200  		0,
   201  		nil,
   202  		nil,
   203  		log,
   204  	)
   205  	finished, ignore := notificationsCheck.isCurrentOperationFinished(pc)
   206  	assert.False(t, finished, "Operation should not be considered finished")
   207  	assert.False(t, ignore, "Operation should not be skipped")
   208  }
   209  
   210  func TestMissingFeed(t *testing.T) {
   211  	testUUID := uuid.NewString()
   212  	testTID := "tid_0123wxyz"
   213  	testLastModified := "2016-10-28T14:00:00.000Z"
   214  
   215  	n := feeds.Notification{ID: testUUID, PublishReference: testTID, LastModified: testLastModified}
   216  	notifications := []*feeds.Notification{&n}
   217  	f := mockFeed("foo", testUUID, notifications)
   218  	subscribedFeeds := make(map[string][]feeds.Feed)
   219  	subscribedFeeds[testEnv] = []feeds.Feed{f}
   220  
   221  	notificationsCheck := &NotificationsCheck{
   222  		mockHTTPCaller(t, "", nil),
   223  		subscribedFeeds,
   224  		feedName,
   225  	}
   226  	log := logger.NewUPPLogger("test", "PANIC")
   227  
   228  	pc := NewPublishCheck(
   229  		newPublishMetricBuilder().withUUID(testUUID).withPlatform(testEnv).withTID(testTID).build(),
   230  		"",
   231  		"",
   232  		0,
   233  		0,
   234  		nil,
   235  		nil,
   236  		log,
   237  	)
   238  	finished, ignore := notificationsCheck.isCurrentOperationFinished(pc)
   239  	assert.False(t, finished, "Operation should not be considered finished")
   240  	assert.False(t, ignore, "Operation should not be ignored")
   241  }
   242  
   243  func TestMissingEnvironment(t *testing.T) {
   244  	testUUID := uuid.NewString()
   245  	testTID := "tid_0123wxyz"
   246  	testLastModified := "2016-10-28T14:00:00.000Z"
   247  
   248  	n := feeds.Notification{ID: testUUID, PublishReference: testTID, LastModified: testLastModified}
   249  	notifications := []*feeds.Notification{&n}
   250  	f := mockFeed(feedName, testUUID, notifications)
   251  	subscribedFeeds := make(map[string][]feeds.Feed)
   252  	subscribedFeeds["foo"] = []feeds.Feed{f}
   253  
   254  	notificationsCheck := &NotificationsCheck{
   255  		mockHTTPCaller(t, "", nil),
   256  		subscribedFeeds,
   257  		feedName,
   258  	}
   259  	log := logger.NewUPPLogger("test", "PANIC")
   260  
   261  	pc := NewPublishCheck(
   262  		newPublishMetricBuilder().withUUID(testUUID).withPlatform(testEnv).withTID(testTID).build(),
   263  		"",
   264  		"",
   265  		0,
   266  		0,
   267  		nil,
   268  		nil,
   269  		log,
   270  	)
   271  	finished, ignore := notificationsCheck.isCurrentOperationFinished(pc)
   272  	assert.False(t, finished, "Operation should not be considered finished")
   273  	assert.False(t, ignore, "Operation should not be ignored")
   274  }
   275  
   276  func TestShouldSkipCheck_ContentIsNotMarkedAsDeleted_CheckNotSkipped(t *testing.T) {
   277  	pm := newPublishMetricBuilder().withMarkedDeleted(false).build()
   278  	notificationsCheck := NotificationsCheck{}
   279  	log := logger.NewUPPLogger("test", "PANIC")
   280  
   281  	pc := NewPublishCheck(pm, "", "", 0, 0, nil, nil, log)
   282  
   283  	if notificationsCheck.shouldSkipCheck(pc) {
   284  		t.Errorf("Expected failure")
   285  	}
   286  }
   287  
   288  func TestShouldSkipCheck_ContentIsMarkedAsDeletedPreviousNotificationsExist_CheckNotSkipped(t *testing.T) {
   289  	pm := newPublishMetricBuilder().withMarkedDeleted(true).withEndpoint("http://notifications-endpoint:8080/content/notifications").build()
   290  	log := logger.NewUPPLogger("test", "PANIC")
   291  	pc := NewPublishCheck(pm, "", "", 0, 0, nil, nil, log)
   292  	response := buildResponse(200, `[{"id": "foobar", "lastModified" : "foobaz", "publishReference" : "unitTestRef" }]`)
   293  	defer response.Body.Close()
   294  	notificationsCheck := NotificationsCheck{
   295  		httpCaller: mockHTTPCaller(t, "", response),
   296  		feedName:   feedName,
   297  	}
   298  	if notificationsCheck.shouldSkipCheck(pc) {
   299  		t.Errorf("Expected failure")
   300  	}
   301  }
   302  
   303  func TestShouldSkipCheck_ContentIsMarkedAsDeletedPreviousNotificationsDoesNotExist_CheckSkipped(t *testing.T) {
   304  	pm := newPublishMetricBuilder().withMarkedDeleted(true).withEndpoint("http://notifications-endpoint:8080/content/notifications").build()
   305  	log := logger.NewUPPLogger("test", "PANIC")
   306  	pc := NewPublishCheck(pm, "", "", 0, 0, nil, nil, log)
   307  	response := buildResponse(200, `[]`)
   308  	defer response.Body.Close()
   309  	notificationsCheck := NotificationsCheck{
   310  		httpCaller: mockHTTPCaller(t, "", response),
   311  		feedName:   feedName,
   312  	}
   313  	if !notificationsCheck.shouldSkipCheck(pc) {
   314  		t.Errorf("Expected success")
   315  	}
   316  }