Skip to content

Commit 705adf2

Browse files
fhopfmohsinh
authored andcommitted
DATAES-117 - Displaying proper error message for entities that don't have an Id
1 parent a08a6fb commit 705adf2

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/support/ElasticsearchEntityInformationCreatorImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEnt
4747
.getPersistentEntity(domainClass);
4848

4949
Assert.notNull(persistentEntity, String.format("Unable to obtain mapping metadata for %s!", domainClass));
50+
Assert.notNull(persistentEntity.getIdProperty(), String.format("No id property found for %s!", domainClass));
5051

5152
return new MappingElasticsearchEntityInformation<T, ID>(persistentEntity);
5253
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.repository.support;
17+
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.mockito.Mock;
22+
import org.mockito.runners.MockitoJUnitRunner;
23+
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
24+
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
25+
import org.springframework.data.mapping.context.MappingContext;
26+
27+
import static org.mockito.Mockito.doReturn;
28+
29+
/**
30+
* @author Florian Hopf
31+
*/
32+
@RunWith(MockitoJUnitRunner.class)
33+
public class ElasticsearchEntityInformationCreatorImplTest {
34+
35+
@Mock
36+
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
37+
@Mock
38+
private ElasticsearchPersistentEntity<String> persistentEntity;
39+
private ElasticsearchEntityInformationCreatorImpl entityInfoCreator;
40+
41+
@Before
42+
public void before() {
43+
entityInfoCreator = new ElasticsearchEntityInformationCreatorImpl(mappingContext);
44+
}
45+
46+
@Test(expected = IllegalArgumentException.class)
47+
public void shouldThrowIllegalArgumentExceptionOnMissingEntity() {
48+
entityInfoCreator.getEntityInformation(String.class);
49+
}
50+
51+
@Test(expected = IllegalArgumentException.class)
52+
public void shouldThrowIllegalArgumentExceptionOnMissingIdAnnotation() {
53+
doReturn(persistentEntity).when(mappingContext).getPersistentEntity(String.class);
54+
doReturn(String.class).when(persistentEntity).getType();
55+
doReturn(null).when(persistentEntity).getIdProperty();
56+
entityInfoCreator.getEntityInformation(String.class);
57+
}
58+
59+
}

0 commit comments

Comments
 (0)