-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java lib API extension to handle new response volatility settings add…
…ed (see #67)
- Loading branch information
Showing
5 changed files
with
237 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...ain/java/com/epam/wilma/service/configuration/ResponseMessageVolatilityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...vice-api/src/main/java/com/epam/wilma/service/domain/ResponseMessageVolatilityStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
108 changes: 108 additions & 0 deletions
108
...java/com/epam/wilma/service/configuration/ResponseMessageVolatilityConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |