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.client.impl; 017 018import static com.hp.hpl.jena.graph.Factory.createDefaultGraph; 019import static com.hp.hpl.jena.graph.NodeFactory.createURI; 020import static com.hp.hpl.jena.graph.Triple.create; 021import static org.mockito.Matchers.any; 022import static org.mockito.Matchers.anyString; 023import static org.mockito.MockitoAnnotations.initMocks; 024import static org.mockito.Mockito.mock; 025import static org.mockito.Mockito.verify; 026import static org.mockito.Mockito.when; 027 028import java.io.ByteArrayInputStream; 029import java.io.IOException; 030import java.io.InputStream; 031import java.text.SimpleDateFormat; 032import java.util.Map; 033 034import org.apache.http.HttpResponse; 035import org.apache.http.StatusLine; 036import org.apache.http.client.methods.HttpPatch; 037import org.apache.http.client.methods.HttpPost; 038import org.apache.http.client.methods.HttpPut; 039 040import org.fcrepo.client.FedoraException; 041import org.fcrepo.client.FedoraRepository; 042import org.fcrepo.client.utils.HttpHelper; 043import org.fcrepo.kernel.api.RdfLexicon; 044import org.junit.Before; 045import org.junit.Test; 046import org.mockito.Mock; 047import com.hp.hpl.jena.graph.Graph; 048import com.hp.hpl.jena.rdf.model.ResourceFactory; 049 050import static org.junit.Assert.assertTrue; 051import static org.junit.Assert.assertEquals; 052import static org.junit.Assert.assertNotNull; 053 054/** 055 * 056 * @author lsitu 057 * 058 */ 059public class FedoraResourceImplTest { 060 061 @Mock 062 FedoraRepository mockRepository; 063 064 @Mock 065 HttpHelper mockHelper; 066 067 private FedoraResourceImpl resource; 068 069 private String path = "/test"; 070 071 private boolean isWritable = true; 072 073 private static SimpleDateFormat dateFormat = 074 new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 075 076 private String testDateValue = "2014-08-14T15:11:30.118Z"; 077 private String testMixinType = RdfLexicon.REPOSITORY_NAMESPACE + "test"; 078 private final String repositoryURL = "http://localhost:8080/rest"; 079 080 @Before 081 public void setUp() throws IOException { 082 initMocks(this); 083 when(mockRepository.getRepositoryUrl()).thenReturn(repositoryURL); 084 resource = new FedoraResourceImpl(mockRepository, mockHelper, path); 085 assertTrue(resource != null); 086 087 final Graph graph = createDefaultGraph(); 088 graph.add( create(createURI(repositoryURL + "/test"), RdfLexicon.CREATED_DATE.asNode(), 089 ResourceFactory.createPlainLiteral(testDateValue).asNode()) ); 090 graph.add( create(createURI(repositoryURL + "/test"), RdfLexicon.LAST_MODIFIED_DATE.asNode(), 091 ResourceFactory.createPlainLiteral(testDateValue).asNode()) ); 092 graph.add( create(createURI(repositoryURL + "/test"), RdfLexicon.HAS_MIXIN_TYPE.asNode(), 093 createURI(testMixinType)) ); 094 graph.add( create(createURI(repositoryURL + "/test"), RdfLexicon.WRITABLE.asNode(), 095 ResourceFactory.createTypedLiteral(new Boolean(isWritable)).asNode()) ); 096 resource.setGraph( graph ); 097 } 098 099 @Test 100 public void testGetCreatedDate() throws FedoraException { 101 assertEquals("Created date is not the same", 102 testDateValue, dateFormat.format(resource.getCreatedDate())); 103 } 104 105 @Test 106 public void testGetAndSetEtagValue() throws FedoraException { 107 ((FedoraResourceImpl)resource).setEtagValue("2a0e84efa8a39de57ebbc5ed3bc7e454a1a768de"); 108 assertEquals ("ETag is not the same", 109 "2a0e84efa8a39de57ebbc5ed3bc7e454a1a768de", resource.getEtagValue()); 110 } 111 112 @Test 113 public void testGetLastModifiedDate() throws FedoraException { 114 assertEquals("LastModifiedDate is not the same", 115 testDateValue, dateFormat.format(resource.getLastModifiedDate())); 116 } 117 118 @Test 119 public void testGetMixins() throws FedoraException { 120 assertTrue (resource.getMixins().contains(testMixinType)); 121 } 122 123 @Test 124 public void testGetName() throws IOException, FedoraException { 125 assertEquals ("Name is not the same", "test", resource.getName()); 126 } 127 128 @Test 129 public void testGetPath() throws FedoraException { 130 assertEquals ("Path is not the same", path, resource.getPath()); 131 } 132 133 @Test 134 public void testGetProperties() throws FedoraException { 135 assertEquals ("Can't retrieve Properties", 136 resource.getProperties().hasNext(), true); 137 } 138 139 @Test 140 public void testGetSize() throws FedoraException { 141 assertTrue (resource.getSize() == 4); 142 } 143 144 @Test 145 public void testIsWritable() { 146 assertEquals ("IsWriatable value is not the same", 147 isWritable, resource.isWritable()); 148 } 149 150 @Test 151 public void TestGetGraph () { 152 assertNotNull(((FedoraResourceImpl)resource).getGraph()); 153 } 154 155 @Test 156 public void testUpdatePropertiesSPARQL() throws Exception { 157 final HttpResponse mockResponse = mock(HttpResponse.class); 158 final StatusLine mockStatus = mock(StatusLine.class); 159 final HttpPatch patch = new HttpPatch(repositoryURL); 160 when(mockHelper.execute(any(HttpPatch.class))).thenReturn(mockResponse); 161 when(mockResponse.getStatusLine()).thenReturn(mockStatus); 162 when(mockStatus.getStatusCode()).thenReturn(204); 163 when(mockHelper.createPatchMethod(anyString(), anyString())).thenReturn(patch); 164 165 resource.updateProperties("test sparql update"); 166 verify(mockHelper).execute(patch); 167 verify(mockHelper).loadProperties(resource); 168 } 169 170 @Test 171 public void testUpdatePropertiesRDF() throws Exception { 172 final HttpResponse mockResponse = mock(HttpResponse.class); 173 final StatusLine mockStatus = mock(StatusLine.class); 174 final HttpPut put = new HttpPut(repositoryURL); 175 when(mockHelper.execute(any(HttpPatch.class))).thenReturn(mockResponse); 176 when(mockResponse.getStatusLine()).thenReturn(mockStatus); 177 when(mockStatus.getStatusCode()).thenReturn(204); 178 when(mockHelper.createTriplesPutMethod(anyString(), any(InputStream.class), anyString())).thenReturn(put); 179 180 final InputStream in = new ByteArrayInputStream("dummy rdf content".getBytes()); 181 resource.updateProperties(in, "text/n3"); 182 verify(mockHelper).execute(put); 183 verify(mockHelper).loadProperties(resource); 184 } 185 186 @Test 187 public void testCreateVersionSnapshot() throws Exception { 188 final HttpResponse mockResponse = mock(HttpResponse.class); 189 final StatusLine mockStatus = mock(StatusLine.class); 190 final HttpPost post = new HttpPost(repositoryURL); 191 when(mockHelper.execute(any(HttpPost.class))).thenReturn(mockResponse); 192 when(mockResponse.getStatusLine()).thenReturn(mockStatus); 193 when(mockStatus.getStatusCode()).thenReturn(204); 194 when(mockHelper.createPostMethod(anyString(), any(Map.class))).thenReturn(post); 195 final String label = "examplelabel"; 196 resource.createVersionSnapshot(label); 197 verify(mockHelper).execute(post); 198 assertEquals(label, post.getFirstHeader("Slug").getValue()); 199 } 200}