我开始在我的一些代码中使用 neo4j-spatial。我认为我可以通过将空间服务器库作为 maven 依赖项包含在我的项目中来集成测试 neo4j-spatial 代码。不过,这对我不起作用。我在任何地方都找不到任何文档。
我怎样才能让我的集成测试工作?
任何提示任何人?:)
只是为了说明我在做什么,我在下面粘贴了我的控制器、服务和存储库代码的一部分,最后的代码发布是我的测试,这些测试不适用于嵌入式 TestServer。
存储库
package nz.co.domain.core.repository.location;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import nz.co.domain.model.pojos.Location;
@Repository
public interface LocationRepository extends GraphRepository<Location> {
@Query("match (n:Location {domainId : {domainId}}) with n call spatial.addNode({layerName}, n) yield node return node;")
public Location indexLocation(@Param("domainId") String locationId, @Param("layerName") String layerName);
@Query("call spatial.withinDistance({layerName},{longitude: {longitude},latitude: {latitude}}, {rangeInKms});")
public Iterable<Location> findLocationsWithinRange(@Param("longitude") String longitude, @Param("latitude") String latitude, @Param("rangeInKms") String rangeInKms, @Param("layerName") String layerName);
@Query("call spatial.addPointLayer({layerName});")
public void createLayer(@Param("layerName") String layerName);
@Query("match ()-[:LAYER]->(n) where n.layer = {layerName} return count(n) > 0;")
public boolean hasLayer(@Param("layerName") String layerName);
public Location findByDomainSpecificId(String domainSpecificId);
public Location findByGooglePlaceId(String googlePlaceId);
}
服务
package nz.co.domain.core.location;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import nz.co.domain.core.repository.location.LocationRepository;
import nz.co.domain.model.UniqueIDGenerator;
import nz.co.domain.model.pojos.Location;
@Service
public class LocationService {
@Inject
private LocationRepository locationRepository;
public void createLayer(String layerName) {
locationRepository.createLayer(layerName);
}
public Location createLocation(double latitude, double longitude, String googlePlaceId, String layerName) {
Location location = new Location(UniqueIDGenerator.randomID(), googlePlaceId, latitude, longitude);
boolean hasLayer = locationRepository.hasLayer(layerName);
if (!hasLayer) {
locationRepository.createLayer(layerName);
}
Location preExistingLocation = locationRepository.findByGooglePlaceId(googlePlaceId);
if (preExistingLocation == null) {
location = locationRepository.save(location);
location = locationRepository.indexLocation(location.getDomainId(), layerName);
} else {
location = preExistingLocation;
}
return location;
}
public Iterable<Location> findLocationsWithinRange(String latitude, String longitude, String rangeInKms, String layerName) {
return locationRepository.findLocationsWithinRange(longitude, latitude, rangeInKms, layerName);
}
public Location loadLocationByGooglePlaceId(String googlePlaceId) {
return locationRepository.findByGooglePlaceId(googlePlaceId);
}
public Location loadLocationByDomainId(String domainId) {
return locationRepository.findByDomainId(domainId);
}
}
控制器
...
/**
* Add location.
* @param profiletypes
* @param profileId
* @return
*/
@RequestMapping(value = "/{profileType}/{profileId}/location", method = RequestMethod.POST, produces = "application/hal+json")
public HttpEntity<LocationResource> addLocation(@PathVariable String profileType,
@PathVariable String profileId, @RequestBody CreateLocationRequest locationRequest, @ApiIgnore LocationResourceAssembler locationResourceAssembler) {
Profile profile = profileService.loadProfileByDomainId(profileId);
Location location = locationService.createLocation(locationRequest.getLatitude(), locationRequest.getLongitude(), locationRequest.getGooglePlaceId(), profileType + "-layer");
profile.setLocation(location);
profileService.save(profile);
location = locationService.loadLocationByGooglePlaceId(location.getGooglePlaceId());
LocationResource resource = locationResourceAssembler.toResource(location);
return new ResponseEntity<>(resource, HttpStatus.CREATED) ;
}
...
服务测试
(这是我的测试,我无法作为针对嵌入式 TestServer 的标准构建的一部分工作)
package nz.co.domain.core.location;
import javax.inject.Inject;
import org.junit.Ignore;
import org.junit.Test;
import nz.co.domain.core.AbstractTest;
import nz.co.domain.model.pojos.Location;
public class LocationServiceTest extends AbstractTest {
@Inject
private LocationService locationService;
@Test
public void indexLocationTest() {
// The Rogue and Vagabond
Location rogueAndVagabond = locationService.createLocation(51.469150, 7.23212, "ChIJmwfKGdivOG0R9eTCVFOngnU", "test-layer");
/* more test code here */
// Te Papa Museum
Location tePapaMuseum = locationService.createLocation(-41.289964, 174.778354, "ChIJfxn9AdGvOG0RpLRGGO3tRX8", "test-layer");
/* more test code here */
// Porirua Club
Location poriruaClub = locationService.createLocation(-41.136048, 174.836409, "ChIJ9wl16m1TP20R3G3npuEokak", "test-layer");
/* more test code here */
Iterable<Location> findLocationsWithinRange = locationService.findLocationsWithinRange("-41.289964", "longitude", "5", "test-layer");
/* more test code here */
}
}