summaryrefslogtreecommitdiffstats
path: root/mod2/catalog-service/src/test/java
diff options
context:
space:
mode:
authorRavi Mantena <rx908f@att.com>2020-11-27 13:34:11 -0500
committerRavi Mantena <rx908f@att.com>2020-12-08 14:13:43 -0500
commite3578fcdcd65c9f919c0f7cc16949b79b70d12b4 (patch)
treee85f8b4048dfc3e7118216894ee36c255cbd6211 /mod2/catalog-service/src/test/java
parent437e1f4e621d63cf576caa5de9c5c4804ea455f9 (diff)
Policy Model Distribution POC Issue-ID: DCAEGEN2-1868
Issue-ID: DCAEGEN2-1868 Change-Id: Ife6421558af59405f3026f66950ff8f2d7aeba17 Signed-off-by: Ravi Mantena <rx908f@att.com>
Diffstat (limited to 'mod2/catalog-service/src/test/java')
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelDistributionObjectMother.java35
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelObjectMother.java57
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelControllerTest.java110
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelCreateRequestValidationTest.java95
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelDistributionControllerTest.java120
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelUpdateRequestValidationTest.java83
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelDistributionServiceImplTest.java329
-rw-r--r--mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelServiceImplTest.java152
8 files changed, 981 insertions, 0 deletions
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelDistributionObjectMother.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelDistributionObjectMother.java
new file mode 100644
index 0000000..dfd1bd7
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelDistributionObjectMother.java
@@ -0,0 +1,35 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.objectmothers;
+
+import org.onap.dcaegen2.platform.mod.util.TestUtil;
+import org.springframework.http.ResponseEntity;
+
+import static org.mockito.ArgumentMatchers.anyString;
+
+public class PolicyModelDistributionObjectMother {
+ public static final String PM_DISTRIBUTION_ENV = "DEV";
+ public static final String PM_DISTRIBUTION_ENV_ERROR = "DEV1";
+ public static final String PM_DISTRIBUTION_MODEL_ID = "5fbfc375f9f1d44e8b0e0ec2";
+ public static final String PM_DISTRIBUTION_MODEL_NAME = "onap-policies-naming";
+ public static final String PM_DISTRIBUTION_MODEL_ID_ERROR = "5fb896389387ea1087bdb1aa";
+
+}
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelObjectMother.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelObjectMother.java
new file mode 100644
index 0000000..a868e82
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/objectmothers/PolicyModelObjectMother.java
@@ -0,0 +1,57 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.objectmothers;
+
+import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest;
+import org.onap.dcaegen2.platform.mod.util.TestUtil;
+
+/**
+ * Object Mother for Policy Model to generate request for Create, Update and Get Policy Model Response
+ */
+public class PolicyModelObjectMother {
+ public static final String PM_CREATE_REQUEST = "src/test/resources/http/requests/policy-model_create_request.json";
+ public static final String PM_UPDATE_REQUEST = "src/test/resources/http/requests/policy-model_update_request.json";
+ public static final String PM_RESPONSE = "src/test/resources/http/responses/policy-model_create_response.json";
+ public static final String POLICY_MODEL_ID = "5fb896389387ea1087bdb1aa"; //"5fae8956cd1bac74e55c9d3a";
+
+ /**
+ * Object Mother for Policy Model to generate request for Create
+ */
+ public static PolicyModelCreateRequest getPolicyModelCreateRequest() {
+ return TestUtil.deserializeJsonFileToModel(PM_CREATE_REQUEST, PolicyModelCreateRequest.class);
+ }
+
+ /**
+ * Object Mother for Policy Model to generate request for Get Policy Model Response
+ */
+ public static PolicyModel getPolicyModelResponse() {
+ return TestUtil.deserializeJsonFileToModel(PM_RESPONSE, PolicyModel.class);
+ }
+
+ /**
+ * Object Mother for Policy Model to generate request for Update
+ */
+ public static PolicyModelUpdateRequest getPolicyModelUpdateRequest() {
+ return TestUtil.deserializeJsonFileToModel(PM_UPDATE_REQUEST, PolicyModelUpdateRequest.class);
+ }
+}
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelControllerTest.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelControllerTest.java
new file mode 100644
index 0000000..e69ba22
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelControllerTest.java
@@ -0,0 +1,110 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.web;
+
+import org.hamcrest.Matchers;
+import org.junit.Ignore;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest;
+import org.onap.dcaegen2.platform.mod.web.controller.PolicyModelController;
+import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.MediaType;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.web.servlet.MockMvc;
+
+import java.util.Arrays;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.USER;
+import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.asJsonString;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.POLICY_MODEL_ID;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelCreateRequest;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelResponse;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelUpdateRequest;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+@ExtendWith(SpringExtension.class)
+@WebMvcTest(PolicyModelController.class)
+public class PolicyModelControllerTest {
+
+ @Autowired
+ MockMvc mockMvc;
+
+ @MockBean
+ private PolicyModelService mockPolicyModelService;
+
+ @BeforeEach
+ void setup() {
+ }
+
+ @Test
+ void test_getAllPolicyModels() throws Exception {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(mockPolicyModelService.getAll()).thenReturn(Arrays.asList(policyModel));
+
+ mockMvc.perform(get("/api/policy-model")
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$", Matchers.hasSize(1)));
+ }
+
+ @Test
+ void test_getPolicyModelById() throws Exception {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(mockPolicyModelService.getPolicyModelById(POLICY_MODEL_ID)).thenReturn(policyModel);
+
+ mockMvc.perform(get("/api/policy-model/" + POLICY_MODEL_ID)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.id", notNullValue()));
+ }
+
+ @Test
+ void test_patchPolicyModel_shouldReturn204AndResponseBody() throws Exception{
+ PolicyModelUpdateRequest policyModelpdateRequest = getPolicyModelUpdateRequest();
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(mockPolicyModelService.updatePolicyModel(policyModelpdateRequest,POLICY_MODEL_ID, USER)).thenReturn(policyModel);
+
+ mockMvc.perform(patch("/api/policy-model/" + POLICY_MODEL_ID).param("user", USER)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(asJsonString(policyModelpdateRequest)).accept(MediaType.APPLICATION_JSON))
+ .andExpect(status().isBadRequest());
+ verify(mockPolicyModelService, times(0)).updatePolicyModel(policyModelpdateRequest,POLICY_MODEL_ID, USER);
+ }
+
+} \ No newline at end of file
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelCreateRequestValidationTest.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelCreateRequestValidationTest.java
new file mode 100644
index 0000000..4b34e02
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelCreateRequestValidationTest.java
@@ -0,0 +1,95 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.web;
+
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest;
+import org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import java.util.Set;
+
+public class PolicyModelCreateRequestValidationTest {
+
+ public Validator validator;
+ private PolicyModelCreateRequest request;
+
+ @BeforeEach
+ public void setup(){
+ validator = Validation.buildDefaultValidatorFactory().getValidator();
+ request = PolicyModelObjectMother.getPolicyModelCreateRequest();
+ }
+
+ @Test
+ void test_pmNameShouldNotBeBlank(){
+ request.setName("");
+ Set<ConstraintViolation<PolicyModelCreateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(3);
+ }
+
+ @Test
+ void test_pmNameShouldFollowRegex() throws Exception{
+ request.setName("pm-1");
+ Set<ConstraintViolation<PolicyModelCreateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(1);
+ }
+
+ @Test
+ void test_pmNameSizeValidation() throws Exception {
+ request.setName("core-name-should-not-exceed-fifty-chars-core-name-should-not-exceed-fifty-chars");
+ Set<ConstraintViolation<PolicyModelCreateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(1);
+ }
+
+
+ @Test
+ void test_pmVersionShouldNotBeNull(){
+ request.setVersion("");
+ Set<ConstraintViolation<PolicyModelCreateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(3);
+
+ }
+
+ @Test
+ void test_pmVersionShouldFollowRegex() throws Exception{
+ request.setContent("1.1.1");
+ Set<ConstraintViolation<PolicyModelCreateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(1);
+ }
+
+ @Test
+ void test_pmContentShouldNotBeBlank(){
+ request.setContent("");
+ Set<ConstraintViolation<PolicyModelCreateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(2);
+ }
+
+ @Test
+ void test_pmOwnerShouldNotBeBlank(){
+ request.setOwner("");
+ Set<ConstraintViolation<PolicyModelCreateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(2);
+ }
+}
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelDistributionControllerTest.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelDistributionControllerTest.java
new file mode 100644
index 0000000..216d4c8
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelDistributionControllerTest.java
@@ -0,0 +1,120 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.web;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
+import org.onap.dcaegen2.platform.mod.web.controller.PolicyModelDistributionController;
+import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelDistributionService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.test.web.servlet.MockMvc;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother.USER;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_ENV;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_ID;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_NAME;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelResponse;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+@ExtendWith(SpringExtension.class)
+@WebMvcTest(PolicyModelDistributionController.class)
+public class PolicyModelDistributionControllerTest {
+
+ @Autowired
+ MockMvc mockMvc;
+
+ @MockBean
+ private PolicyModelDistributionService mockPolicyModelDistributionService;
+
+ @BeforeEach
+ void setup() {
+ }
+
+ @Test
+ void test_getPolicyModelById() throws Exception {
+ PolicyModel policyModel = getPolicyModelResponse();
+ ResponseEntity mockResponseEntity = ResponseEntity.status(200).body(policyModel.getContent());
+
+ when(mockPolicyModelDistributionService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
+
+ mockMvc.perform(get("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
+ .param("env",PM_DISTRIBUTION_ENV).contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk());
+
+ verify(mockPolicyModelDistributionService, times(1)).getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+ }
+
+ @Test
+ void test_getPolicyModelById_BadRequest() throws Exception {
+ PolicyModel policyModel = getPolicyModelResponse();
+ ResponseEntity mockResponseEntity = ResponseEntity.status(400).body(policyModel.getContent());
+
+ when(mockPolicyModelDistributionService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
+
+ mockMvc.perform(get("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().is4xxClientError());
+
+ verify(mockPolicyModelDistributionService, times(0)).getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+ }
+
+ @Test
+ void test_distributePolicyModelById_shouldReturn201AndResponseBody() throws Exception {
+ PolicyModel policyModel = getPolicyModelResponse();
+ ResponseEntity mockResponseEntity = ResponseEntity.status(200).body(policyModel.getContent());
+
+ when(mockPolicyModelDistributionService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
+
+ mockMvc.perform(post("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
+ .param("env",PM_DISTRIBUTION_ENV).contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().isOk());
+ verify(mockPolicyModelDistributionService, times(1)).distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+ }
+
+ @Test
+ void test_distributePolicyModelById_BadRequest() throws Exception {
+ PolicyModel policyModel = getPolicyModelResponse();
+ ResponseEntity mockResponseEntity = ResponseEntity.status(400).body(policyModel.getContent());
+
+ when(mockPolicyModelDistributionService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID)).thenReturn(mockResponseEntity);
+
+ mockMvc.perform(post("/api/policy-type/" + PM_DISTRIBUTION_MODEL_ID)
+ .contentType(MediaType.APPLICATION_JSON))
+ .andExpect(status().is4xxClientError());
+ verify(mockPolicyModelDistributionService, times(0)).distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+ }
+
+
+} \ No newline at end of file
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelUpdateRequestValidationTest.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelUpdateRequestValidationTest.java
new file mode 100644
index 0000000..f1945fd
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/PolicyModelUpdateRequestValidationTest.java
@@ -0,0 +1,83 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.web;
+
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest;
+import org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import java.util.Set;
+
+public class PolicyModelUpdateRequestValidationTest {
+
+ public Validator validator;
+ private PolicyModelUpdateRequest request;
+
+ @BeforeEach
+ public void setup(){
+ validator = Validation.buildDefaultValidatorFactory().getValidator();
+ request = PolicyModelObjectMother.getPolicyModelUpdateRequest();
+ }
+
+ @Test
+ void test_pmNameShouldNotBeBlank(){
+ request.setName("");
+ Set<ConstraintViolation<PolicyModelUpdateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(2);
+ }
+
+ @Test
+ void test_pmNameShouldFollowRegex() throws Exception{
+ request.setName("pm-1");
+ Set<ConstraintViolation<PolicyModelUpdateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(1);
+ }
+
+ @Test
+ void test_pmNameSizeValidation() throws Exception {
+ request.setName("core-name-should-not-exceed-fifty-chars-core-name-should-not-exceed-fifty-chars");
+ Set<ConstraintViolation<PolicyModelUpdateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(1);
+ }
+
+
+ @Test
+ void test_pmVersionShouldNotBeNull(){
+ request.setVersion("1.1.1");
+ Set<ConstraintViolation<PolicyModelUpdateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(1);
+
+ }
+
+ @Test
+ void test_pmVersionShouldFollowRegex() throws Exception{
+ request.setContent("1.1.1");
+ Set<ConstraintViolation<PolicyModelUpdateRequest>> violations = validator.validate(request);
+ Assertions.assertThat(violations.size()).isEqualTo(1);
+ }
+
+}
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelDistributionServiceImplTest.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelDistributionServiceImplTest.java
new file mode 100644
index 0000000..a1451c4
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelDistributionServiceImplTest.java
@@ -0,0 +1,329 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.web.service;
+
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
+import org.onap.dcaegen2.platform.mod.util.PolicyModelUtils;
+import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelDistributionServiceImpl;
+import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelGateway;
+import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelServiceImpl;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.reactive.function.client.WebClient;
+
+import java.io.IOException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_ENV;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_ID;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelDistributionObjectMother.PM_DISTRIBUTION_MODEL_ID_ERROR;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelResponse;
+
+@ExtendWith(MockitoExtension.class)
+class PolicyModelDistributionServiceImplTest {
+
+ @Spy
+ private PolicyModelDistributionServiceImpl pmDistributionImplService = new PolicyModelDistributionServiceImpl();
+
+ @Mock
+ private PolicyModelServiceImpl policyModelServiceImpl;
+
+ @Mock
+ private PolicyModelGateway policyModelGateway;
+
+ @Mock
+ private PolicyModelUtils policyModelUtils;
+
+ @BeforeEach
+ void initialize(){
+ pmDistributionImplService.setPolicyModelServiceImpl(policyModelServiceImpl);
+ pmDistributionImplService.setPolicyModelGateway(policyModelGateway);
+ pmDistributionImplService.setPolicyModelUtils(policyModelUtils);
+ }
+
+ @Test
+ void test_getPolicyModelDistributionByIdReturnSucess() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ // Calling the same method twice to resolve the issue of mock server hanging for response
+ initResponse(mockBackEnd,200,"Operation Successfull");
+ initResponse(mockBackEnd,200,"Operation Successfull");
+
+ ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.OK);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ mockBackEnd.shutdown();
+ }
+
+
+ @Test
+ void test_getPolicyModelDistributionByIdReturnUnauthorized() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,401,"Authentication Error");
+
+ ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ mockBackEnd.shutdown();
+ }
+
+ @Test
+ void test_getPolicyModelDistributionByIdReturnForbidden() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,403,"Authorization Error");
+
+ ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
+
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
+ mockBackEnd.shutdown();
+ }
+
+ @Test
+ void test_getPolicyModelDistributionByIdReturnNotFound() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,404,"Policy Model Not Found");
+
+ ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
+ mockBackEnd.shutdown();
+ }
+
+ @Test
+ void test_getPolicyModelDistributionByIdReturnUnsupportedMediaType() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,415,"Unsupported Media Type");
+
+ ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ mockBackEnd.shutdown();
+ }
+
+ @Test
+ void test_getPolicyModelDistributionByIdReturnInternalServerError() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById("test")).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,500,"Internal Server Error");
+
+ ResponseEntity expected = pmDistributionImplService.getPolicyModelDistributionById(PM_DISTRIBUTION_ENV,"test");
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById("test");
+ mockBackEnd.shutdown();
+ }
+
+
+ @Test
+ void test_distributePolicyModelReturnSucess() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ // Calling the same method twice to resolve the issue of mock server hanging for response
+ initResponse(mockBackEnd,200,"Operation Successfull");
+ initResponse(mockBackEnd,200,"Operation Successfull");
+
+ ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.OK);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ verify(policyModelGateway, times(1)).save(any());
+ mockBackEnd.shutdown();
+ }
+
+ @Test
+ void test_distributePolicyModelReturnBadRequest() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR)).thenReturn(policyModel);
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,400,"Invalid Body of Policy Model");
+
+ ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID_ERROR);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID_ERROR);
+ verify(policyModelGateway, times(1)).save(any());
+ mockBackEnd.shutdown();
+ }
+
+ @Test
+ void test_distributePolicyModelReturnUnauthorized() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,401,"Authentication Error");
+
+ ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ verify(policyModelGateway, times(1)).save(any());
+ }
+
+ @Test
+ void test_distributePolicyModelReturnForbidden() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,403,"Authorization Error");
+
+ ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ verify(policyModelGateway, times(1)).save(any());
+ }
+
+ @Test
+ void test_distributePolicyModelReturnNotAcceptable() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,406,"Not Acceptable Policy Model Version");
+
+ ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ verify(policyModelGateway, times(1)).save(any());
+ }
+
+ @Test
+ void test_distributePolicyModelReturnUnsupportedMediaType() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById(PM_DISTRIBUTION_MODEL_ID)).thenReturn(policyModel);
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,415,"Unsupported Media Type");
+
+ ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,PM_DISTRIBUTION_MODEL_ID);
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById(PM_DISTRIBUTION_MODEL_ID);
+ verify(policyModelGateway, times(1)).save(any());
+ }
+
+ @Test
+ void test_distributePolicyModelReturnInternalServerError() throws IOException {
+ PolicyModel policyModel = getPolicyModelResponse();
+
+ when(policyModelServiceImpl.getPolicyModelById("test")).thenReturn(policyModel);
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ MockWebServer mockBackEnd = initialize_webServer();
+
+ initResponse(mockBackEnd,500,"Internal Server Error");
+
+ ResponseEntity expected = pmDistributionImplService.distributePolicyModel(PM_DISTRIBUTION_ENV,"test");
+
+ assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
+ verify(policyModelServiceImpl, times(1)).getPolicyModelById("test");
+ verify(policyModelGateway, times(1)).save(any());
+ }
+
+ private MockWebServer initialize_webServer() throws IOException {
+ MockWebServer mockBackEnd = new MockWebServer();
+ mockBackEnd.start();
+
+ String baseUrl = String.format("http://localhost:%s", mockBackEnd.getPort());
+ when(policyModelUtils.getPolicyEngineURL(PM_DISTRIBUTION_ENV)).thenReturn(baseUrl);
+ when(policyModelUtils.getWebClient(PM_DISTRIBUTION_ENV)).thenReturn(WebClient.create(baseUrl));
+
+ return mockBackEnd;
+ }
+
+ private void initResponse(MockWebServer mockBackEnd, int responseCode, String responseBody) {
+ String webClientResponse = responseBody;
+ mockBackEnd.enqueue(new MockResponse().setResponseCode(responseCode).setBody(webClientResponse)
+ .addHeader("Content-Type", "text/plain;charset=ISO-8859-1"));
+ }
+
+
+} \ No newline at end of file
diff --git a/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelServiceImplTest.java b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelServiceImplTest.java
new file mode 100644
index 0000000..726ed3b
--- /dev/null
+++ b/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/service/PolicyModelServiceImplTest.java
@@ -0,0 +1,152 @@
+/*
+ * ============LICENSE_START=======================================================
+ * org.onap.dcae
+ * ================================================================================
+ * Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dcaegen2.platform.mod.web.service;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelConflictException;
+import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelNotFoundException;
+import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest;
+import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest;
+import org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother;
+import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelGateway;
+import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelServiceImpl;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother.USER;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.POLICY_MODEL_ID;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelCreateRequest;
+import static org.onap.dcaegen2.platform.mod.objectmothers.PolicyModelObjectMother.getPolicyModelUpdateRequest;
+
+@ExtendWith(MockitoExtension.class)
+class PolicyModelServiceImplTest {
+
+ @Spy
+ private PolicyModelServiceImpl pmImplService = new PolicyModelServiceImpl();
+
+ @Mock
+ private PolicyModelGateway policyModelGateway;
+
+
+ @BeforeEach
+ void setUp() {
+ pmImplService.setPolicyModelGateway(policyModelGateway);
+ }
+
+ @Test
+ void getAll() {
+ PolicyModel instance_1 = PolicyModel.builder().id("123").name("test").owner("user").version("1.0.0").build();
+ PolicyModel instance_2 = PolicyModel.builder().id("345").name("test1").owner("user1").version("1.1.0").build();
+
+ when(policyModelGateway.findAll()).thenReturn(Arrays.asList(instance_1, instance_2));
+
+ List<PolicyModel> instances = pmImplService.getAll();
+
+ assertThat(instances.size()).isEqualTo(2);
+ verify(policyModelGateway, times(1)).findAll();
+ }
+
+ @Test
+ void test_getPolicyModelById() {
+ PolicyModel expected = PolicyModelObjectMother.getPolicyModelResponse();
+
+ when(policyModelGateway.findById(POLICY_MODEL_ID)).thenReturn(Optional.of(expected));
+
+ PolicyModel original = pmImplService.getPolicyModelById(POLICY_MODEL_ID);
+
+ assertThat(original.getId()).isEqualTo(expected.getId());
+ verify(policyModelGateway, times(1)).findById(POLICY_MODEL_ID);
+ }
+
+ @Test
+ void test_policyModelNotFound_willRaiseException() {
+ when(policyModelGateway.findById(POLICY_MODEL_ID)).thenReturn(Optional.empty());
+ assertThatExceptionOfType(PolicyModelNotFoundException.class).isThrownBy(() -> pmImplService.getPolicyModelById(POLICY_MODEL_ID));
+ }
+
+
+ @Test
+ void test_createPolicyModel() {
+
+ PolicyModelCreateRequest policyModelCreateRequest = getPolicyModelCreateRequest();
+ PolicyModel policyModel = PolicyModelObjectMother.getPolicyModelResponse();
+
+ when(policyModelGateway.findByNameAndVersion(policyModelCreateRequest.getName(), policyModelCreateRequest.getVersion())).thenReturn(Optional.empty());
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ pmImplService.createPolicyModel(policyModelCreateRequest,USER);
+
+ assertThat(policyModel.getVersion()).isEqualTo(policyModelCreateRequest.getVersion());
+ assertThat(policyModel.getName()).isEqualTo(policyModelCreateRequest.getName());
+
+ verify(policyModelGateway, times(1)).findByNameAndVersion(policyModelCreateRequest.getName(), policyModelCreateRequest.getVersion());
+ verify(policyModelGateway, times(1)).save(any());
+ }
+
+ @Test
+ void test_createPolicyModel_willRaiseException() {
+ PolicyModelCreateRequest policyModelCreateRequest = getPolicyModelCreateRequest();
+ PolicyModel policyModel = PolicyModelObjectMother.getPolicyModelResponse();
+
+ when(policyModelGateway.findByNameAndVersion(policyModelCreateRequest.getName(), policyModelCreateRequest.getVersion())).thenReturn(Optional.of(policyModel));
+
+ assertThatExceptionOfType(PolicyModelConflictException.class).isThrownBy(() -> pmImplService.createPolicyModel(policyModelCreateRequest,USER));
+ }
+
+ @Test
+ void test_updatePolicyModel() {
+
+ PolicyModelUpdateRequest policyModelUpdateRequest = getPolicyModelUpdateRequest();
+ policyModelUpdateRequest.setName("test1");
+ policyModelUpdateRequest.setVersion("1.2.1");
+ PolicyModel policyModel = PolicyModelObjectMother.getPolicyModelResponse();
+
+ when(policyModelGateway.findById(POLICY_MODEL_ID)).thenReturn(Optional.of(policyModel));
+
+ when(policyModelGateway.findByNameAndVersion(policyModelUpdateRequest.getName(), policyModelUpdateRequest.getVersion())).thenReturn(Optional.empty());
+ when(policyModelGateway.save(any())).thenReturn(policyModel);
+
+ pmImplService.updatePolicyModel(policyModelUpdateRequest,POLICY_MODEL_ID, USER);
+
+ assertThat(policyModel.getVersion()).isEqualTo(policyModelUpdateRequest.getVersion());
+ assertThat(policyModel.getName()).isEqualTo(policyModelUpdateRequest.getName());
+
+ verify(policyModelGateway, times(1)).findById(POLICY_MODEL_ID);
+ verify(policyModelGateway, times(1)).findByNameAndVersion(policyModelUpdateRequest.getName(), policyModelUpdateRequest.getVersion());
+ verify(policyModelGateway, times(1)).save(any());
+ }
+
+
+} \ No newline at end of file