From b15b381c5fa631699c1a3d27e482d3d77bed6ad8 Mon Sep 17 00:00:00 2001 From: Zhaoxing Date: Sat, 30 Sep 2017 14:08:50 +0800 Subject: Add unit test for vfc-nfvo-wfengine Change-Id: If70f670ce62c3ca577d11b036e705b07a80f82cf Issue-id: VFC-454 Signed-off-by: Zhaoxing --- .../restservicetask/HighLevelRestApiTest.java | 277 ++++++++++++--------- 1 file changed, 162 insertions(+), 115 deletions(-) (limited to 'activiti-extension/src/test/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApiTest.java') diff --git a/activiti-extension/src/test/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApiTest.java b/activiti-extension/src/test/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApiTest.java index 7e791f7..0f20a1e 100644 --- a/activiti-extension/src/test/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApiTest.java +++ b/activiti-extension/src/test/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApiTest.java @@ -1,4 +1,4 @@ -/* +/** * Copyright 2017 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,129 +18,176 @@ package org.onap.workflow.activitiext.restservicetask; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.methods.GetMethod; import org.junit.Before; import org.junit.Test; +import org.junit.Assert; import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.onap.workflow.activitiext.common.RestInfo; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -@PrepareForTest({HighLevelRestApi.class,LowLevelRestApi.class}) +@PrepareForTest({LowLevelRestApi.class}) @RunWith(PowerMockRunner.class) public class HighLevelRestApiTest { - - private HighLevelRestApi highLevelRestApi; - - @Before - public void setUp() { - highLevelRestApi = new HighLevelRestApi(); - } - - @Test - public void testGet() { - - try { - HttpResponseMessage msg = new HttpResponseMessage(); - msg.setStatusCode(200); - msg.setResponseBody(null); - PowerMockito.mockStatic(LowLevelRestApi.class); - PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); - - String uri = "10.74.148.107/openoapi/catalog/v1/csars"; - String acceptValue = "application/json"; - String contentTypeValue = "application/json"; - - HighLevelRestApi.Get(uri, acceptValue, contentTypeValue); - assertThat(HighLevelRestApi.Get(uri, acceptValue, contentTypeValue), is(msg)); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Test - public void testPost() { - - try { - HttpResponseMessage msg = new HttpResponseMessage(); - msg.setStatusCode(200); - msg.setResponseBody(null); - PowerMockito.mockStatic(LowLevelRestApi.class); - PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); - - String uri = "10.74.148.107/openoapi/catalog/v1/csars"; - String requestBody = "{'type':'NFAR'}"; - String acceptValue = "application/json"; - String contentTypeValue = "application/json"; - - assertThat(HighLevelRestApi.Post(uri, requestBody, acceptValue, contentTypeValue), is(msg)); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Test - public void testDelete() { - - try { - HttpResponseMessage msg = new HttpResponseMessage(); - msg.setStatusCode(200); - msg.setResponseBody(null); - PowerMockito.mockStatic(LowLevelRestApi.class); - PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); - - String uri = "10.74.148.107/openoapi/catalog/v1/csars/aa1bc611c9fbc08247d5ea71fd67ec3f"; - String acceptValue = "application/json"; - String contentTypeValue = "application/json"; - - assertThat(HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue), is(msg)); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Test - public void testPut() { - - try { - HttpResponseMessage msg = new HttpResponseMessage(); - msg.setStatusCode(200); - msg.setResponseBody(null); - PowerMockito.mockStatic(LowLevelRestApi.class); - PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); - - String uri = "10.74.148.107/openoapi/catalog/v1/csars"; - String requestBody = "{'type':'NFAR'}"; - String acceptValue = "application/json"; - String contentTypeValue = "application/json"; - - assertThat(HighLevelRestApi.Put(uri, requestBody, acceptValue, contentTypeValue), is(msg)); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Test - public void testCreateNameValuePairArrayFromQuery() { - NameValuePair[] pair = new NameValuePair[2]; - NameValuePair p1 = new NameValuePair(); - p1.setName("name"); - p1.setValue("liuyao"); - NameValuePair p2 = new NameValuePair(); - p2.setName("pwd"); - p2.setValue("zte"); - - pair[0] = p1; - pair[1] = p2; - - String query = "name=liuyao&pwd=zte"; - - assertThat(highLevelRestApi.createNameValuePairArrayFromQuery(query), is(pair)); - } - + + private HighLevelRestApi highLevelRestApi; + + @Before + public void setUp() { + highLevelRestApi = new HighLevelRestApi(); + } + + @Test + public void testInvoke() { + + try { + RestInfo info = new RestInfo(); + info.setMethod("GET"); + info.setName("name"); + info.setPath("/catalog/v1"); + info.setUrl("csars"); + info.setRealUri(""); + info.setVersion("v1"); + + HttpResponseMessage msg = new HttpResponseMessage(); + msg.setStatusCode(200); + msg.setResponseBody(null); + PowerMockito.mockStatic(LowLevelRestApi.class); + PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); + + assertThat(HighLevelRestApi.invoke(info), is(msg));; + } catch (Exception e) { + assert (false); + } + } + + @Test + public void testGet() { + + try { + HttpResponseMessage msg = new HttpResponseMessage(); + msg.setStatusCode(200); + msg.setResponseBody(null); + PowerMockito.mockStatic(LowLevelRestApi.class); + PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); + + String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5"; + String acceptValue = "application/json"; + String contentTypeValue = "application/json"; + + assertThat(HighLevelRestApi.Get(uri, acceptValue, contentTypeValue), is(msg)); + } catch (Exception e) { + assert (false); + } + } + + @Test + public void testPost() { + + try { + HttpResponseMessage msg = new HttpResponseMessage(); + msg.setStatusCode(200); + msg.setResponseBody(null); + PowerMockito.mockStatic(LowLevelRestApi.class); + PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); + + String uri = "10.74.148.107/openoapi/catalog/v1/dbtest?id=5"; + String requestBody = "{'type':'NFAR'}"; + String acceptValue = "application/json"; + String contentTypeValue = "application/json"; + + assertThat(HighLevelRestApi.Post(uri, requestBody, acceptValue, contentTypeValue), is(msg)); + } catch (Exception e) { + assert (false); + } + } + + @Test + public void testDelete() { + + try { + HttpResponseMessage msg = new HttpResponseMessage(); + msg.setStatusCode(200); + msg.setResponseBody(null); + PowerMockito.mockStatic(LowLevelRestApi.class); + PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); + + String uri = "10.74.148.107/openoapi/catalog/v1/csars/aa1bc611c9fbc08247d5ea71fd67ec3f"; + String acceptValue = "application/json"; + String contentTypeValue = "application/json"; + + assertThat(HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue), is(msg)); + } catch (Exception e) { + assert (false); + } + } + + @Test + public void testPut() { + + try { + HttpResponseMessage msg = new HttpResponseMessage(); + msg.setStatusCode(200); + msg.setResponseBody(null); + PowerMockito.mockStatic(LowLevelRestApi.class); + PowerMockito.when(LowLevelRestApi.executeHttpMethod(Mockito.anyObject())).thenReturn(msg); + + String uri = "10.74.148.107/openoapi/catalog/v1/csars"; + String requestBody = "{'type':'NFAR'}"; + String acceptValue = "application/json"; + String contentTypeValue = "application/json"; + + assertThat(HighLevelRestApi.Put(uri, requestBody, acceptValue, contentTypeValue), is(msg)); + } catch (Exception e) { + assert (false); + } + } + + @SuppressWarnings("static-access") + @Test + public void testCreateNameValuePairArrayFromQuery() { + NameValuePair[] pair = new NameValuePair[2]; + NameValuePair p1 = new NameValuePair(); + p1.setName("name"); + p1.setValue("liuyao"); + NameValuePair p2 = new NameValuePair(); + p2.setName("pwd"); + p2.setValue("zte"); + + pair[0] = p1; + pair[1] = p2; + + String query = "name=liuyao&pwd=zte"; + + assertThat(highLevelRestApi.createNameValuePairArrayFromQuery(query), is(pair)); + } + + @Test + public void testSetAcceptHeader() { + try { + HttpMethodBase method = new GetMethod(); + HighLevelRestApi.setAcceptHeader(method, "[application/json]"); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + + @Test + public void testSetContentTypeHeader() { + + try { + HttpMethodBase method = new GetMethod(); + HighLevelRestApi.setContentTypeHeader(method, "[application/json]"); + Assert.assertTrue(true); + } catch (Exception e) { + Assert.assertTrue(false); + } + } + } -- cgit 1.2.3-korg