/** 
 *  Copyright (c) 2015 The original author or authors
 *
 *  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.
 */

package org.reveno.atp.acceptance.tests;

import java.io.File;
import java.io.RandomAccessFile;

import org.junit.Assert;
import org.junit.Test;
import org.reveno.atp.acceptance.api.events.AccountCreatedEvent;
import org.reveno.atp.acceptance.api.events.OrderCreatedEvent;
import org.reveno.atp.api.Reveno;

public class ExceptionalCasesTests extends RevenoBaseTest {

	@Test
	public void testEventsCommitsFileCorrupted() throws Exception {
		Reveno reveno = createEngine();
		reveno.startup();
		
		generateAndSendCommands(reveno, 5_000);
	
		reveno.shutdown();
		
		eraseRandomBuffer(findFirstFile("evn"));
		
		reveno = createEngine();
		Waiter accountCreatedEvent = listenFor(reveno, AccountCreatedEvent.class, Integer.MAX_VALUE);
		Waiter orderCreatedEvent = listenFor(reveno, OrderCreatedEvent.class, Integer.MAX_VALUE);
		reveno.startup();
		
		Assert.assertFalse(accountCreatedEvent.isArrived());
		Assert.assertFalse(orderCreatedEvent.isArrived());
		Assert.assertTrue(accountCreatedEvent.getCount() < Integer.MAX_VALUE);
		Assert.assertTrue(orderCreatedEvent.getCount() < Integer.MAX_VALUE);
		
		reveno.shutdown();
		
		reveno = createEngine();
		accountCreatedEvent = listenFor(reveno, AccountCreatedEvent.class, 1);
		orderCreatedEvent = listenFor(reveno, OrderCreatedEvent.class, 1);
		reveno.startup();
		
		Assert.assertFalse(accountCreatedEvent.isArrived());
		Assert.assertFalse(orderCreatedEvent.isArrived());
		Assert.assertEquals(1, accountCreatedEvent.getCount());
		Assert.assertEquals(1, orderCreatedEvent.getCount());
		
		reveno.shutdown();
	}
	
	
	protected void eraseRandomBuffer(File file) throws Exception {
		try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
			raf.setLength((long)((raf.length() * (Math.random() * Math.random()))));
		}
	}
	
}

@@ -886,32 +886,134 @@
 erCreatedEvent;%0A
+import org.reveno.atp.acceptance.views.AccountView;%0Aimport org.reveno.atp.acceptance.views.OrderView;%0A
 import org.reven
@@ -1263,16 +1263,175 @@
 5_000);%0A
+%09%09%0A%09%09Assert.assertEquals(5_000, reveno.query().select(AccountView.class).size());%0A%09%09Assert.assertEquals(5_000, reveno.query().select(OrderView.class).size());%0A
 %09%0A%09%09reve
@@ -1525,104 +1525,8 @@
 ();%0A
-%09%09Waiter accountCreatedEvent = listenFor(reveno, AccountCreatedEvent.class, Integer.MAX_VALUE);%0A
 %09%09Wa
@@ -1655,101 +1655,152 @@
 sert
-False(accountCreatedEvent.isArrived());%0A%09%09Assert.assertFalse(orderCreatedEvent.isArrived
+Equals(5_000, reveno.query().select(AccountView.class).size());%0A%09%09Assert.assertEquals(5_000, reveno.query().select(OrderView.class).size
 ());%0A
+%09%09%0A
 %09%09As
@@ -1814,20 +1814,19 @@
 sert
-True(account
+False(order
 Crea
@@ -1838,38 +1838,19 @@
 ent.
-getCount() %3C Integer.MAX_VALUE
+isArrived()
 );%0A%09
@@ -1975,16 +1975,23 @@
 ne();%0A%09%09
+Waiter 
 accountC
@@ -2137,32 +2137,191 @@
 o.startup();%0A%09%09%0A
+%09%09Assert.assertEquals(5_000, reveno.query().select(AccountView.class).size());%0A%09%09Assert.assertEquals(5_000, reveno.query().select(OrderView.class).size());%0A%09%09%0A
 %09%09Assert.assertF
@@ -2555,16 +2555,462 @@
 );%0A%09%7D%0A%09%0A
+%09@Test%0A%09public void testTransactionCommitsFileCorrupted() throws Exception %7B%0A%09%09Reveno reveno = createEngine();%0A%09%09reveno.startup();%0A%09%09%0A%09%09generateAndSendCommands(reveno, 5_000);%0A%09%09%0A%09%09reveno.shutdown();%0A%09%09%0A%09%09eraseRandomBuffer(findFirstFile(%22tx%22));%0A%09%09eraseRandomBuffer(findFirstFile(%22evn%22));%0A%09%09%0A%09%09reveno = createEngine();%0A%09%09reveno.startup();%0A%09%09%0A%09%09Assert.assertTrue(5_000 %3E reveno.query().select(OrderView.class).size());%0A%09%09%0A%09%09reveno.shutdown();%0A%09%7D%0A%09%0A
 %09%0A%09prote

test fixes.