diff options
author | Luji7 <lu.ji3@zte.com.cn> | 2017-10-13 09:47:07 +0800 |
---|---|---|
committer | Luji7 <lu.ji3@zte.com.cn> | 2017-10-13 09:47:14 +0800 |
commit | d4a65cb4eb1377f4b376f5d2f3636e5bb7cf057c (patch) | |
tree | 7bd19591665134adc855c1b842d7cad9053ef6a0 | |
parent | eea68ef0cf77f6ce36f1c0244cec6aa2ff8e3296 (diff) |
create service request need pass through server.
Change-Id: Ie4b87c0afe7e8d1b27cffb45e62fa6aeb8f9697a
Issue-id: USECASEUI-36
Signed-off-by: Luji7 <lu.ji3@zte.com.cn>
8 files changed, 85 insertions, 97 deletions
diff --git a/server/src/main/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmController.java b/server/src/main/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmController.java index 13ef5d55..846196b8 100644 --- a/server/src/main/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmController.java +++ b/server/src/main/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmController.java @@ -17,15 +17,18 @@ package org.onap.usecaseui.server.controller.lcm; import org.onap.usecaseui.server.service.lcm.ServiceLcmService; import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation; -import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceInstantiationRequest; import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; @Controller @org.springframework.context.annotation.Configuration @@ -43,7 +46,7 @@ public class ServiceLcmController { @ResponseBody @RequestMapping(value = {"/onapapi/uui-lcm/v1/services/"}, method = RequestMethod.POST , produces = "application/json") - public ServiceOperation instantiateService(@RequestBody ServiceInstantiationRequest request){ + public ServiceOperation instantiateService(HttpServletRequest request){ return serviceLcmService.instantiateService(request); } diff --git a/server/src/main/java/org/onap/usecaseui/server/service/lcm/ServiceLcmService.java b/server/src/main/java/org/onap/usecaseui/server/service/lcm/ServiceLcmService.java index fc893f3b..6ca22bea 100644 --- a/server/src/main/java/org/onap/usecaseui/server/service/lcm/ServiceLcmService.java +++ b/server/src/main/java/org/onap/usecaseui/server/service/lcm/ServiceLcmService.java @@ -16,12 +16,13 @@ package org.onap.usecaseui.server.service.lcm; import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation; -import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceInstantiationRequest; import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceOperation; +import javax.servlet.http.HttpServletRequest; + public interface ServiceLcmService { - ServiceOperation instantiateService(ServiceInstantiationRequest request); + ServiceOperation instantiateService(HttpServletRequest request); OperationProgressInformation queryOperationProgress(String serviceId, String operationId); diff --git a/server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/so/SOService.java b/server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/so/SOService.java index 4d8fac98..14acc027 100644 --- a/server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/so/SOService.java +++ b/server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/so/SOService.java @@ -15,19 +15,16 @@ */ package org.onap.usecaseui.server.service.lcm.domain.so; +import okhttp3.RequestBody; import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation; -import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceInstantiationRequest; import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceOperation; import retrofit2.Call; -import retrofit2.http.DELETE; -import retrofit2.http.GET; -import retrofit2.http.POST; -import retrofit2.http.Path; +import retrofit2.http.*; public interface SOService { @POST("/so/e2eServiceInstances/v2") - Call<ServiceOperation> instantiateService(ServiceInstantiationRequest request); + Call<ServiceOperation> instantiateService(@Body RequestBody body); @GET("/so/e2eServiceInstances/v2/{serviceId}/operations/{operationId}") Call<OperationProgressInformation> queryOperationProgress(@Path("serviceId") String serviceId, @Path("operationId") String operationId); diff --git a/server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/so/bean/ServiceInstantiationRequest.java b/server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/so/bean/ServiceInstantiationRequest.java deleted file mode 100644 index 98984093..00000000 --- a/server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/so/bean/ServiceInstantiationRequest.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright 2016-2017 ZTE Corporation. - * - * 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.usecaseui.server.service.lcm.domain.so.bean; - -import java.util.Map; - -public class ServiceInstantiationRequest { - - private String name; - - private String description; - - private String serviceDefId; - - private String templateId; - - private Map<String, String> parameters; - - public ServiceInstantiationRequest(String name, String description, String serviceDefId, String templateId, Map<String, String> parameters) { - this.name = name; - this.description = description; - this.serviceDefId = serviceDefId; - this.templateId = templateId; - this.parameters = parameters; - } - - public String getName() { - return name; - } - - public String getDescription() { - return description; - } - - public String getServiceDefId() { - return serviceDefId; - } - - public String getTemplateId() { - return templateId; - } - - public Map<String, String> getParameters() { - return parameters; - } -} diff --git a/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmService.java b/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmService.java index 7bb3777a..760eed14 100644 --- a/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmService.java +++ b/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmService.java @@ -15,18 +15,21 @@ */ package org.onap.usecaseui.server.service.lcm.impl; +import okhttp3.RequestBody; import org.onap.usecaseui.server.service.lcm.ServiceLcmService; import org.onap.usecaseui.server.service.lcm.domain.so.SOService; import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation; -import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceInstantiationRequest; import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceOperation; import org.onap.usecaseui.server.service.lcm.domain.so.exceptions.SOException; -import org.onap.usecaseui.server.util.RestfulServices; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Service; +import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import static org.onap.usecaseui.server.util.RestfulServices.create; +import static org.onap.usecaseui.server.util.RestfulServices.extractBody; + @Service("ServiceLcmService") @org.springframework.context.annotation.Configuration @EnableAspectJAutoProxy @@ -35,7 +38,7 @@ public class DefaultServiceLcmService implements ServiceLcmService { private SOService soService; public DefaultServiceLcmService() { - this(RestfulServices.create(SOService.class)); + this(create(SOService.class)); } public DefaultServiceLcmService(SOService soService) { @@ -43,9 +46,10 @@ public class DefaultServiceLcmService implements ServiceLcmService { } @Override - public ServiceOperation instantiateService(ServiceInstantiationRequest request) { + public ServiceOperation instantiateService(HttpServletRequest request) { try { - return soService.instantiateService(request).execute().body(); + RequestBody requestBody = extractBody(request); + return soService.instantiateService(requestBody).execute().body(); } catch (IOException e) { throw new SOException("SO Service is not available!", e); } diff --git a/server/src/main/java/org/onap/usecaseui/server/util/RestfulServices.java b/server/src/main/java/org/onap/usecaseui/server/util/RestfulServices.java index c26aab69..66c01de1 100644 --- a/server/src/main/java/org/onap/usecaseui/server/util/RestfulServices.java +++ b/server/src/main/java/org/onap/usecaseui/server/util/RestfulServices.java @@ -15,9 +15,15 @@ */ package org.onap.usecaseui.server.util; +import okhttp3.RequestBody; +import okhttp3.MediaType; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + public class RestfulServices { public static <T> T create(Class<T> clazz) { @@ -27,4 +33,19 @@ public class RestfulServices { .build(); return retrofit.create(clazz); } + + public static RequestBody extractBody(HttpServletRequest request) throws IOException { + int len = request.getContentLength(); + ServletInputStream inStream = null; + try { + inStream = request.getInputStream(); + byte[] buffer = new byte[len]; + inStream.read(buffer, 0, len); + return RequestBody.create(MediaType.parse("application/json"), buffer); + }finally { + if (inStream != null) { + inStream.close(); + } + } + } } diff --git a/server/src/test/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmControllerTest.java b/server/src/test/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmControllerTest.java index 292d07c0..67182ece 100644 --- a/server/src/test/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmControllerTest.java +++ b/server/src/test/java/org/onap/usecaseui/server/controller/lcm/ServiceLcmControllerTest.java @@ -18,7 +18,8 @@ package org.onap.usecaseui.server.controller.lcm; import org.junit.Before; import org.junit.Test; import org.onap.usecaseui.server.service.lcm.ServiceLcmService; -import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceInstantiationRequest; + +import javax.servlet.http.HttpServletRequest; import static org.mockito.Mockito.*; @@ -36,7 +37,7 @@ public class ServiceLcmControllerTest { @Test public void testInstantiateService() throws Exception { - ServiceInstantiationRequest request = mock(ServiceInstantiationRequest.class); + HttpServletRequest request = mock(HttpServletRequest.class); controller.instantiateService(request); verify(service, times(1)).instantiateService(request); diff --git a/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmServiceTest.java b/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmServiceTest.java index 607a9148..e086dd4c 100644 --- a/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmServiceTest.java +++ b/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceLcmServiceTest.java @@ -20,12 +20,15 @@ import org.junit.Test; import org.onap.usecaseui.server.service.lcm.ServiceLcmService; import org.onap.usecaseui.server.service.lcm.domain.so.SOService; import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation; -import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceInstantiationRequest; import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceOperation; import org.onap.usecaseui.server.service.lcm.domain.so.exceptions.SOException; -import java.util.HashMap; +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.onap.usecaseui.server.util.CallStub.failedCall; @@ -34,34 +37,51 @@ import static org.onap.usecaseui.server.util.CallStub.successfulCall; public class DefaultServiceLcmServiceTest { @Test - public void itCanInstantiateService() { + public void itCanInstantiateService() throws IOException { SOService soService = mock(SOService.class); - ServiceInstantiationRequest request = new ServiceInstantiationRequest( - "name", - "description", - "123", - "123", - new HashMap<>() - ); ServiceOperation operation = new ServiceOperation("1", "1"); - when(soService.instantiateService(request)).thenReturn(successfulCall(operation)); + when(soService.instantiateService(anyObject())).thenReturn(successfulCall(operation)); + + HttpServletRequest request = mockRequest(); ServiceLcmService service = new DefaultServiceLcmService(soService); Assert.assertSame(operation, service.instantiateService(request)); } + private HttpServletRequest mockRequest() throws IOException { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getContentLength()).thenReturn(0); + ServletInputStream inStream = new ServletInputStream() { + @Override + public boolean isFinished() { + return false; + } + + @Override + public boolean isReady() { + return false; + } + + @Override + public void setReadListener(ReadListener readListener) { + + } + + @Override + public int read() throws IOException { + return 0; + } + }; + when(request.getInputStream()).thenReturn(inStream); + return request; + } + @Test(expected = SOException.class) - public void instantiateServiceWillThrowExceptionWhenSOIsNotAvailable() { + public void instantiateServiceWillThrowExceptionWhenSOIsNotAvailable() throws IOException { SOService soService = mock(SOService.class); - ServiceInstantiationRequest request = new ServiceInstantiationRequest( - "name", - "description", - "123", - "123", - new HashMap<>() - ); - when(soService.instantiateService(request)).thenReturn(failedCall("SO is not available!")); + when(soService.instantiateService(anyObject())).thenReturn(failedCall("SO is not available!")); + HttpServletRequest request = mockRequest(); ServiceLcmService service = new DefaultServiceLcmService(soService); |