001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.camel;
017
018import static org.junit.Assert.assertEquals;
019import static org.junit.Assert.assertFalse;
020import static org.junit.Assert.assertNull;
021import static org.junit.Assert.assertTrue;
022import static org.mockito.Matchers.any;
023import static org.mockito.Matchers.anyString;
024import static org.mockito.Matchers.eq;
025import static org.mockito.Mockito.when;
026
027import java.io.InputStream;
028import java.net.URI;
029
030import org.junit.Test;
031import org.junit.runner.RunWith;
032import org.mockito.Mock;
033import org.mockito.runners.MockitoJUnitRunner;
034import org.springframework.transaction.CannotCreateTransactionException;
035import org.springframework.transaction.TransactionDefinition;
036import org.springframework.transaction.TransactionSystemException;
037import org.springframework.transaction.support.DefaultTransactionDefinition;
038import org.springframework.transaction.support.DefaultTransactionStatus;
039import org.springframework.transaction.support.TransactionTemplate;
040
041/**
042 * @author Aaron Coburn
043 */
044@RunWith(MockitoJUnitRunner.class)
045public class FcrepoTransactionManagerTest {
046
047    @Mock
048    private FcrepoClient mockClient;
049
050    @Test
051    public void testProperties() {
052        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
053        final String baseUrl = "http://localhost:8080/rest";
054        final String authUsername = "foo";
055        final String authPassword = "bar";
056        final String authHost = "baz";
057        final String transactionId = "1234567890";
058
059        assertNull(txMgr.getAuthUsername());
060        assertNull(txMgr.getAuthPassword());
061        assertNull(txMgr.getAuthHost());
062        assertNull(txMgr.getBaseUrl());
063
064        txMgr.setBaseUrl(baseUrl);
065        txMgr.setAuthUsername(authUsername);
066        txMgr.setAuthPassword(authPassword);
067        txMgr.setAuthHost(authHost);
068
069        assertEquals(baseUrl, txMgr.getBaseUrl());
070        assertEquals(authUsername, txMgr.getAuthUsername());
071        assertEquals(authPassword, txMgr.getAuthPassword());
072        assertEquals(authHost, txMgr.getAuthHost());
073    }
074
075    @Test
076    public void testTransactionCommit() throws FcrepoOperationFailedException {
077        final String baseUrl = "http://localhost:8080/rest";
078        final String tx = "tx:1234567890";
079        final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT);
080        final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
081        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
082        txMgr.setBaseUrl(baseUrl);
083        TestUtils.setField(txMgr, "client", mockClient);
084
085        final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
086        final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
087                TransactionDefinition.PROPAGATION_REQUIRED);
088
089        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
090        transactionTemplate.afterPropertiesSet();
091
092        when(mockClient.post(eq(beginUri), any(InputStream.class), anyString())).thenReturn(
093                new FcrepoResponse(beginUri, 201, null, URI.create(baseUrl + "/" + tx), null));
094        when(mockClient.post(eq(commitUri), any(InputStream.class), anyString())).thenReturn(
095                new FcrepoResponse(commitUri, 201, null, null, null));
096
097        DefaultTransactionStatus status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
098        FcrepoTransactionObject txObj = (FcrepoTransactionObject)status.getTransaction();
099
100        assertEquals(tx, txObj.getSessionId());
101        assertFalse(status.isCompleted());
102
103        status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
104
105        txMgr.commit(status);
106
107        txObj = (FcrepoTransactionObject)status.getTransaction();
108
109        assertNull(txObj.getSessionId());
110        assertTrue(status.isCompleted());
111    }
112
113    @Test
114    public void testTransactionRollback() throws FcrepoOperationFailedException {
115        final String baseUrl = "http://localhost:8080/rest";
116        final String tx = "tx:1234567890";
117        final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT);
118        final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
119        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
120        txMgr.setBaseUrl(baseUrl);
121        TestUtils.setField(txMgr, "client", mockClient);
122
123        final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
124        final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
125                TransactionDefinition.PROPAGATION_REQUIRED);
126
127        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
128        transactionTemplate.afterPropertiesSet();
129
130        when(mockClient.post(eq(beginUri), any(InputStream.class), anyString())).thenReturn(
131                new FcrepoResponse(beginUri, 201, null, URI.create(baseUrl + "/" + tx), null));
132        when(mockClient.post(eq(commitUri), any(InputStream.class), anyString())).thenReturn(
133                new FcrepoResponse(commitUri, 201, null, null, null));
134
135        DefaultTransactionStatus status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
136        FcrepoTransactionObject txObj = (FcrepoTransactionObject)status.getTransaction();
137
138        assertEquals(tx, txObj.getSessionId());
139        assertFalse(status.isCompleted());
140
141        status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
142
143        txMgr.rollback(status);
144
145        txObj = (FcrepoTransactionObject)status.getTransaction();
146
147        assertNull(txObj.getSessionId());
148        assertTrue(status.isCompleted());
149    }
150
151    @Test (expected = CannotCreateTransactionException.class)
152    public void testTransactionBeginError() throws FcrepoOperationFailedException {
153        final String baseUrl = "http://localhost:8080/rest";
154        final String tx = "tx:1234567890";
155        final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
156        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
157        txMgr.setBaseUrl(baseUrl);
158        TestUtils.setField(txMgr, "client", mockClient);
159
160        final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
161        final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
162                TransactionDefinition.PROPAGATION_REQUIRED);
163
164        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
165        transactionTemplate.afterPropertiesSet();
166
167        when(mockClient.post(eq(beginUri), any(InputStream.class), anyString())).thenThrow(
168                new FcrepoOperationFailedException(beginUri, 400, "Bad Request"));
169
170        txMgr.getTransaction(txDef);
171    }
172
173    @Test (expected = CannotCreateTransactionException.class)
174    public void testTransactionBeginNoLocationError() throws FcrepoOperationFailedException {
175        final String baseUrl = "http://localhost:8080/rest";
176        final String tx = "tx:1234567890";
177        final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
178        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
179        txMgr.setBaseUrl(baseUrl);
180        TestUtils.setField(txMgr, "client", mockClient);
181
182        final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
183        final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
184                TransactionDefinition.PROPAGATION_REQUIRED);
185
186        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
187        transactionTemplate.afterPropertiesSet();
188
189        when(mockClient.post(eq(beginUri), any(InputStream.class), anyString())).thenReturn(
190                new FcrepoResponse(beginUri, 201, null, null, null));
191
192        txMgr.getTransaction(txDef);
193    }
194
195    @Test (expected = CannotCreateTransactionException.class)
196    public void testTransactionNullResponseError() throws FcrepoOperationFailedException {
197        final String baseUrl = "http://localhost:8080/rest";
198        final String tx = "tx:1234567890";
199        final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
200        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
201        txMgr.setBaseUrl(baseUrl);
202        TestUtils.setField(txMgr, "client", mockClient);
203
204        final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
205        final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
206                TransactionDefinition.PROPAGATION_REQUIRED);
207
208        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
209        transactionTemplate.afterPropertiesSet();
210
211        when(mockClient.post(eq(beginUri), any(InputStream.class), anyString())).thenReturn(null);
212
213        txMgr.getTransaction(txDef);
214    }
215
216    @Test (expected = TransactionSystemException.class)
217    public void testTransactionCommitError() throws FcrepoOperationFailedException {
218        final String baseUrl = "http://localhost:8080/rest";
219        final String tx = "tx:1234567890";
220        final URI commitUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.COMMIT);
221        final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
222        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
223        txMgr.setBaseUrl(baseUrl);
224        TestUtils.setField(txMgr, "client", mockClient);
225
226        final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
227        final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
228                TransactionDefinition.PROPAGATION_REQUIRED);
229
230        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
231        transactionTemplate.afterPropertiesSet();
232
233        when(mockClient.post(eq(beginUri), any(InputStream.class), anyString())).thenReturn(
234                new FcrepoResponse(beginUri, 201, null, URI.create(baseUrl + "/" + tx), null));
235        when(mockClient.post(eq(commitUri), any(InputStream.class), anyString())).thenThrow(
236                new FcrepoOperationFailedException(commitUri, 400, "Bad Request"));
237
238        DefaultTransactionStatus status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
239
240        final FcrepoTransactionObject txObj = (FcrepoTransactionObject)status.getTransaction();
241        assertEquals(tx, txObj.getSessionId());
242        assertFalse(status.isCompleted());
243
244        status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
245        txMgr.commit(status);
246    }
247
248    @Test (expected = TransactionSystemException.class)
249    public void testTransactionRollbackError() throws FcrepoOperationFailedException {
250        final String baseUrl = "http://localhost:8080/rest";
251        final String tx = "tx:1234567890";
252        final URI rollbackUri = URI.create(baseUrl + "/" + tx + FcrepoConstants.ROLLBACK);
253        final URI beginUri = URI.create(baseUrl + FcrepoConstants.TRANSACTION);
254        final FcrepoTransactionManager txMgr = new FcrepoTransactionManager();
255        txMgr.setBaseUrl(baseUrl);
256        TestUtils.setField(txMgr, "client", mockClient);
257
258        final TransactionTemplate transactionTemplate = new TransactionTemplate(txMgr);
259        final DefaultTransactionDefinition txDef = new DefaultTransactionDefinition(
260                TransactionDefinition.PROPAGATION_REQUIRED);
261
262        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
263        transactionTemplate.afterPropertiesSet();
264
265        when(mockClient.post(eq(beginUri), any(InputStream.class), anyString())).thenReturn(
266                new FcrepoResponse(beginUri, 201, null, URI.create(baseUrl + "/" + tx), null));
267        when(mockClient.post(eq(rollbackUri), any(InputStream.class), anyString())).thenThrow(
268                new FcrepoOperationFailedException(rollbackUri, 400, "Bad Request"));
269
270        DefaultTransactionStatus status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
271
272        final FcrepoTransactionObject txObj = (FcrepoTransactionObject)status.getTransaction();
273        assertEquals(tx, txObj.getSessionId());
274        assertFalse(status.isCompleted());
275
276        status = (DefaultTransactionStatus)txMgr.getTransaction(txDef);
277        txMgr.rollback(status);
278    }
279}