Search in sources :

Example 16 with GoApiResponse

use of com.thoughtworks.go.plugin.api.response.GoApiResponse in project gocd by gocd.

the class PluginSettingsRequestProcessorTest method shouldGetPluginSettingsForPluginThatExistsInDB.

@Test
public void shouldGetPluginSettingsForPluginThatExistsInDB() {
    String PLUGIN_ID = "plugin-foo-id";
    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenReturn(new Plugin(PLUGIN_ID, "{\"k1\": \"v1\",\"k2\": \"v2\"}"));
    String responseBody = "expected-response";
    Map<String, String> settingsMap = new HashMap<>();
    settingsMap.put("k1", "v1");
    settingsMap.put("k2", "v2");
    when(pluginExtension.canHandlePlugin(PLUGIN_ID)).thenReturn(true);
    when(pluginExtension.pluginSettingsJSON(PLUGIN_ID, settingsMap)).thenReturn(responseBody);
    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody("expected-request");
    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);
    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(), is(responseBody));
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) GoApiResponse(com.thoughtworks.go.plugin.api.response.GoApiResponse) HashMap(java.util.HashMap) DefaultGoApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest) NullPlugin(com.thoughtworks.go.domain.NullPlugin) Plugin(com.thoughtworks.go.domain.Plugin) Test(org.junit.Test)

Example 17 with GoApiResponse

use of com.thoughtworks.go.plugin.api.response.GoApiResponse in project gocd by gocd.

the class PluginSettingsRequestProcessorTest method shouldNotGetPluginSettingsForPluginThatDoesNotExistInDB.

@Test
public void shouldNotGetPluginSettingsForPluginThatDoesNotExistInDB() {
    String PLUGIN_ID = "plugin-foo-id";
    String requestBody = "expected-request";
    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenReturn(new NullPlugin());
    when(pluginExtension.canHandlePlugin(PLUGIN_ID)).thenReturn(true);
    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody(requestBody);
    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);
    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(), is(nullValue()));
    verify(pluginExtension).pluginSettingsJSON(PLUGIN_ID, Collections.EMPTY_MAP);
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) GoApiResponse(com.thoughtworks.go.plugin.api.response.GoApiResponse) NullPlugin(com.thoughtworks.go.domain.NullPlugin) DefaultGoApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest) Test(org.junit.Test)

Example 18 with GoApiResponse

use of com.thoughtworks.go.plugin.api.response.GoApiResponse in project gocd by gocd.

the class PluginSettingsRequestProcessorTest method shouldRespondWith400IfPluginExtensionErrorsOut.

@Test
public void shouldRespondWith400IfPluginExtensionErrorsOut() {
    String PLUGIN_ID = "plugin-foo-id";
    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenReturn(new Plugin(PLUGIN_ID, "{\"k1\": \"v1\",\"k2\": \"v2\"}"));
    when(pluginExtension.canHandlePlugin(PLUGIN_ID)).thenReturn(true);
    when(pluginExtension.pluginSettingsJSON(eq(PLUGIN_ID), any(Map.class))).thenThrow(RuntimeException.class);
    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody("expected-request");
    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);
    assertThat(response.responseCode(), is(400));
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) GoApiResponse(com.thoughtworks.go.plugin.api.response.GoApiResponse) DefaultGoApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest) HashMap(java.util.HashMap) Map(java.util.Map) NullPlugin(com.thoughtworks.go.domain.NullPlugin) Plugin(com.thoughtworks.go.domain.Plugin) Test(org.junit.Test)

Example 19 with GoApiResponse

use of com.thoughtworks.go.plugin.api.response.GoApiResponse in project gocd by gocd.

the class ServerInfoRequestProcessorTest method shouldReturnAServerIdInJSONForm.

@Test
public void shouldReturnAServerIdInJSONForm() throws Exception {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_ID, "1.0", new GoPluginIdentifier("extension1", Arrays.asList("1.0")));
    when(pluginExtension.canHandlePlugin(pluginId)).thenReturn(true);
    when(pluginExtension.serverInfoJSON(pluginId, serverConfig.getServerId(), serverConfig.getSiteUrl().getUrl(), serverConfig.getSecureSiteUrl().getUrl())).thenReturn("server_info");
    GoApiResponse response = processor.process(pluginDescriptor, request);
    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(), is("server_info"));
}
Also used : GoPluginIdentifier(com.thoughtworks.go.plugin.api.GoPluginIdentifier) GoApiResponse(com.thoughtworks.go.plugin.api.response.GoApiResponse) DefaultGoApiRequest(com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest) Test(org.junit.Test)

Example 20 with GoApiResponse

use of com.thoughtworks.go.plugin.api.response.GoApiResponse in project gocd by gocd.

the class SessionRequestProcessorTest method shouldGetFromSession.

@Test
public void shouldGetFromSession() {
    String pluginId = "plugin-id-1";
    Map<String, String> sessionData = new HashMap<>();
    sessionData.put("k3", "v3");
    sessionData.put("k4", "v4");
    when(session.getAttribute(pluginId)).thenReturn(sessionData);
    String requestBody = "expected-request";
    when(jsonMessageHandler.requestMessageSessionGetAndRemove(requestBody)).thenReturn(pluginId);
    SessionRequestProcessor applicationAccessorSpy = spy(processor);
    doReturn(session).when(applicationAccessorSpy).getUserSession();
    GoApiResponse response = applicationAccessorSpy.process(pluginDescriptor, getGoPluginApiRequest("1.0", SessionRequestProcessor.GET_FROM_SESSION, requestBody));
    assertThat(response.responseCode(), is(200));
    assertEquals(JsonHelper.fromJson(response.responseBody(), Map.class), sessionData);
    verify(session).getAttribute(pluginId);
}
Also used : GoApiResponse(com.thoughtworks.go.plugin.api.response.GoApiResponse) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

GoApiResponse (com.thoughtworks.go.plugin.api.response.GoApiResponse)23 Test (org.junit.Test)23 DefaultGoApiRequest (com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest)10 GoPluginIdentifier (com.thoughtworks.go.plugin.api.GoPluginIdentifier)6 NullPlugin (com.thoughtworks.go.domain.NullPlugin)4 HashMap (java.util.HashMap)4 Plugin (com.thoughtworks.go.domain.Plugin)3 Gson (com.google.gson.Gson)2 ElasticAgentMetadata (com.thoughtworks.go.server.domain.ElasticAgentMetadata)2 Map (java.util.Map)2 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 User (com.thoughtworks.go.plugin.access.authentication.models.User)1 AuthorizationMessageConverterV1 (com.thoughtworks.go.plugin.access.authorization.AuthorizationMessageConverterV1)1 GoApiRequest (com.thoughtworks.go.plugin.api.request.GoApiRequest)1 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)1 PluginRoleService (com.thoughtworks.go.server.service.PluginRoleService)1 ServerHealthState (com.thoughtworks.go.serverhealth.ServerHealthState)1 InOrder (org.mockito.InOrder)1 PreAuthenticatedAuthenticationToken (org.springframework.security.providers.preauth.PreAuthenticatedAuthenticationToken)1