Add UT for query service tempalte. 73/16573/1
authorLuji7 <lu.ji3@zte.com.cn>
Fri, 29 Sep 2017 03:34:10 +0000 (11:34 +0800)
committerLuji7 <lu.ji3@zte.com.cn>
Fri, 29 Sep 2017 03:38:28 +0000 (11:38 +0800)
Change-Id: I1270eaa8fe2442cb2f8584a967a96b4d5bc6c9e3
Issue-Id: USECASEUI-36
Signed-off-by: Luji7 <lu.ji3@zte.com.cn>
server/pom.xml
server/src/main/java/org/onap/usecaseui/server/bean/lcm/VfNsPackageInfo.java
server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/aai/bean/VimInfo.java
server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/sdc/bean/SDCServiceTemplate.java
server/src/main/java/org/onap/usecaseui/server/service/lcm/domain/sdc/bean/Vnf.java
server/src/main/java/org/onap/usecaseui/server/service/lcm/impl/DefaultPackageDistributionService.java
server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultCustomerServiceTest.java [new file with mode: 0644]
server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultPackageDistributionServiceTest.java [new file with mode: 0644]
server/src/test/java/org/onap/usecaseui/server/util/CallStub.java [new file with mode: 0644]

index 5532eda..d0f308f 100644 (file)
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.mockito</groupId>
+                    <artifactId>mockito-core</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <dependency>
             <groupId>org.springframework.restdocs</groupId>
index d51ec4b..0e8ef96 100644 (file)
@@ -20,6 +20,7 @@ import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.Vnf;
 
 import java.util.List;
+import java.util.Objects;
 
 public class VfNsPackageInfo {
 
@@ -46,4 +47,19 @@ public class VfNsPackageInfo {
     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;
+        VfNsPackageInfo that = (VfNsPackageInfo) o;
+        return Objects.equals(nsPackage, that.nsPackage) &&
+                Objects.equals(vnfPackages, that.vnfPackages) &&
+                Objects.equals(vimInfos, that.vimInfos);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(nsPackage, vnfPackages, vimInfos);
+    }
 }
