Skip to content

Commit

Permalink
SONAR-8581 WS api/navigation/global use the new PagesDefinition API
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Jan 13, 2017
1 parent 4548ea6 commit fc86449
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 86 deletions.
Expand Up @@ -17,6 +17,7 @@
* along with this program; if not, write to the Free Software Foundation, * along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */

package org.sonar.server.ui.ws; package org.sonar.server.ui.ws;


import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
Expand All @@ -29,12 +30,9 @@
import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService.NewController; import org.sonar.api.server.ws.WebService.NewController;
import org.sonar.api.utils.text.JsonWriter; import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.NavigationSection;
import org.sonar.api.web.Page;
import org.sonar.db.Database; import org.sonar.db.Database;
import org.sonar.db.dialect.H2; import org.sonar.db.dialect.H2;
import org.sonar.server.ui.ViewProxy; import org.sonar.server.ui.PageRepository;
import org.sonar.server.ui.Views;


import static org.sonar.api.CoreProperties.CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY; import static org.sonar.api.CoreProperties.CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY;
import static org.sonar.api.CoreProperties.HOURS_IN_DAY; import static org.sonar.api.CoreProperties.HOURS_IN_DAY;
Expand All @@ -57,14 +55,14 @@ public class GlobalAction implements NavigationWsAction {
RATING_GRID, RATING_GRID,
CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY); CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY);


private final Views views; private final PageRepository pageRepository;
private final Settings settings; private final Settings settings;
private final ResourceTypes resourceTypes; private final ResourceTypes resourceTypes;
private final Server server; private final Server server;
private final Database database; private final Database database;


