DATALDAP-5 - Add CDI extension for LDAP repositories.
We now provide a CDI extension to create LDAP repositories in a CDI container. LDAP repositories require a LdapOperations bean as underlying resource manager for LDAP access.
class LdapTemplateProducer {
@Produces
@Singleton
public LdapTemplate createLdapTemplate() {
LdapTemplate ldapTemplateMock = …
return ldapTemplateMock;
}
}
class RepositoryClient {
@Inject
PersonRepository repository;
}
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.ldap.repository.cdi;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.enterprise.inject.se.SeContainer;
|
||||
import javax.enterprise.inject.se.SeContainerInitializer;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.ldap.config.DummyEntity;
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link LdapRepositoryExtension}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class CdiExtensionIntegrationTests {
|
||||
|
||||
static SeContainer container;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
|
||||
container = SeContainerInitializer.newInstance() //
|
||||
.disableDiscovery() //
|
||||
.addPackages(CdiExtensionIntegrationTests.class) //
|
||||
.initialize();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
container.close();
|
||||
}
|
||||
|
||||
@Test // DATALDAP-5
|
||||
public void bootstrapsRepositoryCorrectly() {
|
||||
|
||||
RepositoryClient client = container.select(RepositoryClient.class).get();
|
||||
LdapTemplate ldapTemplateMock = client.getLdapTemplate();
|
||||
|
||||
DummyEntity entity = new DummyEntity();
|
||||
when(ldapTemplateMock.findAll(DummyEntity.class)).thenReturn(Collections.singletonList(entity));
|
||||
|
||||
SampleRepository repository = client.getSampleRepository();
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
verify(client.getLdapTemplate()).delete(entity);
|
||||
}
|
||||
|
||||
@Test // DATALDAP-5
|
||||
public void returnOneFromCustomImpl() {
|
||||
|
||||
RepositoryClient repositoryConsumer = container.select(RepositoryClient.class).get();
|
||||
assertThat(repositoryConsumer.getSampleRepository().returnOne()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.ldap.repository.cdi;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import javax.enterprise.inject.Produces;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
|
||||
/**
|
||||
* Simple component exposing a {@link LdapTemplate} instance as CDI bean.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class LdapTemplateProducer {
|
||||
|
||||
@Produces
|
||||
@Singleton
|
||||
public LdapTemplate createLdapTemplate() {
|
||||
|
||||
LdapTemplate ldapTemplateMock = mock(LdapTemplate.class);
|
||||
ObjectDirectoryMapper odmMock = mock(ObjectDirectoryMapper.class);
|
||||
|
||||
when(ldapTemplateMock.getObjectDirectoryMapper()).thenReturn(odmMock);
|
||||
|
||||
return ldapTemplateMock;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.ldap.repository.cdi;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Getter
|
||||
class RepositoryClient {
|
||||
|
||||
@Inject SampleRepository sampleRepository;
|
||||
@Inject LdapTemplate ldapTemplate;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.ldap.repository.cdi;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
import org.springframework.data.ldap.config.DummyEntity;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface SampleRepository extends CrudRepository<DummyEntity, Name>, SampleRepositoryCustom {}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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.ldap.repository.cdi;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
interface SampleRepositoryCustom {
|
||||
|
||||
int returnOne();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.ldap.repository.cdi;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class SampleRepositoryImpl implements SampleRepositoryCustom {
|
||||
|
||||
@Override
|
||||
public int returnOne() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user