Skip to content

Commit

Permalink
Java lib API extension to handle new response volatility settings add…
Browse files Browse the repository at this point in the history
…ed (see #67)
  • Loading branch information
tkohegyi committed Dec 30, 2015
1 parent 6f8c31c commit d5de241
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public MessageMarkingConfiguration(WilmaServiceConfig config, WilmaHttpClient cl
}

/**
* Gets the localhost blocking status.
* Gets the message marking status.
*
* @return localhost blocking status in JSONObject
* @return message marking status in JSONObject
*/
public MessageMarkingStatus getMessageMarkingStatus() {
LOG.debug("Call message marking status API.");
Expand All @@ -80,9 +80,9 @@ public MessageMarkingStatus getMessageMarkingStatus() {
}

/**
* Sets the localhost blocking status.
* Sets the message marking status.
*
* @param control the new blocking status
* @param control the new message marking status
* @return <tt>true</tt> if the request is successful, otherwise return <tt>false</tt>
*/
public boolean setMessageMarkingStatus(MessageMarkingStatus control) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.epam.wilma.service.configuration;

/*==========================================================================
Copyright 2015 EPAM Systems
This file is part of Wilma.
Wilma is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wilma is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wilma. If not, see <http://www.gnu.org/licenses/>.
===========================================================================*/

import com.epam.wilma.service.domain.ResponseMessageVolatilityStatus;
import com.epam.wilma.service.domain.WilmaServiceConfig;
import com.epam.wilma.service.http.WilmaHttpClient;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Collects the message marking related commands.
*
* @author Tamas_Kohegyi
*/
public class ResponseMessageVolatilityConfiguration extends AbstractConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(ResponseMessageVolatilityConfiguration.class);

private static final String STATUS_GETTER_URL_POSTFIX = "config/public/responsevolatility/status";
private static final String STATUS_SETTER_URL_POSTFIX_FORMAT = "config/admin/responsevolatility/%s";

/**
* Constructor.
*
* @param config the Wilma server configuration
*/
public ResponseMessageVolatilityConfiguration(WilmaServiceConfig config) {
super(config);
}

/**
* Constructor.
*
* @param config the Wilma server configuration
* @param client the Wilma HTTP client
*/
public ResponseMessageVolatilityConfiguration(WilmaServiceConfig config, WilmaHttpClient client) {
super(config, client);
}

/**
* Gets the response volatility status.
*
* @return response volatility status in JSONObject
*/
public ResponseMessageVolatilityStatus getResponseMessageVolatilityStatus() {
LOG.debug("Call response message volatility status API.");
ResponseMessageVolatilityStatus status = null;

JSONObject o = getterRequest(STATUS_GETTER_URL_POSTFIX);
if (o != null) {
Boolean responseVolatility = (Boolean) o.get("responseVolatility");

if (responseVolatility) {
status = ResponseMessageVolatilityStatus.ON;
} else {
status = ResponseMessageVolatilityStatus.OFF;
}
}
return status;
}

/**
* Sets the response volatility status.
*
* @param control the new response volatility status
* @return <tt>true</tt> if the request is successful, otherwise return <tt>false</tt>
*/
public boolean setResponseMessageVolatilityStatus(ResponseMessageVolatilityStatus control) {
LOG.debug("Call response message volatility setter API with value: " + control);

String postfix = String.format(STATUS_SETTER_URL_POSTFIX_FORMAT, control.toString().toLowerCase());
return setterRequest(postfix);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
===========================================================================*/

/**
* Represents the possible states of localhost blocking.
* Represents the possible states of message marking.
*
* @author Tamas_Kohegyi
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.epam.wilma.service.domain;

/*==========================================================================
Copyright 2015 EPAM Systems
This file is part of Wilma.
Wilma is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wilma is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wilma. If not, see <http://www.gnu.org/licenses/>.
===========================================================================*/

/**
* Represents the possible states of response message volatility.
*
* @author Tamas_Kohegyi
*/
public enum ResponseMessageVolatilityStatus {

ON, OFF;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.epam.wilma.service.configuration;

/*==========================================================================
Copyright 2015 EPAM Systems
This file is part of Wilma.
Wilma is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Wilma is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Wilma. If not, see <http://www.gnu.org/licenses/>.
===========================================================================*/

import com.epam.wilma.service.domain.ResponseMessageVolatilityStatus;
import com.epam.wilma.service.domain.WilmaServiceConfig;
import com.epam.wilma.service.http.WilmaHttpClient;
import com.google.common.base.Optional;
import org.junit.Assert;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static com.epam.wilma.service.domain.ResponseMessageVolatilityStatus.OFF;
import static com.epam.wilma.service.domain.ResponseMessageVolatilityStatus.ON;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

/**
* Unit test for {@link ResponseMessageVolatilityConfiguration}.
*
* @author Tamas_Kohegyi
*/
public class ResponseMessageVolatilityConfigurationTest {

private static final String HOST = "host";
private static final Integer PORT = 1;
private static final String MARKING_STATUS_URL = "http://host:1/config/public/responsevolatility/status";
private static final String MARKING_STATUS_SETTER_URL_ON = "http://host:1/config/admin/responsevolatility/on";
private static final String MARKING_STATUS_SETTER_URL_OFF = "http://host:1/config/admin/responsevolatility/off";
private static final String JSON_STRING = "{\"responseVolatility\":true}";

@Mock
private WilmaHttpClient client;

private ResponseMessageVolatilityConfiguration responseMessageVolatilityConfiguration;

@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);

WilmaServiceConfig config = createMockConfig();
responseMessageVolatilityConfiguration = new ResponseMessageVolatilityConfiguration(config, client);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void shouldThrowExceptionWhenConfigIsMissing() {
new ResponseMessageVolatilityConfiguration(null);
}

@Test
public void shouldReturnNullIfClientReturnsOptionalAbsent() {
when(client.sendGetterRequest(MARKING_STATUS_URL)).thenReturn(Optional.<String>absent());

ResponseMessageVolatilityStatus result = responseMessageVolatilityConfiguration.getResponseMessageVolatilityStatus();

assertNull(result);
verify(client, never()).sendSetterRequest(anyString());
}

@Test
public void shouldReturnJSONObjectWithProperValue() {
when(client.sendGetterRequest(MARKING_STATUS_URL)).thenReturn(Optional.of(JSON_STRING));

ResponseMessageVolatilityStatus result = responseMessageVolatilityConfiguration.getResponseMessageVolatilityStatus();

Assert.assertTrue(result == ResponseMessageVolatilityStatus.ON);
verify(client, never()).sendSetterRequest(anyString());
}

@Test
public void shouldReturnWithProperBooleanValueForSetterRequest() {
when(client.sendSetterRequest(MARKING_STATUS_SETTER_URL_ON)).thenReturn(true);
when(client.sendSetterRequest(MARKING_STATUS_SETTER_URL_OFF)).thenReturn(false);

assertTrue(responseMessageVolatilityConfiguration.setResponseMessageVolatilityStatus(ON));
assertFalse(responseMessageVolatilityConfiguration.setResponseMessageVolatilityStatus(OFF));
verify(client, never()).sendGetterRequest(anyString());
}

private WilmaServiceConfig createMockConfig() {
return WilmaServiceConfig.getBuilder()
.withHost(HOST)
.withPort(PORT)
.build();
}
}

0 comments on commit d5de241

Please sign in to comment.