index 73a9873..336481f 100644 (file)
@@ -18,6 +18,8 @@ package org.onap.usecaseui.server.service.lcm.domain.aai.bean;
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 
+import java.util.Objects;
+
 public class VimInfo {
 
     private String cloudOwner;
@@ -41,4 +43,18 @@ public class VimInfo {
     public String getCloudRegionId() {
         return cloudRegionId;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        VimInfo vimInfo = (VimInfo) o;
+        return Objects.equals(cloudOwner, vimInfo.cloudOwner) &&
+                Objects.equals(cloudRegionId, vimInfo.cloudRegionId);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(cloudOwner, cloudRegionId);
+    }
 }
index 38afd55..9a686bc 100644 (file)
  */
 package org.onap.usecaseui.server.service.lcm.domain.sdc.bean;
 
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.Objects;
+
 public class SDCServiceTemplate {
 
     private String uuid;
@@ -27,7 +32,13 @@ public class SDCServiceTemplate {
 
     private String category;
 
-    public SDCServiceTemplate(String uuid, String invariantUUID, String name, String toscaModelURL, String category) {
+    @JsonCreator
+    public SDCServiceTemplate(
+            @JsonProperty String uuid,
+            @JsonProperty String invariantUUID,
+            @JsonProperty String name,
+            @JsonProperty String toscaModelURL,
+            @JsonProperty String category) {
         this.uuid = uuid;
         this.invariantUUID = invariantUUID;
         this.name = name;
@@ -54,4 +65,21 @@ public class SDCServiceTemplate {
     public String getCategory() {
         return category;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        SDCServiceTemplate that = (SDCServiceTemplate) o;
+        return Objects.equals(uuid, that.uuid) &&
+                Objects.equals(invariantUUID, that.invariantUUID) &&
+                Objects.equals(name, that.name) &&
+                Objects.equals(toscaModelURL, that.toscaModelURL) &&
+                Objects.equals(category, that.category);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(uuid, invariantUUID, name, toscaModelURL, category);
+    }
 }
index 8b98c71..27949d6 100644 (file)
  */
 package org.onap.usecaseui.server.service.lcm.domain.sdc.bean;
 
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.Objects;
+
 public class Vnf {
+
+    private String uuid;
+
+    private String invariantUUID;
+
+    private String name;
+
+    @JsonCreator
+    public Vnf(
+            @JsonProperty String uuid,
+            @JsonProperty String invariantUUID,
+            @JsonProperty String name) {
+        this.uuid = uuid;
+        this.invariantUUID = invariantUUID;
+        this.name = name;
+    }
+
+    public String getUuid() {
+        return uuid;
+    }
+
+    public String getInvariantUUID() {
+        return invariantUUID;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+        Vnf vnf = (Vnf) o;
+        return Objects.equals(uuid, vnf.uuid) &&
+                Objects.equals(invariantUUID, vnf.invariantUUID) &&
+                Objects.equals(name, vnf.name);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(uuid, invariantUUID, name);
+    }
 }
index 383ee29..e4a08b8 100644 (file)
@@ -63,9 +63,9 @@ public class DefaultPackageDistributionService implements PackageDistributionSer
     public VfNsPackageInfo retrievePackageInfo() {
         try {
             List<SDCServiceTemplate> nsTemplate = sdcCatalogService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED).execute().body();
-            List<Vnf> vnfs = sdcCatalogService.listResources(RESOURCETYPE_VF, DISTRIBUTION_STATUS_DISTRIBUTED).execute().body();
+            List<Vnf> vnf = sdcCatalogService.listResources(RESOURCETYPE_VF, DISTRIBUTION_STATUS_DISTRIBUTED).execute().body();
             List<VimInfo> vim = aaiService.listVimInfo().execute().body();
-            return new VfNsPackageInfo(nsTemplate, vnfs, vim);
+            return new VfNsPackageInfo(nsTemplate, vnf, vim);
         } catch (IOException e) {
             throw new SDCCatalogException("SDC Service is not available!", e);
         }
diff --git a/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultCustomerServiceTest.java b/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultCustomerServiceTest.java
new file mode 100644 (file)
index 0000000..af579af
--- /dev/null
@@ -0,0 +1,55 @@
+/**
+ * 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.mockito.Mockito;
+import org.onap.usecaseui.server.service.lcm.CustomerService;
+import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
+import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAICustomer;
+import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;
+import org.onap.usecaseui.server.util.CallStub;
+import retrofit2.Call;
+
+import java.util.List;
+
+import static java.util.Collections.singletonList;
+
+public class DefaultCustomerServiceTest {
+
+    @Test
+    public void itCanRetrieveCustomersFromAAI() {
+        List<AAICustomer> customers = singletonList(new AAICustomer("1", "name", "type"));
+
+        AAIService aaiService = Mockito.mock(AAIService.class);
+        Call<List<AAICustomer>> call = CallStub.successfulCall(customers);
+        Mockito.when(aaiService.listCustomer()).thenReturn(call);
+
+        CustomerService customerService = new DefaultCustomerService(aaiService);
+        Assert.assertSame(customers, customerService.listCustomer());
+    }
+
+    @Test(expected = AAIException.class)
+    public void itWillThrowExceptionWhenAAIIsNotAvailable() {
+        AAIService aaiService = Mockito.mock(AAIService.class);
+        Call<List<AAICustomer>> call = CallStub.failedCall("AAI is not available!");
+        Mockito.when(aaiService.listCustomer()).thenReturn(call);
+
+        CustomerService customerService = new DefaultCustomerService(aaiService);
+        customerService.listCustomer();
+    }
+}
\ No newline at end of file
diff --git a/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultPackageDistributionServiceTest.java b/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultPackageDistributionServiceTest.java
new file mode 100644 (file)
index 0000000..1943082
--- /dev/null
@@ -0,0 +1,109 @@
+/**
+ * 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.mockito.Mockito;
+import org.onap.usecaseui.server.bean.lcm.VfNsPackageInfo;
+import org.onap.usecaseui.server.service.lcm.PackageDistributionService;
+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.bean.Vnf;
+import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
+import org.onap.usecaseui.server.service.lcm.domain.vfc.VfcService;
+import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Csar;
+import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.DistributionResult;
+import org.onap.usecaseui.server.service.lcm.domain.vfc.exceptions.VfcException;
+import org.onap.usecaseui.server.util.CallStub;
+import retrofit2.Call;
+
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.*;
+
+public class DefaultPackageDistributionServiceTest {
+
+    @Test
+    public void itCanRetrievePackageFromSDCAndAAI() {
+        List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "", ""));
+        List<Vnf> vnf = Collections.singletonList(new Vnf("2","2","vnf"));
+        SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
+
+        List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
+        AAIService aaiService = newAAIService(vim);
+
+        PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
+
+        Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf, vim)));
+    }
+
+    private AAIService newAAIService(List<VimInfo> vim) {
+        AAIService aaiService = Mockito.mock(AAIService.class);
+
+        Call<List<VimInfo>> vimCall = CallStub.successfulCall(vim);
+        Mockito.when(aaiService.listVimInfo()).thenReturn(vimCall);
+        return aaiService;
+    }
+
+    private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
+        SDCCatalogService sdcService = Mockito.mock(SDCCatalogService.class);
+
+        Call<List<SDCServiceTemplate>> serviceCall = CallStub.successfulCall(serviceTemplate);
+        Mockito.when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
+
+        Call<List<Vnf>> vnfCall = CallStub.successfulCall(vnf);
+        Mockito.when(sdcService.listResources(RESOURCETYPE_VF, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(vnfCall);
+        return sdcService;
+    }
+
+    @Test(expected = SDCCatalogException.class)
+    public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
+        SDCCatalogService sdcService = Mockito.mock(SDCCatalogService.class);
+        Call<List<SDCServiceTemplate>> serviceCall = CallStub.failedCall("SDC is not available!");
+        Mockito.when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
+
+        List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
+        AAIService aaiService = newAAIService(vim);
+
+        PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
+        service.retrievePackageInfo();
+    }
+
+    @Test
+    public void itCanPostNsPackageToVFC() {
+        VfcService vfcService = Mockito.mock(VfcService.class);
+        Csar csar = new Csar();
+        DistributionResult result = new DistributionResult("status", "description", "errorcode");
+        Mockito.when(vfcService.distributeNsPackage(csar)).thenReturn(CallStub.successfulCall(result));
+        PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
+
+        Assert.assertSame(result, service.postNsPackage(csar));
+    }
+
+    @Test(expected = VfcException.class)
+    public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
+        VfcService vfcService = Mockito.mock(VfcService.class);
+        Csar csar = new Csar();
+        Mockito.when(vfcService.distributeNsPackage(csar)).thenReturn(CallStub.failedCall("VFC is not available!"));
+        PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
+        service.postNsPackage(csar);
+    }
+}
\ No newline at end of file
diff --git a/server/src/test/java/org/onap/usecaseui/server/util/CallStub.java b/server/src/test/java/org/onap/usecaseui/server/util/CallStub.java
new file mode 100644 (file)
index 0000000..1fe7638
--- /dev/null
@@ -0,0 +1,83 @@
+/**
+ * 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.util;
+
+import okhttp3.Request;
+import retrofit2.Call;
+import retrofit2.Callback;
+import retrofit2.Response;
+
+import java.io.IOException;
+
+public class CallStub<T> implements Call<T> {
+
+    private boolean isSuccess;
+    private T result;
+    private String failMessage;
+
+    private CallStub(boolean isSuccess, T result, String message) {
+        this.isSuccess = isSuccess;
+        this.result = result;
+        this.failMessage = message;
+    }
+
+    @Override
+    public Response<T> execute() throws IOException {
+        if (isSuccess) {
+            return Response.success(result);
+        } else {
+            throw new IOException(failMessage);
+        }
+    }
+
+    @Override
+    public void enqueue(Callback<T> callback) {
+
+    }
+
+    @Override
+    public boolean isExecuted() {
+        return false;
+    }
+
+    @Override
+    public void cancel() {
+
+    }
+
+    @Override
+    public boolean isCanceled() {
+        return false;
+    }
+
+    @Override
+    public Call<T> clone() {
+        return null;
+    }
+
+    @Override
+    public Request request() {
+        return null;
+    }
+
+    public static <T> CallStub<T> successfulCall(T result) {
+        return new CallStub<>(true, result, "");
+    }
+
+    public static <T> CallStub<T> failedCall(String message) {
+        return new CallStub<>(false, null, message);
+    }
+}