public GlobalAction(Views views, Settings settings, ResourceTypes resourceTypes, Server server, Database database) { public GlobalAction(PageRepository pageRepository, Settings settings, ResourceTypes resourceTypes, Server server, Database database) {
this.views = views; this.pageRepository = pageRepository;
this.settings = settings; this.settings = settings;
this.resourceTypes = resourceTypes; this.resourceTypes = resourceTypes;
this.server = server; this.server = server;
Expand Down Expand Up @@ -95,13 +93,11 @@ public void handle(Request request, Response response) throws Exception {


private void writePages(JsonWriter json) { private void writePages(JsonWriter json) {
json.name("globalPages").beginArray(); json.name("globalPages").beginArray();
for (ViewProxy<Page> page : views.getPages(NavigationSection.HOME)) { for (org.sonar.api.web.page.Page page : pageRepository.getGlobalPages(false)) {
if (page.isUserAuthorized()) { json.beginObject()
json.beginObject() .prop("key", page.getKey())
.prop("id", page.getId()) .prop("name", page.getName())
.prop("name", page.getTitle()) .endObject();
.endObject();
}
} }
json.endArray(); json.endArray();
} }
Expand Down
@@ -1,12 +1,12 @@
{ {
"globalPages": [ "globalPages": [
{ {
"id": "my_plugin_page", "key": "another_plugin/page",
"name": "My Plugin Page" "name": "My Another Page"
}, },
{ {
"id": "my_rails_app", "key": "my_plugin/page",
"name": "My Rails App" "name": "My Plugin Page"
} }
], ],
"settings": { "settings": {
Expand Down
Expand Up @@ -17,6 +17,7 @@
* along with this program; if not, write to the Free Software Foundation, * along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */

package org.sonar.server.ui.ws; package org.sonar.server.ui.ws;


import org.junit.Rule; import org.junit.Rule;
Expand All @@ -27,18 +28,18 @@
import org.sonar.api.resources.ResourceType; import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypeTree; import org.sonar.api.resources.ResourceTypeTree;
import org.sonar.api.resources.ResourceTypes; import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.web.NavigationSection; import org.sonar.api.web.page.Page;
import org.sonar.api.web.Page; import org.sonar.api.web.page.PageDefinition;
import org.sonar.api.web.UserRole;
import org.sonar.api.web.View;
import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.platform.PluginRepository;
import org.sonar.db.Database; import org.sonar.db.Database;
import org.sonar.db.dialect.H2; import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MySql; import org.sonar.db.dialect.MySql;
import org.sonar.server.tester.UserSessionRule; import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ui.Views; import org.sonar.server.ui.PageRepository;
import org.sonar.server.ws.WsActionTester; import org.sonar.server.ws.WsActionTester;


import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.sonar.test.JsonAssert.assertJson; import static org.sonar.test.JsonAssert.assertJson;
Expand All @@ -64,7 +65,7 @@ public void empty_call() throws Exception {


@Test @Test
public void return_qualifiers() throws Exception { public void return_qualifiers() throws Exception {
init(new View[] {}, new ResourceTypeTree[] { init(new Page[] {}, new ResourceTypeTree[] {
ResourceTypeTree.builder() ResourceTypeTree.builder()
.addType(ResourceType.builder("POL").build()) .addType(ResourceType.builder("POL").build())
.addType(ResourceType.builder("LOP").build()) .addType(ResourceType.builder("LOP").build())
Expand Down Expand Up @@ -108,22 +109,22 @@ public void return_deprecated_logo_settings() throws Exception {


@Test @Test
public void return_global_pages_for_anonymous() throws Exception { public void return_global_pages_for_anonymous() throws Exception {
init(createViews(), new ResourceTypeTree[] {}); init(createPages(), new ResourceTypeTree[] {});


executeAndVerify("global_pages_for_anonymous.json"); executeAndVerify("global_pages_for_anonymous.json");
} }


@Test @Test
public void return_global_pages_for_user() throws Exception { public void return_global_pages_for_user() throws Exception {
init(createViews(), new ResourceTypeTree[] {}); init(createPages(), new ResourceTypeTree[] {});
userSessionRule.login("obiwan"); userSessionRule.login("obiwan");


executeAndVerify("global_pages_for_user.json"); executeAndVerify("global_pages_for_user.json");
} }


@Test @Test
public void return_global_pages_for_admin() throws Exception { public void return_global_pages_for_admin_user() throws Exception {
init(createViews(), new ResourceTypeTree[] {}); init(createPages(), new ResourceTypeTree[] {});
userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);


executeAndVerify("global_pages_for_admin.json"); executeAndVerify("global_pages_for_admin.json");
Expand All @@ -147,7 +148,7 @@ public void return_if_production_database_or_not() throws Exception {


@Test @Test
public void test_example_response() throws Exception { public void test_example_response() throws Exception {
init(createViews(), new ResourceTypeTree[] { init(createPages(), new ResourceTypeTree[] {
ResourceTypeTree.builder() ResourceTypeTree.builder()
.addType(ResourceType.builder("POL").build()) .addType(ResourceType.builder("POL").build())
.addType(ResourceType.builder("LOP").build()) .addType(ResourceType.builder("LOP").build())
Expand Down Expand Up @@ -175,56 +176,31 @@ public void test_example_response() throws Exception {
} }


private void init() { private void init() {
init(new View[] {}, new ResourceTypeTree[] {}); init(new org.sonar.api.web.page.Page[] {}, new ResourceTypeTree[] {});
} }


private void init(View[] views, ResourceTypeTree[] resourceTypeTrees) { private void init(org.sonar.api.web.page.Page[] pages, ResourceTypeTree[] resourceTypeTrees) {
when(database.getDialect()).thenReturn(new H2()); when(database.getDialect()).thenReturn(new H2());
ws = new WsActionTester(new GlobalAction(new Views(userSessionRule, views), settings, new ResourceTypes(resourceTypeTrees), server, database)); PluginRepository pluginRepository = mock(PluginRepository.class);
when(pluginRepository.hasPlugin(anyString())).thenReturn(true);
PageRepository pageRepository = new PageRepository(pluginRepository, new PageDefinition[] {context -> {
for (Page page : pages) {
context.addPage(page);
}
}});
pageRepository.start();
ws = new WsActionTester(new GlobalAction(pageRepository, settings, new ResourceTypes(resourceTypeTrees), server, database));
} }


private void executeAndVerify(String json) { private void executeAndVerify(String json) {
assertJson(ws.newRequest().execute().getInput()).isSimilarTo(getClass().getResource(GlobalActionTest.class.getSimpleName() + "/" + json)); assertJson(ws.newRequest().execute().getInput()).isSimilarTo(getClass().getResource(GlobalActionTest.class.getSimpleName() + "/" + json));
} }


private View[] createViews() { private Page[] createPages() {
Page page = new Page() { Page page = Page.builder("my_plugin/page").setName("My Plugin Page").build();
@Override Page anotherPage = Page.builder("another_plugin/page").setName("My Another Page").build();
public String getTitle() { Page adminPage = Page.builder("my_plugin/admin_page").setName("Admin Page").setAdmin(true).build();
return "My Plugin Page";
}

@Override
public String getId() {
return "my_plugin_page";
}
};


Page controller = new Page() { return new Page[] {page, anotherPage, adminPage};
@Override
public String getTitle() {
return "My Rails App";
}

@Override
public String getId() {
return "my_rails_app";
}
};

@NavigationSection(NavigationSection.HOME)
@UserRole(GlobalPermissions.SYSTEM_ADMIN)
class AdminPage implements Page {
@Override
public String getTitle() {
return "Admin Page";
}

@Override
public String getId() {
return "admin_page";
}
}
return new View[] {page, controller, new AdminPage()};
} }
} }
@@ -1,16 +1,12 @@
{ {
"globalPages": [ "globalPages": [
{ {
"id": "my_plugin_page", "key": "another_plugin/page",
"name": "My Plugin Page" "name": "My Another Page"
},
{
"id": "my_rails_app",
"name": "My Rails App"
}, },
{ {
"id": "admin_page", "key": "my_plugin/page",
"name": "Admin Page" "name": "My Plugin Page"
} }
] ]
} }
@@ -1,12 +1,12 @@
{ {
"globalPages": [ "globalPages": [
{ {
"id": "my_plugin_page", "key": "another_plugin/page",
"name": "My Plugin Page" "name": "My Another Page"
}, },
{ {
"id": "my_rails_app", "key": "my_plugin/page",
"name": "My Rails App" "name": "My Plugin Page"
} }
] ]
} }
@@ -1,12 +1,12 @@
{ {
"globalPages": [ "globalPages": [
{ {
"id": "my_plugin_page", "key": "another_plugin/page",
"name": "My Plugin Page" "name": "My Another Page"
}, },
{ {
"id": "my_rails_app", "key": "my_plugin/page",
"name": "My Rails App" "name": "My Plugin Page"
} }
] ]
} }

0 comments on commit fc86449

Please sign in to comment.