RestResourceFinder { RestResource findResourceAt(int caretPosition) { String basePath = resource.getInterface().getBasePath(); if (!StringUtils.isNullOrEmpty(basePath) && caretPosition <= basePath.length()) { return null; } for (RestResource r : RestUtils.extractAncestorsParentFirst(resource)) { if (caretPosition <= r.getFullPath().length()) { return r; } } return resource; } RestResourceFinder(RestResource resource);   }
@Test public void findLonelyResource() throws SoapUIException { RestResource resource = ModelItemFactory.makeRestResource(); resource.setPath("/resource"); RestResourceFinder finder = new RestResourceFinder(resource); assertThat(finder.findResourceAt(1), is(resource)); }
@Test public void findParentInTwoLevelResource() throws SoapUIException { RestResource parent = ModelItemFactory.makeRestResource(); RestResource child = parent.addNewChildResource("child", "/child"); parent.setPath("/parent"); RestResourceFinder finder = new RestResourceFinder(child); assertThat(finder.findResourceAt(1), is(parent)); }
@Test public void findChildInTwoLevelResource() throws SoapUIException { RestResource parent = ModelItemFactory.makeRestResource(); parent.setPath("/parent"); RestResource child = parent.addNewChildResource("child", "/child"); RestResourceFinder finder = new RestResourceFinder(child); assertThat(finder.findResourceAt(10), is(child)); }
@Test public void findChildInTwoLevelResource2() throws SoapUIException { RestResource one = ModelItemFactory.makeRestResource(); one.setPath("/one"); RestResource two = one.addNewChildResource("two", "/two"); RestResource three = two.addNewChildResource("three", "/three"); RestResource four = three.addNewChildResource("four", "/four"); RestResourceFinder finder = new RestResourceFinder(four); assertThat(finder.findResourceAt(0), is(one)); assertThat(finder.findResourceAt(1), is(one)); assertThat(finder.findResourceAt(2), is(one)); assertThat(finder.findResourceAt(3), is(one)); assertThat(finder.findResourceAt(4), is(one)); assertThat(finder.findResourceAt(5), is(two)); assertThat(finder.findResourceAt(6), is(two)); assertThat(finder.findResourceAt(7), is(two)); assertThat(finder.findResourceAt(8), is(two)); assertThat(finder.findResourceAt(9), is(three)); assertThat(finder.findResourceAt(10), is(three)); assertThat(finder.findResourceAt(11), is(three)); assertThat(finder.findResourceAt(12), is(three)); assertThat(finder.findResourceAt(13), is(three)); assertThat(finder.findResourceAt(14), is(three)); assertThat(finder.findResourceAt(15), is(four)); assertThat(finder.findResourceAt(16), is(four)); assertThat(finder.findResourceAt(17), is(four)); assertThat(finder.findResourceAt(18), is(four)); assertThat(finder.findResourceAt(19), is(four)); assertThat(finder.findResourceAt(20), is(four)); }
@Test public void returnsNullWhenBasePathIsClicked() throws Exception { RestResource parent = ModelItemFactory.makeRestResource(); String basePath = "/base"; parent.getInterface().setBasePath(basePath); RestResourceFinder finder = new RestResourceFinder(parent); assertThat(finder.findResourceAt(basePath.length()), is(nullValue())); }