Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* @author Oliver Gierke
* @author Greg Turnquist
* @author Jeremy Rickard
* @author Jeroen Reijn
*/
@RepositoryRestController
class RepositoryEntityController extends AbstractRepositoryRestController implements ApplicationEventPublisherAware {
Expand Down Expand Up @@ -103,7 +104,6 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem
* @param config must not be {@literal null}.
* @param entityLinks must not be {@literal null}.
* @param assembler must not be {@literal null}.
* @param auditableBeanWrapperFactory must not be {@literal null}.
*/
@Autowired
public RepositoryEntityController(Repositories repositories, RepositoryRestConfiguration config,
Expand Down Expand Up @@ -337,7 +337,7 @@ public ResponseEntity<Resource<?>> getItemResource(RootResourceInformation resou
return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(//
() -> assembler.toFullResource(it));

}).orElseGet(() -> new ResponseEntity<Resource<?>>(HttpStatus.NOT_FOUND));
}).orElseThrow(() -> new ResourceNotFoundException());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017 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.springframework.data.rest.webmvc;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.Collections;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingContext;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.repository.support.RepositoryInvoker;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.PersistentEntitiesResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.data.web.PagedResourcesAssembler;

/**
* Unit tests for {@link RepositoryEntityController}
*
* @author Jeroen Reijn
*/
@RunWith(MockitoJUnitRunner.class)
public class RepositoryEntityControllerTest {

@Mock Repositories repositories;
@Mock RepositoryRestConfiguration restConfiguration;
@Mock RepositoryEntityLinks repositoryEntityLinks;
@Mock HttpHeadersPreparer httpHeadersPreparer;
@Mock RepositoryInvoker invoker;
@Mock PagedResourcesAssembler<Object> assembler;

KeyValueMappingContext<?, ?> mappingContext = new KeyValueMappingContext<>();

@Test(expected = ResourceNotFoundException.class) // DATAREST-1143
public void testUnknownItemThrowsResourceNotFound() throws Exception {

KeyValuePersistentEntity<?, ?> entity = mappingContext
.getRequiredPersistentEntity(RepositoryPropertyReferenceControllerUnitTests.Sample.class);

ResourceMappings mappings = new PersistentEntitiesResourceMappings(
new PersistentEntities(Collections.singleton(mappingContext)));

ResourceMetadata metadata = spy(
mappings.getMetadataFor(RepositoryPropertyReferenceControllerUnitTests.Sample.class));

when(metadata.getSupportedHttpMethods())
.thenReturn(RepositoryPropertyReferenceControllerUnitTests.AllSupportedHttpMethods.INSTANCE);

RootResourceInformation information = new RootResourceInformation(metadata, entity, invoker);
RepositoryEntityController repositoryEntityController = new RepositoryEntityController(repositories,
restConfiguration, repositoryEntityLinks, assembler, httpHeadersPreparer);
repositoryEntityController.getItemResource(information, "1", null, null);
}

}