FRANotificationTest.m revision 415243fbc81341293a852ff6aa14e9608d08685c
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott/*
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * The contents of this file are subject to the terms of the Common Development and
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * Distribution License (the License). You may not use this file except in compliance with the
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * License.
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott *
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * specific language governing permission and limitations under the License.
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott *
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * When distributing Covered Software, include this CDDL Header Notice in each file and include
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * Header, with the fields enclosed by brackets [] replaced by your own identifying
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * information: "Portions copyright [year] [name of copyright owner]".
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott *
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott * Copyright 2016 ForgeRock AS.
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott */
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott#import <Foundation/Foundation.h>
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import <OCMock/OCMock.h>
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott#import <XCTest/XCTest.h>
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAIdentityDatabase.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAIdentityDatabaseSQLiteOperations.h"
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott#import "FRANotification.h"
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott@interface FRANotificationTest : XCTestCase
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott@end
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott@implementation FRANotificationTest {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell FRAIdentityDatabaseSQLiteOperations *mockSqlOperations;
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell FRAIdentityDatabase *database;
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott FRANotification* notification;
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell id databaseObserverMock;
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott- (void)setUp {
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott [super setUp];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell mockSqlOperations = OCMClassMock([FRAIdentityDatabaseSQLiteOperations class]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell database = [[FRAIdentityDatabase alloc] initWithSqlOperations:mockSqlOperations];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell notification = [[FRANotification alloc] initWithDatabase:database];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell databaseObserverMock = OCMObserverMock();
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott- (void)tearDown {
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott [super tearDown];
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott- (void)testInitialStateOfNotification {
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Given
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // When
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Then
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott XCTAssertEqual([notification isPending], YES);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott XCTAssertEqual([notification isApproved], NO);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott- (void)testShouldApproveNotification {
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Given
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // When
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott [notification approve];
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Then
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott XCTAssertEqual([notification isPending], NO);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott XCTAssertEqual([notification isApproved], YES);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott- (void)testShouldDenyNotification {
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Given
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // When
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott [notification deny];
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Then
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott XCTAssertEqual([notification isPending], NO);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott XCTAssertEqual([notification isApproved], NO);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testSavedNotificationAutomaticallySavesItselfToDatabaseWhenApproved {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Given
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [database insertNotification:notification];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // When
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [notification approve];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Then
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell OCMVerify([mockSqlOperations updateNotification:notification]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testSavedNotificationAutomaticallySavesItselfToDatabaseWhenDenied {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Given
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [database insertNotification:notification];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // When
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [notification deny];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Then
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell OCMVerify([mockSqlOperations updateNotification:notification]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testBroadcastsOneChangeNotificationWhenNotificationUpdateIsAutomaticallySavedToDatabase {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Given
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [database insertNotification:notification];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [[NSNotificationCenter defaultCenter] addMockObserver:databaseObserverMock name:FRAIdentityDatabaseChangedNotification object:database];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [[databaseObserverMock expect] notificationWithName:FRAIdentityDatabaseChangedNotification object:database userInfo:[OCMArg any]];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // When
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [notification approve];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Then
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell OCMVerifyAll(databaseObserverMock);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott@end
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott