Commit 92150849 authored by Noel Alonso's avatar Noel Alonso
Browse files

Añade evento previo a rollback + test

El evento rollback será específico de cada tipo, ya que debe contener la
última versión correcta del elemento
parent 8bcd8616
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ public abstract class EventTypes {
		DELETE_FAILED = "DELETE_FAILED",
		DELETE_CANCELLED = "DELETE_CANCELLED",
		//FAIL
		PREPARE_ROLLBACK = "PREPARE_ROLLBACK",
		ROLLBACK = "ROLLBACK";
		//@formatter:on

+8 −18
Original line number Diff line number Diff line
@@ -20,31 +20,21 @@ package es.redmic.brokerlib.avro.fail;
 * #L%
 */

import org.apache.avro.Schema;

import es.redmic.brokerlib.avro.common.EventTypes;
import es.redmic.brokerlib.avro.common.SimpleEvent;

public class RollbackEvent extends SimpleEvent {

	// @formatter:off

	public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{"
		+ "\"type\":\"record\",\"name\":\"RollbackEvent\","
				+ "\"namespace\":\"es.redmic.brokerlib.avro.fail\",\"fields\":["
			+ getEventBaseSchema() + "]}");
public abstract class BaseRollbackEvent extends SimpleEvent {

	// @formatter:on
	private String failEventType;

	static String type = EventTypes.ROLLBACK;

	public RollbackEvent() {
	public BaseRollbackEvent(String type) {
		super(type);
	}

	@Override
	public Schema getSchema() {
		return SCHEMA$;
	public String getFailEventType() {
		return failEventType;
	}

	public void setFailEventType(String failEventType) {
		this.failEventType = failEventType;
	}
}
+107 −0
Original line number Diff line number Diff line
package es.redmic.brokerlib.avro.fail;

/*-
 * #%L
 * broker-lib
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import org.apache.avro.Schema;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import es.redmic.brokerlib.avro.common.EventTypes;

public class PrepareRollbackEvent extends BaseRollbackEvent {

	// @formatter:off

	public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{"
		+ "\"type\":\"record\",\"name\":\"PrepareRollbackEvent\","
				+ "\"namespace\":\"es.redmic.brokerlib.avro.fail\",\"fields\":["
				+ "{\"name\":\"failEventType\",\"type\": \"string\"},"
			+ getEventBaseSchema() + "]}");
	
	// @formatter:on

	static String type = EventTypes.PREPARE_ROLLBACK;

	public PrepareRollbackEvent() {
		super(type);
	}

	@Override
	public Object get(int field$) {
		switch (field$) {
		case 0:
			return getFailEventType();
		case 1:
			return getAggregateId();
		case 2:
			return getVersion();
		case 3:
			return getType();
		case 4:
			return getDate().getMillis();
		case 5:
			return getSessionId();
		case 6:
			return getUserId();
		case 7:
			return getId();
		default:
			throw new org.apache.avro.AvroRuntimeException("Bad index");
		}
	}

	@Override
	public void put(int field$, Object value$) {
		switch (field$) {
		case 0:
			setFailEventType(value$.toString());
			break;
		case 1:
			setAggregateId(value$.toString());
			break;
		case 2:
			setVersion((java.lang.Integer) value$);
			break;
		case 3:
			setType(value$.toString());
			break;
		case 4:
			setDate(new DateTime(value$, DateTimeZone.UTC));
			break;
		case 5:
			setSessionId(value$.toString());
			break;
		case 6:
			setUserId(value$.toString());
			break;
		case 7:
			setId(value$.toString());
			break;
		default:
			throw new org.apache.avro.AvroRuntimeException("Bad index");
		}
	}

	@Override
	public Schema getSchema() {
		return SCHEMA$;
	}
}
+52 −0
Original line number Diff line number Diff line
package es.redmic.brokerlib.avro;

/*-
 * #%L
 * broker-lib
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.UUID;

import org.junit.Test;

import es.redmic.brokerlib.avro.fail.PrepareRollbackEvent;
import es.redmic.brokerlib.utils.AvroBaseTest;

public class FailEventsCheckAvroSchemaTest extends AvroBaseTest {

	@Test
	public void PrepareRollbackEventSerializeAndDeserialize_IsSuccessful_IfSchemaAndDataAreCorrect() {

		PrepareRollbackEvent event = new PrepareRollbackEvent();
		event.setAggregateId("aggregateId");
		event.setFailEventType("failEventType");
		event.setSessionId(UUID.randomUUID().toString());
		event.setUserId("userId");
		event.setVersion(1);

		Object result = serializerAndDeserializer(event);

		assertTrue("El objeto obtenido debe ser una instancia de PrepareRollbackEvent",
				PrepareRollbackEvent.class.isInstance(result));

		assertEquals(result, event);
	}
}