001/* 002 * The contents of this file are subject to the license and copyright 003 * detailed in the LICENSE and NOTICE files at the root of the source 004 * tree. 005 */ 006 007package org.fcrepo.kernel.impl.observer; 008 009import org.fcrepo.config.ServerManagedPropsMode; 010import org.fcrepo.kernel.api.Transaction; 011import org.fcrepo.kernel.api.identifiers.FedoraId; 012import org.fcrepo.kernel.api.observer.Event; 013import org.fcrepo.kernel.api.observer.EventType; 014import org.fcrepo.kernel.impl.operations.DeleteResourceOperationFactoryImpl; 015import org.fcrepo.kernel.impl.operations.NonRdfSourceOperationFactoryImpl; 016import org.fcrepo.kernel.impl.operations.RdfSourceOperationFactoryImpl; 017import org.fcrepo.kernel.impl.operations.VersionResourceOperationFactoryImpl; 018 019import org.junit.Before; 020import org.junit.Test; 021import org.junit.runner.RunWith; 022import org.mockito.Mock; 023import org.mockito.junit.MockitoJUnitRunner; 024 025import java.io.ByteArrayInputStream; 026import java.util.Set; 027 028import static org.fcrepo.kernel.api.RdfLexicon.RDF_SOURCE; 029import static org.hamcrest.MatcherAssert.assertThat; 030import static org.hamcrest.Matchers.contains; 031import static org.hamcrest.Matchers.containsInAnyOrder; 032import static org.junit.Assert.assertEquals; 033import static org.junit.Assert.assertNotNull; 034import static org.mockito.Mockito.when; 035 036/** 037 * @author pwinckles 038 */ 039@RunWith(MockitoJUnitRunner.Silent.class) 040public class ResourceOperationEventBuilderTest { 041 042 private static final FedoraId FEDORA_ID = FedoraId.create("/test"); 043 private static final String USER = "user1"; 044 private static final String BASE_URL = "http://locahost/rest"; 045 046 @Mock 047 private Transaction transaction; 048 049 @Before 050 public void setup() { 051 when(transaction.getId()).thenReturn("tx-123"); 052 } 053 054 @Test 055 public void buildCreateEventFromCreateRdfOperation() { 056 final var operation = new RdfSourceOperationFactoryImpl() 057 .createBuilder(transaction, FEDORA_ID, RDF_SOURCE.toString(), ServerManagedPropsMode.RELAXED) 058 .userPrincipal(USER) 059 .build(); 060 061 final var event = ResourceOperationEventBuilder.fromResourceOperation(FEDORA_ID, operation, null) 062 .withBaseUrl(BASE_URL) 063 .build(); 064 065 assertDefaultEvent(event, EventType.RESOURCE_CREATION); 066 } 067 068 @Test 069 public void buildCreateEventFromCreateNonRdfOperation() { 070 final var fedoraId = FedoraId.create("/test/ab/c"); 071 final var user = "user2"; 072 final var operation = new NonRdfSourceOperationFactoryImpl() 073 .createInternalBinaryBuilder(transaction, fedoraId, new ByteArrayInputStream(new byte[]{})) 074 .userPrincipal(user) 075 .build(); 076 077 final var event = ResourceOperationEventBuilder.fromResourceOperation(fedoraId, operation, null) 078 .withBaseUrl(BASE_URL) 079 .build(); 080 081 assertEquals(fedoraId, event.getFedoraId()); 082 assertEquals(fedoraId.getFullIdPath(), event.getPath()); 083 assertEquals(user, event.getUserID()); 084 assertThat(event.getTypes(), contains(EventType.RESOURCE_CREATION)); 085 assertNotNull(event.getEventID()); 086 assertNotNull(event.getDate()); 087 } 088 089 @Test 090 public void buildCreateEventFromVersionOperation() { 091 final var operation = new VersionResourceOperationFactoryImpl().createBuilder(transaction, FEDORA_ID) 092 .userPrincipal(USER) 093 .build(); 094 095 final var event = ResourceOperationEventBuilder.fromResourceOperation(FEDORA_ID, operation, null) 096 .withBaseUrl(BASE_URL) 097 .build(); 098 099 assertDefaultEvent(event, EventType.RESOURCE_MODIFICATION); 100 } 101 102 @Test 103 public void buildDeleteEventFromDeleteOperation() { 104 final var operation = new DeleteResourceOperationFactoryImpl().deleteBuilder(transaction, FEDORA_ID) 105 .userPrincipal(USER) 106 .build(); 107 108 final var event = ResourceOperationEventBuilder.fromResourceOperation(FEDORA_ID, operation, null) 109 .withBaseUrl(BASE_URL) 110 .build(); 111 112 assertDefaultEvent(event, EventType.RESOURCE_DELETION); 113 } 114 115 @Test 116 public void buildUpdateEventFromUpdateRdfOperation() { 117 final var operation = new RdfSourceOperationFactoryImpl() 118 .updateBuilder(transaction, FEDORA_ID, ServerManagedPropsMode.RELAXED) 119 .userPrincipal(USER) 120 .build(); 121 122 final var event = ResourceOperationEventBuilder.fromResourceOperation(FEDORA_ID, operation, null) 123 .withBaseUrl(BASE_URL) 124 .build(); 125 126 assertDefaultEvent(event, EventType.RESOURCE_MODIFICATION); 127 } 128 129 @Test 130 public void buildUpdateEventFromUpdateNonRdfOperation() { 131 final var operation = new NonRdfSourceOperationFactoryImpl() 132 .updateInternalBinaryBuilder(transaction, FEDORA_ID, new ByteArrayInputStream(new byte[]{})) 133 .userPrincipal(USER) 134 .build(); 135 136 final var event = ResourceOperationEventBuilder.fromResourceOperation(FEDORA_ID, operation, null) 137 .withBaseUrl(BASE_URL) 138 .build(); 139 140 assertDefaultEvent(event, EventType.RESOURCE_MODIFICATION); 141 } 142 143 @Test 144 public void mergeValidObjects() { 145 final var createOperation = new RdfSourceOperationFactoryImpl() 146 .createBuilder(transaction, FEDORA_ID, RDF_SOURCE.toString(), ServerManagedPropsMode.RELAXED) 147 .userPrincipal(USER) 148 .build(); 149 150 final var createEventBuilder = ResourceOperationEventBuilder 151 .fromResourceOperation(FEDORA_ID, createOperation, null) 152 .withBaseUrl(BASE_URL); 153 154 final var updateOperation = new NonRdfSourceOperationFactoryImpl() 155 .updateInternalBinaryBuilder(transaction, FEDORA_ID, new ByteArrayInputStream(new byte[]{})) 156 .userPrincipal(USER) 157 .build(); 158 159 final var updateEventBuilder = ResourceOperationEventBuilder 160 .fromResourceOperation(FEDORA_ID, updateOperation, null) 161 .withBaseUrl(BASE_URL); 162 final var updateEvent = updateEventBuilder.build(); 163 164 final var merged = createEventBuilder.merge(updateEventBuilder).build(); 165 166 assertEquals(FEDORA_ID, merged.getFedoraId()); 167 assertEquals(FEDORA_ID.getFullIdPath(), merged.getPath()); 168 assertEquals(USER, merged.getUserID()); 169 assertThat(merged.getTypes(), containsInAnyOrder(EventType.RESOURCE_CREATION, EventType.RESOURCE_MODIFICATION)); 170 assertEquals(updateEvent.getDate(), merged.getDate()); 171 } 172 173 @Test 174 public void populateOtherEventFields() { 175 final var operation = new NonRdfSourceOperationFactoryImpl() 176 .updateInternalBinaryBuilder(transaction, FEDORA_ID, new ByteArrayInputStream(new byte[]{})) 177 .userPrincipal(USER) 178 .build(); 179 180 final var baseUrl = "http://localhost/rest"; 181 final var userAgent = "user-agent"; 182 final var resourceTypes = Set.of("resource-type"); 183 184 final var event = ResourceOperationEventBuilder.fromResourceOperation(FEDORA_ID, operation, null) 185 .withBaseUrl(baseUrl) 186 .withUserAgent(userAgent) 187 .withResourceTypes(resourceTypes) 188 .build(); 189 190 assertEquals(baseUrl, event.getBaseUrl()); 191 assertEquals(userAgent, event.getUserAgent()); 192 assertEquals(resourceTypes, event.getResourceTypes()); 193 } 194 195 private void assertDefaultEvent(final Event event, final EventType type) { 196 assertEquals(FEDORA_ID, event.getFedoraId()); 197 assertEquals(FEDORA_ID.getFullIdPath(), event.getPath()); 198 assertEquals(USER, event.getUserID()); 199 assertThat(event.getTypes(), contains(type)); 200 assertNotNull(event.getEventID()); 201 assertNotNull(event.getDate()); 202 } 203 204}