diff options
author | luxin <luxin7@huawei.com> | 2017-09-08 09:48:32 +0800 |
---|---|---|
committer | luxin <luxin7@huawei.com> | 2017-09-09 15:11:31 +0800 |
commit | 517452b495f79fd1e78d21bb2c38198be27f1bbc (patch) | |
tree | ee237a5b80c7d6940755577915adbfbc72a92128 /ResmanagementService | |
parent | f4475e0a471b8cbb6f366679f59f5a357d7f5ad2 (diff) |
Add UT code for vfc-resmanagement
Change-Id: I93b3c55452142d407c1a528790292f156f433683
Issue-Id:VFC-273
Signed-off-by: luxin <luxin7@huawei.com>
Diffstat (limited to 'ResmanagementService')
9 files changed, 2654 insertions, 1 deletions
diff --git a/ResmanagementService/deployment/src/main/release/db/mysql/db-schema.sql b/ResmanagementService/deployment/src/main/release/db/mysql/db-schema.sql index e53f856..efcaf54 100644 --- a/ResmanagementService/deployment/src/main/release/db/mysql/db-schema.sql +++ b/ResmanagementService/deployment/src/main/release/db/mysql/db-schema.sql @@ -177,7 +177,7 @@ CREATE TABLE vnfinfo ( DROP TABLE IF EXISTS ns; CREATE TABLE ns ( - ID VARCHAR(256) NOT NULL, + ID VARCHAR(128) NOT NULL, NAME VARCHAR(256) NULL, NSD_ID VARCHAR(256) NULL, DESCRIPTION VARCHAR(1024) NULL, diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/ReaderHelperTest.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/ReaderHelperTest.java new file mode 100644 index 0000000..f786c78 --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/ReaderHelperTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import static org.junit.Assert.assertEquals; + +import java.io.Reader; +import java.io.StringReader; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class ReaderHelperTest { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link org.openo.baseservice.util.inf.ReaderHelper#getLine()}. + */ + @Test + public void testGetLine() { + final String message = "hello.. how are you?"; + final Reader reader = new StringReader(message); + final ReaderHelper helper = new ReaderHelper(reader); + final String actual = helper.getLine(); + assertEquals(message, actual); + } + + /** + * Test method for {@link org.openo.baseservice.util.inf.ReaderHelper#getLine()}. + */ + @Test + public void testGetLineMultiLine() { + final String line1 = "hello.. how are you?"; + final String line2 = "I am fine."; + final Reader reader = new StringReader(line1 + System.lineSeparator() + line2); + final ReaderHelper helper = new ReaderHelper(reader); + String actual = helper.getLine(); + assertEquals(line1, actual); + actual = helper.getLine(); + assertEquals(line2, actual); + actual = helper.getLine(); + assertEquals(null, actual); + } + + @Test + public void testGetLineNull() { + final ReaderHelper helper = new ReaderHelper(null); + final String actual = helper.getLine(); + assertEquals(null, actual); + + } + +} diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestHttpRest.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestHttpRest.java new file mode 100644 index 0000000..0a6aa6a --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestHttpRest.java @@ -0,0 +1,958 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.lang.reflect.Field; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.HttpExchange; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import mockit.Mock; +import mockit.MockUp; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestHttpRest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testInitHttpRest() throws Exception { + final RestfulOptions options = new RestfulOptions(); + new MockUp<HttpClient>() { + + @Mock + public void doStart() { + } + }; + final HttpRest httpRest = new HttpRest(); + httpRest.initHttpRest(options); + final Field httpClient = HttpBaseRest.class.getDeclaredField("client"); + httpClient.setAccessible(true); + Assert.assertNotNull(httpClient.get(httpRest)); + } + + /** + * <br/> + * + * @throws NoSuchFieldException + * @throws Exception + * @since + */ + @Test + public void testCreateRestHttpContentExchange() throws NoSuchFieldException, Exception { + final HttpBaseRest httpRest = new HttpRest(); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + final RestHttpContentExchange exchange = httpRest.createRestHttpContentExchange(callback); + assertNotNull(exchange); + final Field callbackField = RestHttpContentExchange.class.getDeclaredField("callback"); + assertNotNull(callbackField); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetStringRestfulParametes() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", "application/json"); + parametes.putHttpContextHeader("Accept-Encoding", "*/*"); + final RestfulResponse response = httpRest.get("path/to/service", parametes); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetStringRestfulParametesRestfulOptions() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulResponse response = httpRest.get("path/to/service", new RestfulParametes(), options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testHeadStringRestfulParametes() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.head("path/to/service", parametes); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testHeadStringRestfulParametesRestfulOptions() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.head("path/to/service", parametes, options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @param options + * @return + * @throws ServiceException + * @since + */ + private HttpRest getHttpRest(final RestfulOptions options) throws ServiceException { + final HttpRest httpRest = new HttpRest(); + { + new MockUp<HttpClient>() { + + @Mock + public void doStart() { + } + + @Mock + public void send(final HttpExchange exchange) throws IOException { + } + }; + httpRest.initHttpRest(options); + + } + return httpRest; + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testAsyncGetStringRestfulParametesRestfulAsyncCallback() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncGet("path/to/service", new RestfulParametes(), callback); + httpRest.asyncGet("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncGetStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), callback); + httpRest.asyncGet("path/to/service", new RestfulParametes(), new RestfulOptions(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPutStringRestfulParametes() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.put("path/to/service", parametes); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPutStringRestfulParametesRestfulOptions() throws ServiceException { + + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parametes = new RestfulParametes(); + parametes.put("id", "1234"); + parametes.put("name", "some-name"); + parametes.put("address", null); + parametes.putHttpContextHeader("Content-Type", ""); + parametes.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.put("path/to/service", parametes, null); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPutStringRestfulParametesRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPut("path/to/service", new RestfulParametes(), callback); + httpRest.asyncPut("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testAsyncPutStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws Exception { + final RestfulOptions options = new RestfulOptions(); + + final HttpRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), callback); + httpRest.asyncPut("path/to/service", new RestfulParametes(), new RestfulOptions(), null); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testAsyncPostStringRestfulParametesRestfulAsyncCallback() throws Exception { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return 99; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_EXCEPTED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPostStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncPost("path/to/service", new RestfulParametes(), options, null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testDeleteStringRestfulParametes() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + + final RestfulResponse response = httpRest.delete("path/to/service", null); + assertEquals(-1, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testDeleteStringRestfulParametesRestfulOptions() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parameters = new RestfulParametes(); + parameters.put("id", "1234"); + parameters.put("name", "some-name"); + parameters.put("address", null); + parameters.setRawData("{ \"data\"=\"sample JSON data\""); + parameters.putHttpContextHeader("Content-Type", ""); + parameters.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.delete("path/to/service", parameters, options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncDeleteStringRestfulParametesRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncDelete("path/to/service", new RestfulParametes(), callback); + httpRest.asyncDelete("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncDeleteStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncDelete("path/to/service", new RestfulParametes(), options, null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPatchStringRestfulParametes() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parameters = new RestfulParametes(); + parameters.put("id", "1234"); + parameters.put("name", "some-name"); + parameters.put("address", null); + parameters.setRawData("{ \"data\"=\"sample JSON data\""); + parameters.putHttpContextHeader("Content-Type", ""); + parameters.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.patch("path/to/service", parameters); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testPatchStringRestfulParametesRestfulOptions() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + final RestfulParametes parameters = new RestfulParametes(); + parameters.put("id", "1234"); + parameters.put("name", "some-name"); + parameters.put("address", null); + parameters.setRawData("{ \"data\"=\"sample JSON data\""); + parameters.putHttpContextHeader("Content-Type", ""); + parameters.putHttpContextHeader("Accept-Encoding", ""); + final RestfulResponse response = httpRest.patch("path/to/service", parameters, options); + assertEquals(HttpExchange.STATUS_COMPLETED, response.getStatus()); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPatchStringRestfulParametesRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPatch("path/to/service", new RestfulParametes(), callback); + httpRest.asyncPatch("path/to/service", new RestfulParametes(), null); + } + + /** + * <br/> + * + * @throws ServiceException + * @since + */ + @Test + public void testAsyncPatchStringRestfulParametesRestfulOptionsRestfulAsyncCallback() throws ServiceException { + final RestfulOptions options = new RestfulOptions(); + options.setRestTimeout(10); + + final HttpBaseRest httpRest = getHttpRest(options); + new MockUp<RestHttpContentExchange>() { + + @Mock + public int waitForDone() { + return HttpExchange.STATUS_COMPLETED; + } + + @Mock + public RestfulResponse getResponse() throws IOException { + final RestfulResponse response = new RestfulResponse(); + response.setStatus(HttpExchange.STATUS_COMPLETED); + return response; + } + + }; + + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + + } + + @Override + public void handleExcepion(final Throwable e) { + + } + + }; + httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, callback); + httpRest.asyncPatch("path/to/service", new RestfulParametes(), options, null); + } + +} diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestHttpContentExchange.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestHttpContentExchange.java new file mode 100644 index 0000000..d89876a --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestHttpContentExchange.java @@ -0,0 +1,589 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.zip.GZIPInputStream; + +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.eclipse.jetty.client.Address; +import org.eclipse.jetty.client.CachedExchange; +import org.eclipse.jetty.client.HttpDestination; +import org.eclipse.jetty.client.HttpExchange; +import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpHeaders; +import org.eclipse.jetty.io.Buffer; +import org.eclipse.jetty.io.ByteArrayBuffer; +import org.eclipse.jetty.util.StringUtil; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import mockit.Mock; +import mockit.MockUp; +import mockit.Mocked; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestRestHttpContentExchange { + + @Mocked + HttpDestination mockedDest; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.ERROR); + } + + /** + * <br/> + * + * @throws IOException + * @since + */ + @Test + public void testOnRequestCommitted() throws IOException { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onRequestCommitted(); + + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG); + exchange.onRequestCommitted(); + } + + /** + * <br/> + * + * @throws IOException + * @since + */ + @Test + public void testOnRequestComplete() throws IOException { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onRequestComplete(); + + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG); + exchange.onRequestComplete(); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testOnResponseComplete() throws Exception { + RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onResponseComplete(); + + LogManager.getLogger(RestHttpContentExchange.class).setLevel(Level.DEBUG); + exchange.onResponseComplete(); + + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + } + + }; + + final Field statusField = HttpExchange.class.getDeclaredField("_status"); + statusField.setAccessible(true); + exchange = new RestHttpContentExchange(false, callback); + statusField.set(exchange, new AtomicInteger(200)); + exchange.setAddress(new Address("localhost", 9999)); + exchange.setRequestURI("/the/request/uri"); + exchange.onResponseComplete(); + assertEquals(1, isCallback.get()); + assertEquals(0, isException.get()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + @Ignore + public void testDecompressGzipToStr() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz"); + final byte[] binaryData = new byte[1024]; + stream.read(binaryData); + final String expected = "sample data."; + + final String actual = exchange.decompressGzipToStr(binaryData); + + assertEquals(actual, expected); + + new MockUp<ByteArrayInputStream>() { + + @Mock + public int read() throws Exception { + throw new IOException(); + } + + @Mock + public int read(final byte abyte0[], final int i, final int j) { + + return -1; + } + + }; + + thrown.expect(IOException.class); + exchange.decompressGzipToStr(binaryData); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + @Ignore + public void testDecompressGzipToStrException() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final InputStream stream = ClassLoader.getSystemResourceAsStream("sample.txt.gz"); + final byte[] binaryData = new byte[1024]; + stream.read(binaryData); + final String expected = "sample data."; + + new MockUp<GZIPInputStream>() { + + @Mock + public void close() throws IOException { + throw new IOException(); + } + + }; + + new MockUp<InputStreamReader>() { + + @Mock + public void close() throws IOException { + throw new IOException(); + } + + }; + + new MockUp<ByteArrayInputStream>() { + + @Mock + public void close() throws IOException { + throw new IOException(); + } + + }; + + final String actual = exchange.decompressGzipToStr(binaryData); + + System.out.println("actual: '" + actual + "'"); + System.out.println("expected: '" + expected + "'"); + assertEquals(actual, expected); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testDecompressGzipToStrNull() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + final String expected = ""; + final String actual = exchange.decompressGzipToStr(null); + + System.out.println("actual: '" + actual + "'"); + System.out.println("expected: '" + expected + "'"); + assertEquals(actual, expected); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testOnResponseHeaderBufferBuffer() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final Buffer name = new ByteArrayBuffer("key"); + final Buffer value = new ByteArrayBuffer("value"); + exchange.onResponseHeader(name, value); + + new MockUp<HttpHeaders>() { + + @Mock + public int getOrdinal(final Buffer buffer) { + return HttpHeaders.CONTENT_ENCODING_ORDINAL; + } + + }; + exchange.onResponseHeader(name, value); + + new MockUp<StringUtil>() { + + @Mock + public String asciiToLowerCase(final String s) { + return "gzip"; + } + + }; + exchange.onResponseHeader(name, value); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnExceptionThrowable() { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onException(new Exception()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnExceptionThrowableWithCallback() { + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + } + + }; + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onException(new Exception()); + assertEquals(0, isCallback.get()); + assertEquals(1, isException.get()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnConnectionFailedThrowable() { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onConnectionFailed(new Exception()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testOnConnectionFailedThrowableException() { + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + } + + }; + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.onConnectionFailed(new Exception()); + assertEquals(0, isCallback.get()); + assertEquals(1, isException.get()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testExpireHttpDestination() { + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.expire(mockedDest); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testExpireHttpDestinationException() throws Exception { + final AtomicInteger isCallback = new AtomicInteger(0); + final AtomicInteger isException = new AtomicInteger(0); + final List<Throwable> thrSet = new ArrayList<Throwable>(); + final RestfulAsyncCallback callback = new RestfulAsyncCallback() { + + @Override + public void callback(final RestfulResponse response) { + isCallback.set(1); + } + + @Override + public void handleExcepion(final Throwable e) { + isException.set(1); + thrSet.add(e); + } + + }; + final RestHttpContentExchange exchange = new RestHttpContentExchange(true, callback); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + exchange.expire(mockedDest); + assertEquals(0, isCallback.get()); + assertEquals(1, isException.get()); + assertEquals(1, thrSet.size()); + final Throwable t = thrSet.get(0); + assertEquals(ServiceException.class, t.getClass()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testIsGzip() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final Buffer name = new ByteArrayBuffer("key"); + final Buffer value = new ByteArrayBuffer("value"); + + new MockUp<HttpHeaders>() { + + @Mock + public int getOrdinal(final Buffer buffer) { + return HttpHeaders.CONTENT_ENCODING_ORDINAL; + } + + }; + exchange.onResponseHeader(name, value); + assertFalse(exchange.isGzip()); + + new MockUp<StringUtil>() { + + @Mock + public String asciiToLowerCase(final String s) { + return "gzip"; + } + + }; + exchange.onResponseHeader(name, value); + assertTrue(exchange.isGzip()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetResponse() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + + final Field statusField = HttpExchange.class.getDeclaredField("_status"); + statusField.setAccessible(true); + statusField.set(exchange, new AtomicInteger(200)); + + RestfulResponse response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + + final HttpFields fields = new HttpFields(); + final Field headerFields = CachedExchange.class.getDeclaredField("_responseFields"); + headerFields.setAccessible(true); + headerFields.set(exchange, fields); + response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + fields.add("Content-Type", "application/json"); + fields.add("Content-Encode", "UTF-8"); + response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + } + + /** + * <br/> + * + * @throws Exception + * @since + */ + @Test + public void testGetResponseGzip() throws Exception { + final RestHttpContentExchange exchange = new RestHttpContentExchange(false, null); + final Address address = new Address("localhost", 9999); + exchange.setAddress(address); + exchange.setRequestURI("/the/request/uri"); + new MockUp<RestHttpContentExchange>() { + + @Mock + public boolean isGzip() { + return true; + } + }; + final Field statusField = HttpExchange.class.getDeclaredField("_status"); + statusField.setAccessible(true); + statusField.set(exchange, new AtomicInteger(200)); + + final RestfulResponse response = exchange.getResponse(); + assertEquals(0, response.getStatus()); + } +} diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulFactory.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulFactory.java new file mode 100644 index 0000000..bd2acf7 --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulFactory.java @@ -0,0 +1,107 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; + +import mockit.Mock; +import mockit.MockUp; +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestRestfulFactory { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRestInstance() { + Restful instance = RestfulFactory.getRestInstance("https"); + assertNull(instance); + new MockUp<HttpRest>() { + + @Mock + public void initHttpRest(final RestfulOptions option) throws ServiceException { + throw new ServiceException(); + } + + }; + instance = RestfulFactory.getRestInstance("http"); + assertNotNull(instance); + + instance = RestfulFactory.getRestInstance("http"); + assertNotNull(instance); + } +} diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulOptions.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulOptions.java new file mode 100644 index 0000000..83889bd --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulOptions.java @@ -0,0 +1,288 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import mockit.integration.junit4.JMockit; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +@RunWith(JMockit.class) +public class TestRestfulOptions { + + @Rule + final public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetPort() { + final RestfulOptions options = new RestfulOptions(); + final int port = 9091; + assertEquals(0, options.getPort()); + options.setPort(port); + assertEquals(port, options.getPort()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetPort() { + final RestfulOptions options = new RestfulOptions(); + final int port = 9091; + assertTrue(options.setPort(port)); + assertEquals(port, options.getPort()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetHost() { + final RestfulOptions options = new RestfulOptions(); + final String host = "localhost"; + assertEquals("", options.getHost()); + options.setHost(host); + assertEquals(host, options.getHost()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetHost() { + final RestfulOptions options = new RestfulOptions(); + final String host = "localhost"; + assertTrue(options.setHost(host)); + assertEquals(host, options.getHost()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetRestTimeout() { + final RestfulOptions options = new RestfulOptions(); + int timeout = 0; + assertFalse(options.setRestTimeout(timeout)); + assertEquals(0, options.getRestTimeout()); + + timeout = 1; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = 10; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT - 1; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + + timeout = RestfulOptions.REST_OPTIONS_TIMEOUT_MAXTIMEOUT + 1; + assertFalse(options.setRestTimeout(timeout)); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRestTimeout() { + final RestfulOptions options = new RestfulOptions(); + int timeout = 0; + assertEquals(0, options.getRestTimeout()); + + timeout = 1; + assertTrue(options.setRestTimeout(timeout)); + assertEquals(timeout, options.getRestTimeout()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetOption() { + final RestfulOptions options = new RestfulOptions(); + assertNull(options.getOption("invalid")); + + options.setHost("localhost"); + Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME); + assertNotNull(obj); + assertTrue(obj instanceof String); + assertEquals("localhost", obj); + + final List<String> list = new ArrayList<String>(); + list.add("data"); + options.setOption("list", list); + obj = options.getOption("list"); + assertNotNull(obj); + assertTrue(obj instanceof List); + assertSame(list, obj); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetIntOption() { + final RestfulOptions options = new RestfulOptions(); + + assertEquals(0, options.getIntOption("count")); + + options.setOption("count", 1); + assertEquals(1, options.getIntOption("count")); + + thrown.expect(RuntimeException.class); + + options.setOption("string-count", "two"); + final int value = options.getIntOption("string-count"); + assertEquals(2, value); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetStringOption() { + final RestfulOptions options = new RestfulOptions(); + + assertEquals("", options.getStringOption("count")); + + options.setOption("string-count", "one"); + assertEquals("one", options.getStringOption("string-count")); + + thrown.expect(RuntimeException.class); + + options.setOption("count", 2); + final String value = options.getStringOption("count"); + assertEquals(2, value); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetOption() { + final RestfulOptions options = new RestfulOptions(); + assertNull(options.getOption("invalid")); + + options.setHost("localhost"); + Object obj = options.getOption(RestfulClientConst.HOST_KEY_NAME); + assertNotNull(obj); + assertTrue(obj instanceof String); + assertEquals("localhost", obj); + + final List<String> list = new ArrayList<String>(); + list.add("data"); + options.setOption("list", list); + obj = options.getOption("list"); + assertNotNull(obj); + assertTrue(obj instanceof List); + assertSame(list, obj); + } +} diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulParametes.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulParametes.java new file mode 100644 index 0000000..617d09d --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulParametes.java @@ -0,0 +1,229 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class TestRestfulParametes { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGet() { + final RestfulParametes params = new RestfulParametes(); + assertNull(params.get("param")); + final Map<String, String> paramMap = new HashMap<String, String>(); + params.setParamMap(paramMap); + paramMap.put("param", "value"); + assertEquals("value", params.get("param")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetRawData() { + final RestfulParametes params = new RestfulParametes(); + final String data = "Sample data."; + params.setRawData(data); + assertEquals(data, params.getRawData()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRawData() { + final RestfulParametes params = new RestfulParametes(); + assertNull(params.getRawData()); + final String data = "Sample data."; + params.setRawData(data); + assertEquals(data, params.getRawData()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testPut() { + final RestfulParametes params = new RestfulParametes(); + params.put("somekey", "somevalue"); + params.put("otherkey", "othervalue"); + assertEquals("somevalue", params.get("somekey")); + assertEquals("othervalue", params.get("otherkey")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testPutHttpContextHeaderStringString() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("Context-Encoding", "UTF-8"); + assertEquals("UTF-8", params.getHttpContextHeader("Context-Encoding")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testPutHttpContextHeaderStringInt() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("Expire-At", 1000); + assertEquals("1000", params.getHttpContextHeader("Expire-At")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetHttpContextHeader() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("Expire-At", 1000); + params.putHttpContextHeader("Context-Encoding", "UTF-8"); + assertEquals("1000", params.getHttpContextHeader("Expire-At")); + assertEquals("UTF-8", params.getHttpContextHeader("Context-Encoding")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetParamMap() { + final RestfulParametes params = new RestfulParametes(); + params.put("key", "value"); + final Map<String, String> map = params.getParamMap(); + assertEquals("value", map.get("key")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetParamMap() { + final RestfulParametes params = new RestfulParametes(); + final Map<String, String> map = new HashMap<String, String>(); + map.put("key", "value"); + params.setParamMap(map); + assertEquals("value", params.get("key")); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetHeaderMap() { + final RestfulParametes params = new RestfulParametes(); + params.putHttpContextHeader("key", "value"); + final Map<String, String> map = params.getHeaderMap(); + assertEquals("value", map.get("key")); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetHeaderMap() { + final RestfulParametes params = new RestfulParametes(); + final Map<String, String> map = new HashMap<String, String>(); + map.put("key", "value"); + params.setHeaderMap(map); + assertEquals("value", params.getHttpContextHeader("key")); + } +} diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulResponse.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulResponse.java new file mode 100644 index 0000000..eaec342 --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestRestfulResponse.java @@ -0,0 +1,273 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class TestRestfulResponse { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetStatus() { + final RestfulResponse response = new RestfulResponse(); + int actual = response.getStatus(); + int expected = -1; + + assertEquals(expected, actual); + expected = 202; + response.setStatus(expected); + actual = response.getStatus(); + assertEquals(expected, actual); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetStatus() { + final RestfulResponse response = new RestfulResponse(); + final int expected = 10; + response.setStatus(expected); + final int actual = response.getStatus(); + assertEquals(expected, actual); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderMap() { + final RestfulResponse response = new RestfulResponse(); + Map<String, String> expected = response.getRespHeaderMap(); + assertNull(expected); + expected = new HashMap<String, String>(); + expected.put("key", "value"); + response.setRespHeaderMap(expected); + final Map<String, String> actual = response.getRespHeaderMap(); + assertNotNull(actual); + assertSame(actual, expected); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetRespHeaderMap() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + Map<String, String> expected = response.getRespHeaderMap(); + assertNull(expected); + expected = new HashMap<String, String>(); + expected.put("key", "value"); + response.setRespHeaderMap(expected); + final Map<String, String> actual = response.getRespHeaderMap(); + assertNotNull(actual); + assertSame(actual, expected); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderInt() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + int actual = response.getRespHeaderInt("somekey"); + assertEquals(-1, actual); + final Map<String, String> headers = new HashMap<String, String>(); + headers.put("key", "value"); + headers.put("count", "1"); + response.setRespHeaderMap(headers); + actual = response.getRespHeaderInt("somekey"); + assertEquals(-1, actual); + + actual = response.getRespHeaderInt("count"); + assertEquals(1, actual); + + thrown.expect(RuntimeException.class); + actual = response.getRespHeaderInt("key"); + assertEquals(1, actual); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderLong() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + long actual = response.getRespHeaderLong("somekey"); + assertEquals(-1, actual); + final Map<String, String> headers = new HashMap<String, String>(); + headers.put("key", "value"); + headers.put("count", "1"); + headers.put("max", "" + Long.MAX_VALUE); + headers.put("max++", Long.MAX_VALUE + 1 + ""); + response.setRespHeaderMap(headers); + actual = response.getRespHeaderLong("somekey"); + assertEquals(-1, actual); + + actual = response.getRespHeaderLong("count"); + assertEquals(1, actual); + + actual = response.getRespHeaderLong("max"); + assertEquals(Long.MAX_VALUE, actual); + + actual = response.getRespHeaderLong("max++"); + assertTrue(actual < 0); + + thrown.expect(RuntimeException.class); + actual = response.getRespHeaderLong("key"); + assertEquals(1, actual); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetRespHeaderStr() { + final RestfulResponse response = new RestfulResponse(); + response.setRespHeaderMap(null); + String actual = response.getRespHeaderStr("somekey"); + assertEquals(null, actual); + final Map<String, String> headers = new HashMap<String, String>(); + headers.put("key", "value"); + headers.put("count", "1"); + headers.put("max", "" + Long.MAX_VALUE); + response.setRespHeaderMap(headers); + actual = response.getRespHeaderStr("somekey"); + assertEquals(null, actual); + + actual = response.getRespHeaderStr("key"); + assertEquals("value", actual); + + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetResponseContent() { + final RestfulResponse response = new RestfulResponse(); + assertEquals(null, response.getResponseContent()); + + final String content = "{ \"content\" = \"The response content\" }"; + response.setResponseJson(content); + assertEquals(content, response.getResponseContent()); + } + + /** + * <br/> + * + * @since + */ + @Test + public void testSetResponseJson() { + final RestfulResponse response = new RestfulResponse(); + assertEquals(null, response.getResponseContent()); + + final String content = "{ \"content\" = \"The response content\" }"; + response.setResponseJson(content); + assertEquals(content, response.getResponseContent()); + } +} diff --git a/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestServiceUtil.java b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestServiceUtil.java new file mode 100644 index 0000000..bccbdb8 --- /dev/null +++ b/ResmanagementService/service/src/test/java/org/onap/vfc/nfvo/resmanagement/common/util/restclient/TestServiceUtil.java @@ -0,0 +1,92 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.resmanagement.common.util.restclient; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version + */ +public class TestServiceUtil { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetServiceHost() { + } + + /** + * <br/> + * + * @since + */ + @Test + public void testGetServicePort() { + } +} |