NewRestResourceActionBase extends AbstractSoapUIAction<T> { protected RestResource createRestResource(T item, String path) { RestResource possibleParent = null; String pathWithoutEndpoint = removeEndpointFrom(path); for (RestResource resource : getResourcesFor(item)) { if (pathWithoutEndpoint.startsWith(resource.getFullPath() + "/")) { int c = 0; for (; c < resource.getChildResourceCount(); c++) { if (pathWithoutEndpoint.startsWith(resource.getChildResourceAt(c).getFullPath() + "/")) { break; } } if (c != resource.getChildResourceCount()) { continue; } possibleParent = resource; break; } } if (possibleParent != null && UISupport.confirm("Create resource as child to [" + possibleParent.getName() + "]", CONFIRM_DIALOG_TITLE)) { String strippedPath = pathWithoutEndpoint; if (pathWithoutEndpoint.length() > 0 && possibleParent.getFullPath().length() > 0) { strippedPath = pathWithoutEndpoint.substring(possibleParent.getFullPath().length() + 1); } return possibleParent.addNewChildResource(extractNameFromPath(strippedPath), strippedPath); } else { String pathWithoutLeadingSlash = pathWithoutEndpoint.startsWith("/") ? pathWithoutEndpoint.substring(1) : pathWithoutEndpoint; return addResourceTo(item, extractNameFromPath(pathWithoutEndpoint), pathWithoutLeadingSlash); } } NewRestResourceActionBase(String title, String description); void perform(T parent, Object param); static final String CONFIRM_DIALOG_TITLE; static final MessageSupport messages; }
@Test public void createsResourceAsChildResourceOfPossibleParent() throws Exception { stubbedDialogs.mockConfirmWithReturnValue(true); String childResourcePath = "anthony_jr"; action.createRestResource(service, ENDPOINT + "/" + PARENT_RESOURCE_PATH + "/" + childResourcePath); List<RestResource> rootLevelResources = service.getResourceList(); assertThat(rootLevelResources, is(aCollectionWithSize(1))); RestResource newChildResource = rootLevelResources.get(0).getAllChildResources()[0]; assertThat(newChildResource.getPath(), is(childResourcePath)); }
@Test public void handlesBasePathWhenFindingPossibleParent() throws Exception { String basePath = "/bada_bing/"; service.setBasePath(basePath); stubbedDialogs.mockConfirmWithReturnValue(true); String childResourcePath = "anthony_jr"; action.createRestResource(service, ENDPOINT + parentResource.getFullPath() + "/" + childResourcePath); List<RestResource> rootLevelResources = service.getResourceList(); assertThat(rootLevelResources, is(aCollectionWithSize(1))); RestResource newChildResource = rootLevelResources.get(0).getAllChildResources()[0]; assertThat(newChildResource.getPath(), is(childResourcePath)); }
@Test public void createsResourceAsRootLevelResourceWhenUserRejects() throws Exception { stubbedDialogs.mockConfirmWithReturnValue(false); String newResourcePath = "anthony_jr"; action.createRestResource(service, ENDPOINT + "/" + PARENT_RESOURCE_PATH + "/" + newResourcePath); List<RestResource> rootLevelResources = service.getResourceList(); assertThat(rootLevelResources, is(aCollectionWithSize(2))); }
@Test public void showsCorrectConfirmationDialog() throws Exception { stubbedDialogs.mockConfirmWithReturnValue(true); String childResourcePath = "anthony_jr"; action.createRestResource(service, ENDPOINT + "/" + PARENT_RESOURCE_PATH + "/" + childResourcePath); assertThat(stubbedDialogs.getConfirmations(), is(aCollectionWithSize(1))); StubbedDialogs.Confirmation confirmation = stubbedDialogs.getConfirmations().get(0); assertThat(confirmation.title, is(NewRestResourceAction.CONFIRM_DIALOG_TITLE)); }