Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CUSTCOM-75 Fix Payara System Property REST API #4497

Merged
merged 5 commits into from Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -38,14 +38,15 @@
only if the new code is made subject to such option by the copyright
holder.

Portions Copyright [2020] [Payara Foundation and/or its affiliates]
-->

<!initPage
setResourceBundle(key="i18n" bundle="org.glassfish.admingui.core.Strings")
setResourceBundle(key="i18nc" bundle="org.glassfish.common.admingui.Strings")
setResourceBundle(key="help_common" bundle="org.glassfish.common.admingui.Helplinks");
/>
<!composition template="/templates/default.layout" guiTitle="$resource{i18n.instanceValues.PageTitle}" >
<!composition template="/templates/default.layout" guiTitle="$resource{i18n.instanceValues.PageTitle,$pageSession{propName}}" >
<!define name="content">
<event>
<!beforeCreate
Expand Down
Expand Up @@ -38,6 +38,7 @@
only if the new code is made subject to such option by the copyright
holder.

Portions Copyright [2020] [Payara Foundation and/or its affiliates]
-->
<sun:panelGroup id="topButtons">
<sun:button id="saveButton" text="$resource{i18n.button.Save}" >
Expand All @@ -46,7 +47,7 @@
foreach (var="row", list="#{pageSession.tableList}") {
mapPut(map="#{requestScope.data}", key="#{row.name}", value="#{row.overrideValue}");
}
gf.restRequest(endpoint="#{pageSession.selfUrl}", method="POST", attrs="#{requestScope.data}", contentType="application/x-www-form-urlencoded", result="#{requestScope.restResponse}");
gf.restRequest(endpoint="#{pageSession.selfUrl}", method="PUT", attrs="#{requestScope.data}", contentType="application/x-www-form-urlencoded", result="#{requestScope.restResponse}");
prepareSuccessfulMsg();
gf.redirect(page="#{pageSession.selfPage}&alertType=${alertType}&alertSummary=${alertSummary}&alertDetail=${alertDetail}");
/>
Expand Down
Expand Up @@ -41,16 +41,11 @@

import static org.junit.Assert.assertEquals;

import com.gargoylesoftware.htmlunit.HttpMethod;

import org.junit.Test;

import fish.payara.samples.NotMicroCompatible;

