diff options
author | Luji7 <lu.ji3@zte.com.cn> | 2017-09-29 15:46:24 +0800 |
---|---|---|
committer | Luji7 <lu.ji3@zte.com.cn> | 2017-09-29 15:46:30 +0800 |
commit | 16f08c39a984fc0eff4060d7dcfe2a49a53afbb0 (patch) | |
tree | 94c45fb7e6e06ddb1d13e2c98d81e0fa1309af85 | |
parent | 202dd6c8995478127c816aed2365a7cbf26fb843 (diff) |
Add UT for extract service input.
Change-Id: Id846ce0ea3f6e694b57f08f49f5b273ac5e89ff5
Issue-Id: USECASEUI-36
Signed-off-by: Luji7 <lu.ji3@zte.com.cn>
5 files changed, 304 insertions, 3 deletions
diff --git a/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInput.java b/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInput.java index 9207f9a5..becc3238 100644 --- a/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInput.java +++ b/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInput.java @@ -15,7 +15,10 @@ */ package org.onap.usecaseui.server.bean.lcm; +import com.google.common.base.MoreObjects; + import java.util.List; +import java.util.Objects; public class ServiceTemplateInput { @@ -85,4 +88,38 @@ public class ServiceTemplateInput { public List<TemplateInput> getInputs() { return inputs; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ServiceTemplateInput that = (ServiceTemplateInput) o; + return Objects.equals(invariantUUID, that.invariantUUID) && + Objects.equals(uuid, that.uuid) && + Objects.equals(name, that.name) && + Objects.equals(type, that.type) && + Objects.equals(description, that.description) && + Objects.equals(category, that.category) && + Objects.equals(subcategory, that.subcategory) && + Objects.equals(inputs, that.inputs); + } + + @Override + public int hashCode() { + return Objects.hash(invariantUUID, uuid, name, type, description, category, subcategory, inputs); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("invariantUUID", invariantUUID) + .add("uuid", uuid) + .add("name", name) + .add("type", type) + .add("description", description) + .add("category", category) + .add("subcategory", subcategory) + .add("inputs", inputs) + .toString(); + } } diff --git a/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInputRsp.java b/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInputRsp.java index 403b991e..4a707d0c 100644 --- a/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInputRsp.java +++ b/server/src/main/java/org/onap/usecaseui/server/bean/lcm/ServiceTemplateInputRsp.java @@ -15,9 +15,11 @@ */ package org.onap.usecaseui.server.bean.lcm; +import com.google.common.base.MoreObjects; import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo; import java.util.List; +import java.util.Objects; public class ServiceTemplateInputRsp { @@ -37,4 +39,26 @@ public class ServiceTemplateInputRsp { public List<VimInfo> getVimInfos() { return vimInfos; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ServiceTemplateInputRsp that = (ServiceTemplateInputRsp) o; + return Objects.equals(serviceTemplateInput, that.serviceTemplateInput) && + Objects.equals(vimInfos, that.vimInfos); + } + + @Override + public int hashCode() { + return Objects.hash(serviceTemplateInput, vimInfos); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("serviceTemplateInput", serviceTemplateInput) + .add("vimInfos", vimInfos) + .toString(); + } } diff --git a/server/src/main/java/org/onap/usecaseui/server/bean/lcm/TemplateInput.java b/server/src/main/java/org/onap/usecaseui/server/bean/lcm/TemplateInput.java index 6367d58f..6359f0db 100644 --- a/server/src/main/java/org/onap/usecaseui/server/bean/lcm/TemplateInput.java +++ b/server/src/main/java/org/onap/usecaseui/server/bean/lcm/TemplateInput.java @@ -15,6 +15,10 @@ */ package org.onap.usecaseui.server.bean.lcm; +import com.google.common.base.MoreObjects; + +import java.util.Objects; + public class TemplateInput { private String name; @@ -54,4 +58,32 @@ public class TemplateInput { public String getDefaultValue() { return defaultValue; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TemplateInput that = (TemplateInput) o; + return Objects.equals(name, that.name) && + Objects.equals(type, that.type) && + Objects.equals(description, that.description) && + Objects.equals(isRequired, that.isRequired) && + Objects.equals(defaultValue, that.defaultValue); + } + + @Override + public int hashCode() { + return Objects.hash(name, type, description, isRequired, defaultValue); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("name", name) + .add("type", type) + .add("description", description) + .add("isRequired", isRequired) + .add("defaultValue", defaultValue) + .toString(); + } } diff --git a/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateService.java b/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateService.java index 588c0b2f..816889c2 100644 --- a/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateService.java +++ b/server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateService.java @@ -70,7 +70,6 @@ public class DefaultServiceTemplateService implements ServiceTemplateService { return this.sdcCatalog.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED).execute().body(); } catch (IOException e) { logger.error("Visit SDC Catalog occur exception"); - logger.info("SDC Catalog Exception: ", e); throw new SDCCatalogException("SDC Catalog is not available.", e); } } @@ -94,7 +93,7 @@ public class DefaultServiceTemplateService implements ServiceTemplateService { } } - private void downloadFile(String templateUrl, String toPath) throws IOException { + protected void downloadFile(String templateUrl, String toPath) throws IOException { try { ResponseBody body = sdcCatalog.downloadCsar(templateUrl).execute().body(); Files.write(body.bytes(),new File(toPath)); @@ -105,7 +104,7 @@ public class DefaultServiceTemplateService implements ServiceTemplateService { } private List<ServiceTemplateInput> extractInputs(String toPath, List<ServiceTemplateInput> serviceTemplateInputs) throws JToscaException, IOException { - ToscaTemplate tosca = new ToscaTemplate(toPath,null,true,null,true); + ToscaTemplate tosca = translateToToscaTemplate(toPath); ServiceTemplateInput serviceTemplateInput = fetchServiceTemplateInput(tosca); serviceTemplateInputs.add(serviceTemplateInput); for (NodeTemplate nodeTemplate : tosca.getNodeTemplates()) { @@ -122,6 +121,10 @@ public class DefaultServiceTemplateService implements ServiceTemplateService { return serviceTemplateInputs; } + protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException { + return new ToscaTemplate(toPath,null,true,null,true); + } + private static ServiceTemplateInput fetchServiceTemplateInput(ToscaTemplate tosca) { String invariantUUID = tosca.getMetaData().getValue("invariantUUID"); String uuid = tosca.getMetaData().getValue("UUID"); diff --git a/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateServiceTest.java b/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateServiceTest.java new file mode 100644 index 00000000..260c2379 --- /dev/null +++ b/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultServiceTemplateServiceTest.java @@ -0,0 +1,205 @@ +/** + * 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.impl; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.usecaseui.server.bean.lcm.ServiceTemplateInput; +import org.onap.usecaseui.server.bean.lcm.ServiceTemplateInputRsp; +import org.onap.usecaseui.server.bean.lcm.TemplateInput; +import org.onap.usecaseui.server.service.lcm.ServiceTemplateService; +import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService; +import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo; +import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService; +import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate; +import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException; +import org.openecomp.sdc.toscaparser.api.NodeTemplate; +import org.openecomp.sdc.toscaparser.api.ToscaTemplate; +import org.openecomp.sdc.toscaparser.api.common.JToscaException; +import org.openecomp.sdc.toscaparser.api.elements.Metadata; +import org.openecomp.sdc.toscaparser.api.parameters.Input; +import retrofit2.Call; + +import java.io.IOException; +import java.util.*; + +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE; +import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED; +import static org.onap.usecaseui.server.util.CallStub.failedCall; +import static org.onap.usecaseui.server.util.CallStub.successfulCall; + +public class DefaultServiceTemplateServiceTest { + + @Test + public void itCanListDistributedServiceTemplate() { + List<SDCServiceTemplate> templates = Collections.singletonList(new SDCServiceTemplate("uuid", "uuid", "name", "url", "category")); + SDCCatalogService sdcService = mock(SDCCatalogService.class); + when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(successfulCall(templates)); + + ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null); + + Assert.assertSame(templates, service.listDistributedServiceTemplate()); + } + + @Test(expected = SDCCatalogException.class) + public void retrieveServiceWillThrowExceptionWhenSDCIsNotAvailable() { + SDCCatalogService sdcService = mock(SDCCatalogService.class); + when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(failedCall("SDC is not available!")); + + ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null); + service.listDistributedServiceTemplate(); + } + + @Test + public void itCanRetrieveInputsFromServiceTemplate() throws IOException { + final String uuid = "1"; + String modelPath = "model_path"; + String nodeUUID = "2"; + + SDCCatalogService sdcService = newSdcCatalogService(nodeUUID); + + List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId")); + AAIService aaiService = newAAIService(vim); + + ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService); + + Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(new ServiceTemplateInputRsp(expectedServiceInputs(uuid, nodeUUID),vim))); + } + + private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) { + return new DefaultServiceTemplateService(sdcService, aaiService) { + + @Override + protected void downloadFile(String templateUrl, String toPath) throws IOException { + // download successfully... + } + + @Override + protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException { + if (toPath.contains(uuid)) { + return e2eToscaTemplate(nodeUUID); + } + return nodeToscaTemplate(nodeUUID); + } + }; + } + + private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException { + SDCCatalogService sdcService = mock(SDCCatalogService.class); + when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "nodeModelUrl", "service"))); + return sdcService; + } + + private List<ServiceTemplateInput> expectedServiceInputs(String uuid, String nodeUUID) { + ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput( + uuid, uuid, "VoLTE", "service", "VoLTE", "service", "", Collections.EMPTY_LIST); + TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default"); + ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput( + nodeUUID, nodeUUID, "", "", "", "service", "", Collections.singletonList(templateInput)); + return Arrays.asList(e2eServiceTemplateInput, nodeTemplateInput); + } + + private ToscaTemplate e2eToscaTemplate(String nodeUUID) { + ToscaTemplate toscaTemplate = mock(ToscaTemplate.class); + Map<String, Object> e2eAttributes = new HashMap<>(); + e2eAttributes.put("invariantUUID", "1"); + e2eAttributes.put("UUID", "1"); + e2eAttributes.put("name", "VoLTE"); + e2eAttributes.put("type", "service"); + e2eAttributes.put("description", "VoLTE"); + e2eAttributes.put("category", "service"); + e2eAttributes.put("subcategory", ""); + when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes)); + when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>()); + NodeTemplate nodeTemplate = mock(NodeTemplate.class); + + Map<String, Object> nodeUUIDAttr = new HashMap<>(); + + nodeUUIDAttr.put("UUID", nodeUUID); + when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr)); + + ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>(); + nodeTemplates.add(nodeTemplate); + when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates); + + return toscaTemplate; + } + + private ToscaTemplate nodeToscaTemplate(String nodeUUID) { + ToscaTemplate toscaTemplate = mock(ToscaTemplate.class); + Map<String, Object> Attributes = new HashMap<>(); + Attributes.put("invariantUUID", nodeUUID); + Attributes.put("UUID", nodeUUID); + Attributes.put("name", ""); + Attributes.put("type", ""); + Attributes.put("description", ""); + Attributes.put("category", "service"); + Attributes.put("subcategory", ""); + when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes)); + + Input input = mock(Input.class); + when(input.getName()).thenReturn("field_name"); + when(input.getDescription()).thenReturn("field_description"); + when(input.getType()).thenReturn("field_type"); + when(input.getDefault()).thenReturn("field_default"); + when(input.isRequired()).thenReturn(true); + + ArrayList<Input> inputs = new ArrayList<>(); + inputs.add(input); + when(toscaTemplate.getInputs()).thenReturn(inputs); + when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>()); + + return toscaTemplate; + } + + private AAIService newAAIService(List<VimInfo> vim) { + AAIService aaiService = mock(AAIService.class); + + Call<List<VimInfo>> vimCall = successfulCall(vim); + when(aaiService.listVimInfo()).thenReturn(vimCall); + return aaiService; + } + + @Test(expected = SDCCatalogException.class) + public void retrieveInputsWillThrowExceptionWhenDownloadFailed() { + ServiceTemplateService service = new DefaultServiceTemplateService() { + @Override + protected void downloadFile(String templateUrl, String toPath) throws IOException { + throw new IOException("download failed!"); + } + }; + service.fetchServiceTemplateInput("1", "url"); + } + + @Test(expected = SDCCatalogException.class) + public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() { + ServiceTemplateService service = new DefaultServiceTemplateService() { + @Override + protected void downloadFile(String templateUrl, String toPath) throws IOException { + // download successfully... + } + + @Override + protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException { + throw new JToscaException("parse tosca template failed!", "123"); + } + }; + service.fetchServiceTemplateInput("1", "url"); + } +}
\ No newline at end of file |