@Override public void execute(EndpointRepository sut) { sut.add(ep1); } }
@Override public void execute(EndpointRepository sut) { sut.add(ep1); } }
@Override public void execute(EndpointRepository sut) { sut.add(ep2); } }
@Override public void execute(EndpointRepository sut) { sut.add(ep1); } }
private Endpoint createEndpoint(Resource resource, String relativePath, EntityTag entityTag, Date lastModified) throws EndpointCreationException { String candidatePath=relativePath; int repetitions=0; while(repetitions<MAX_ENDPOINT_CREATION_FAILURE) { LOGGER.debug("({}) Creating endpoint for {} [{},{},{}]",repetitions,resource.id(),entityTag,lastModified,relativePath); try { String resourcePath = calculateResourcePath(resource,candidatePath); LOGGER.debug("({}) Trying resource path {} ",repetitions,resourcePath); Endpoint newEndpoint = this.modelFactory.createEndpoint(resourcePath, resource, lastModified, entityTag); this.endpointRepository.add(newEndpoint); return newEndpoint; } catch (EndpointNotFoundException e) { throw new EndpointCreationException("Could not calculate path for resource '"+resource.id()+"'",e); } catch (IllegalArgumentException e) { LOGGER.debug("Could not create endpoint ({} --> {})",repetitions,candidatePath,e); repetitions++; candidatePath=null; } } throw new EndpointCreationException("Could not create endpoint for resource '"+resource.id()+"' after "+MAX_ENDPOINT_CREATION_FAILURE+" tries"); }
@Override public void execute(EndpointRepository sut) { Endpoint newEndpoint = endpoint(path,resource); sut.add(newEndpoint); debug("Adding endpoint: %s",newEndpoint); } }
@Test public void testPublishResource$preexisting$noClash() throws ApplicationConfigurationException { this.endpointRepository.add(this.endpoint); this.resourceRepository.add(this.resource); this.sut.publishResource(NAME1,PersonHandler.class,VALID_PATH); this.sut.configureRootResources(); Resource resource1 = verifyResource(NAME1, PersonHandler.class); assertThat(resource1,sameInstance(this.resource)); Endpoint endpoint1 = verifyEndpoint(VALID_PATH, resource1); assertThat(endpoint1,sameInstance(this.endpoint)); }
@Test public void testPublishResource$preexisting$idClash() throws ApplicationConfigurationException { this.endpointRepository.add(this.endpoint); this.resourceRepository.add(this.resource); this.sut.publishResource(NAME1,PersonHandler.class,"anotherPath/"); try { this.sut.configureRootResources(); fail("Should not allow publishing root resources with preexisting id clash"); } catch (ApplicationConfigurationException e) { logFailure(e); } }
@Test public void testPublishResource$preexisting$pathClash() throws ApplicationConfigurationException { this.endpointRepository.add(this.endpoint); this.resourceRepository.add(this.resource); this.sut.publishResource(NAME2,PersonHandler.class,VALID_PATH); try { this.sut.configureRootResources(); fail("Should not allow publishing root resources with preexisting path clash"); } catch (ApplicationConfigurationException e) { logFailure(e); } }
private void publish(RootResource rootResource, Date creationDate) throws ApplicationConfigurationException { ResourceId resourceId = rootResource.resourceId(); String path = rootResource.path(); Resource prevResource = this.resourceRepository.resourceById(resourceId,Resource.class); Endpoint prevEndpoint = this.endpointRepository.endpointOfPath(path); if(prevEndpoint!=null && !prevEndpoint.resourceId().equals(resourceId)) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as that path is already in use by a resource %s",toString(resourceId),path,toString(prevEndpoint.resourceId()))); } if(prevEndpoint==null) { if(prevResource!=null) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as it is already published at '%s'",toString(resourceId),path,this.endpointRepository.endpointOfResource(resourceId).path())); } else { Resource resource=this.modelFactory.createResource(rootResource.template(),rootResource.name()); this.resourceRepository.add(resource); Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,creationDate,EntityTag.createStrong(path)); this.endpointRepository.add(endpoint); } } }
private <T extends Resource> T publishResource(Class<? extends T> clazz, String templateId, Name<?> resourceName, String path) { Transaction transaction = RuntimeDelegate.getInstance().getTransactionManager().currentTransaction(); transaction.begin(); Resource newResource=this.modelFactory.createResource(this.tms.templateOfId(templateId),resourceName); T resource=clazz.cast(newResource); RuntimeDelegate.getInstance().getResourceRepository().add(resource); Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,new Date(),EntityTag.createStrong(path)); RuntimeDelegate.getInstance().getEndpointRepository().add(endpoint); transaction.commit(); return resource; }
@Test public void testPublishResource$preexisting$corrupted() throws ApplicationConfigurationException { Resource createResource = TestingModelFactory.createResource(NAME1, "UnknownTemplate"); this.endpointRepository.add(createEndpoint(VALID_PATH, createResource)); this.resourceRepository.add(createResource); this.sut.publishResource(NAME2,PersonHandler.class,VALID_PATH); try { this.sut.configureRootResources(); fail("Should not allow publishing root resources with preexisting path clash"); } catch (ApplicationConfigurationException e) { logFailure(e); } }
private Resource initialize(final String id, final String path) { final Transaction transaction= RuntimeDelegate. getInstance(). getTransactionManager(). currentTransaction(); transaction.begin(); try { this.uow = UnitOfWork.newCurrent(); final Resource resource= this.modelFactory.createResource( this.templateManagementService.templateOfId("personTemplate"), name(id)); final Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,new Date(),EntityTag.createStrong(path)); RuntimeDelegate.getInstance().getResourceRepository().add(resource); RuntimeDelegate.getInstance().getEndpointRepository().add(endpoint); UnitOfWork.setCurrent(null); transaction.commit(); return resource; } finally { if(transaction.isActive()) { transaction.rollback(); } } }