/**
* Test the REST management interface for it's basic HTTP responses
*/
@NotMicroCompatible
public class HttpResponsesTest extends RestManagementTest {

/**
Expand All @@ -59,7 +54,11 @@ public class HttpResponsesTest extends RestManagementTest {
*/
@Test
public void when_DELETE_non_existent_resource_expect_404() {
int status = request(HttpMethod.DELETE, "servers/server/server/application-ref/non-existent-application-ref").getStatus();
int status = target
.path("servers/server/server/application-ref/non-existent-application-ref")
.request()
.delete()
.getStatus();
assertEquals("A non-existent resource should return a 404.", 404, status);
}

Expand Down
Expand Up @@ -39,33 +39,29 @@
*/
package fish.payara.samples.rest.management;

import static org.junit.Assert.assertNotNull;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import com.gargoylesoftware.htmlunit.HttpMethod;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.Archive;
import org.junit.Before;
import org.junit.runner.RunWith;

import fish.payara.samples.NotMicroCompatible;
import fish.payara.samples.PayaraArquillianTestRunner;
import fish.payara.samples.PayaraTestShrinkWrap;
import fish.payara.samples.ServerOperations;
import fish.payara.samples.rest.management.util.RestManagementClientBuilder;

@RunWith(PayaraArquillianTestRunner.class)
@NotMicroCompatible
public abstract class RestManagementTest {

private WebTarget target;
protected WebTarget target;

@ArquillianResource
private URL baseUrl;
Expand All @@ -78,43 +74,12 @@ public static Archive<?> deploy() {

@Before
public final void setUpFields() throws URISyntaxException {
URI adminBaseUrl = ServerOperations.toAdminPort(baseUrl).toURI();
assertNotNull("Something went wrong with the test, and an admin port cannot be found.", adminBaseUrl);
target = ClientBuilder
.newClient()
.target(adminBaseUrl.resolve("/management/domain/").toString());
}

/**
* Make a synchronous request to the REST management interface.
*
* @param method the HTTP method to use for the request
* @param path the path of the request, relative to /management/domain/
* @param entity the entity to send in the request
*
* @return the response of the request.
*/
protected Response request(HttpMethod method, String path, Entity<?> entity) {
return target
.path(path)
.request()
.header("X-Requested-By", "Payara")
.build(method.toString(), entity)
.invoke();
}


/**
* Make a synchronous request to the REST management interface. This method
* sends nothing in the body. To send data, see
* {@link #request(HttpMethod, String, Entity)}.
*
* @param method the HTTP method to use for the request
* @param path the path of the request, relative to /management/domain/
*
* @return the response of the request.
*/
protected Response request(HttpMethod method, String path) {
return request(method, path, null);
URI adminBaseUri;
try {
adminBaseUri = ServerOperations.toAdminPort(baseUrl).toURI();
} catch (Exception ex) {
throw new IllegalStateException("Unable to find admin base URL. Should this profile have an admin console?", ex);
}
target = RestManagementClientBuilder.newClient(adminBaseUri);
}
}
@@ -0,0 +1,160 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.samples.rest.management;

import static java.lang.String.format;
import static java.util.stream.Collectors.toMap;
import static javax.ws.rs.client.Entity.entity;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

import javax.json.JsonObject;
import javax.json.JsonString;

import org.jboss.arquillian.junit.InSequence;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Test;

import fish.payara.samples.rest.management.extension.TemporaryInstance;

/**
* Test the system property endpoint of the REST management interface
*/
public class SystemPropertyTest extends RestManagementTest {

@ArquillianResource
private TemporaryInstance instance;

/**
* Tests that when a POST request is made to the endpoint the system properties
* posted are added.
*/
@Test
@InSequence(1)
public void when_POST_system_properties_expect_success() {
Map<String, Object> properties = new HashMap<>();
properties.put("ASADMIN_LISTENER_PORT", 12345);
properties.put("HTTP_LISTENER_PORT", 23456);
properties.put("HTTP_SSL_LISTENER_PORT", 34567);

target.path(systemPropertiesEndpoint())
.request()
.post(entity(properties, APPLICATION_JSON));

Map<String, String> systemProperties = getSystemProperties();

verifySystemProperty("ASADMIN_LISTENER_PORT", properties, systemProperties);
verifySystemProperty("HTTP_LISTENER_PORT", properties, systemProperties);
verifySystemProperty("HTTP_SSL_LISTENER_PORT", properties, systemProperties);
assertEquals("No other system properties should exist before they've been added.", 3, systemProperties.size());
}

/**
* Tests that when a PUT request is made all other system properties are
* overwritten. See CUSTCOM-75 for details.
*/
@Test
@InSequence(2)
public void when_PUT_system_property_expect_others_are_deleted() {
Map<String, Object> properties = new HashMap<>();
properties.put("HTTP_LISTENER_PORT", 45678);
properties.put("HTTP_SSL_LISTENER_PORT", 56789);

target.path(systemPropertiesEndpoint())
.request()
.put(entity(properties, APPLICATION_JSON));

Map<String, String> systemProperties = getSystemProperties();

verifySystemProperty("HTTP_LISTENER_PORT", properties, systemProperties);
verifySystemProperty("HTTP_SSL_LISTENER_PORT", properties, systemProperties);
assertEquals("No other system properties should exist after a PUT.", 2, systemProperties.size());
}

/**
* Tests that when a POST request is made the posted variables are updated and
* the others remain. See CUSTCOM-75 for details.
*/
@Test
@InSequence(3)
public void when_POST_system_property_expect_only_change_submitted() {
Map<String, Object> properties = new HashMap<>();
properties.put("HTTP_LISTENER_PORT", 67891);

target.path(systemPropertiesEndpoint())
.request()
.post(entity(properties, APPLICATION_JSON));

Map<String, String> systemProperties = getSystemProperties();

verifySystemProperty("HTTP_LISTENER_PORT", properties, systemProperties);
assertEquals("All other system properties should be maintained after a POST.", 2, systemProperties.size());
}

private Map<String, String> getSystemProperties() {
return target.path(systemPropertiesEndpoint())
.request()
.get(JsonObject.class)
.getJsonObject("extraProperties")
.getJsonArray("systemProperties")
.parallelStream()
.filter(value -> value.asJsonObject().get("value") != null)
.collect(toMap(
value -> ((JsonString) value.asJsonObject().get("name")).getString(),
value -> ((JsonString) value.asJsonObject().get("value")).getString()));
}

private String systemPropertiesEndpoint() {
return format("servers/server/%s/system-properties", instance.getName());
}

private static void verifySystemProperty(String propertyName, Map<String, ? extends Object> localMap, Map<String, ? extends Object> systemProperties) {
assertTrue(String.format("The %s system property wasn't found.", propertyName),
systemProperties.containsKey(propertyName));
assertEquals(String.format("The %s system property was incorrect.", propertyName),
localMap.get(propertyName).toString(),
systemProperties.get(propertyName).toString());
}

}