diff options
Diffstat (limited to 'common/src/test/java')
-rw-r--r-- | common/src/test/java/org/onap/so/client/policy/PolicyClientImplTest.java | 118 |
1 files changed, 75 insertions, 43 deletions
diff --git a/common/src/test/java/org/onap/so/client/policy/PolicyClientImplTest.java b/common/src/test/java/org/onap/so/client/policy/PolicyClientImplTest.java index ad22c65248..3323fd8409 100644 --- a/common/src/test/java/org/onap/so/client/policy/PolicyClientImplTest.java +++ b/common/src/test/java/org/onap/so/client/policy/PolicyClientImplTest.java @@ -4,6 +4,8 @@ * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Copyright (C) 2019 Nokia. + * ================================================================================ * 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 @@ -23,32 +25,50 @@ package org.onap.so.client.policy; import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mockito.doReturn; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import org.onap.so.client.RestClient; import org.onap.so.client.defaultproperties.PolicyRestPropertiesImpl; +import org.onap.so.client.policy.entities.AllowedTreatments; +import org.onap.so.client.policy.entities.Bbid; import org.onap.so.client.policy.entities.Config; import org.onap.so.client.policy.entities.ConfigRequestParameters; +import org.onap.so.client.policy.entities.DecisionAttributes; import org.onap.so.client.policy.entities.DictionaryData; +import org.onap.so.client.policy.entities.DictionaryItemsRequest; +import org.onap.so.client.policy.entities.DictionaryJson; import org.onap.so.client.policy.entities.PolicyConfig; import org.onap.so.client.policy.entities.PolicyDecision; +import org.onap.so.client.policy.entities.PolicyDecisionRequest; import org.onap.so.client.policy.entities.PolicyServiceType; -import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import org.onap.so.client.policy.entities.Workstep; public class PolicyClientImplTest { + private static final String SERVICE_TYPE = "serviceTest"; + private static final String VNF_TYPE = "vTypeTest"; + private static final String BB_ID = "bbTest"; + private static final String WORK_STEP = "wStepTest"; + private static final String ERROR_CODE = "errCodeTest"; + @BeforeClass public static void setUp() { System.setProperty("mso.config.path", "src/test/resources"); @@ -67,53 +87,65 @@ public class PolicyClientImplTest { } @Test - @Ignore - public void getDecisionTest() { - PolicyClient client = new PolicyClientImpl(); - PolicyDecision decision = client.getDecision("S", "V", "BB1", "1", "123"); - assertEquals("Decision is correct", decision.getDecision(), "PERMIT"); - assertEquals("Decision details is correct", decision.getDetails(), "Retry"); + public void getDecision_success() { + // given + PolicyClientImpl testedObject = Mockito.spy(PolicyClientImpl.class); + PolicyRestClient policyRestClientMock = Mockito.mock(PolicyRestClient.class); + when(testedObject.getPolicyRestClient(PolicyServiceType.GET_DECISION)).thenReturn(policyRestClientMock); + // when + testedObject.getDecision(SERVICE_TYPE, VNF_TYPE, BB_ID, WORK_STEP, ERROR_CODE); + // then + ArgumentCaptor<PolicyDecisionRequest> captor1 = ArgumentCaptor.forClass(PolicyDecisionRequest.class); + verify(policyRestClientMock).post(captor1.capture(), eq(PolicyDecision.class)); + verifyPolicyDecisionRequestArg(captor1.getValue()); } - @Test - @Ignore - public void getAllowedTreatmentsTest() { - PolicyClient client = new PolicyClientImpl(); - DictionaryData dictClient = client.getAllowedTreatments("BB1", "1"); - final String dictBbidString = dictClient.getBbid().getString(); - final String dictWorkStepString = dictClient.getWorkstep().getString(); - assertEquals("DictionaryData matches a response Bbid", dictBbidString, "BB1"); - assertEquals("DicitonaryData matches a response WorkStep", dictWorkStepString, "1"); + private void verifyPolicyDecisionRequestArg(PolicyDecisionRequest actual) { + assertThat(actual.getEcompcomponentName()).isEqualTo(RestClient.ECOMP_COMPONENT_NAME); + DecisionAttributes decisionAttributes = actual.getDecisionAttributes(); + assertThat(decisionAttributes.getServiceType()).isEqualTo(SERVICE_TYPE); + assertThat(decisionAttributes.getvNFType()).isEqualTo(VNF_TYPE); + assertThat(decisionAttributes.getBbID()).isEqualTo(BB_ID); + assertThat(decisionAttributes.getWorkStep()).isEqualTo(WORK_STEP); + assertThat(decisionAttributes.getErrorCode()).isEqualTo(ERROR_CODE); } @Test - public void getDecisionMockTest() { - String serviceType = "S"; - String vnfType = "V"; - String bbID = "BB1"; - String workStep = "1"; - String errorCode = "123"; - - PolicyDecision expected = new PolicyDecision(); - expected.setDecision("PERMIT"); - expected.setDetails("Retry"); - - DecisionAttributes decisionAttributes = new DecisionAttributes(); - decisionAttributes.setServiceType(serviceType); - decisionAttributes.setVNFType(vnfType); - decisionAttributes.setBBID(bbID); - decisionAttributes.setWorkStep(workStep); - decisionAttributes.setErrorCode(errorCode); - PolicyClient client = Mockito.spy(PolicyClientImpl.class); - - doReturn(expected).when(client).getDecision(serviceType, vnfType, bbID, workStep, errorCode); - - PolicyDecision actual = client.getDecision(serviceType, vnfType, bbID, workStep, errorCode); - assertThat(actual, sameBeanAs(expected)); + public void getAllowedTreatments_success() { + // given + PolicyClientImpl testedObject = Mockito.spy(PolicyClientImpl.class); + PolicyRestClient policyRestClientMock = Mockito.mock(PolicyRestClient.class); + when(testedObject.getPolicyRestClient(PolicyServiceType.GET_DICTIONARY_ITEMS)).thenReturn(policyRestClientMock); + when(policyRestClientMock.post(any(DictionaryItemsRequest.class), eq(AllowedTreatments.class))) + .thenReturn(createAllowedTreatments()); + // when + DictionaryData dictionaryDataResult = testedObject.getAllowedTreatments(BB_ID, WORK_STEP); + // then + assertThat(dictionaryDataResult.getBbid().getString()).isEqualTo(BB_ID); + assertThat(dictionaryDataResult.getWorkstep().getString()).isEqualTo(WORK_STEP); + } + + private AllowedTreatments createAllowedTreatments() { + AllowedTreatments allowedTreatments = new AllowedTreatments(); + DictionaryJson dictionaryJson = new DictionaryJson(); + dictionaryJson.setDictionaryDatas(createDictionaryDataList()); + allowedTreatments.setDictionaryJson(dictionaryJson); + return allowedTreatments; + } + + private List<DictionaryData> createDictionaryDataList() { + DictionaryData dictionaryData = new DictionaryData(); + Bbid bbid = new Bbid(); + bbid.setString(BB_ID); + dictionaryData.setBbid(bbid); + Workstep workstep = new Workstep(); + workstep.setString(WORK_STEP); + dictionaryData.setWorkstep(workstep); + return Arrays.asList(dictionaryData, new DictionaryData()); } @Test - public void getConfigFromStringJsonTest() throws JsonParseException, JsonMappingException, IOException { + public void getConfigFromStringJsonTest() throws IOException { PolicyClientImpl client = new PolicyClientImpl(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); @@ -128,7 +160,7 @@ public class PolicyClientImplTest { } @Test - public void getConfigWithPolicyNameTest() throws JsonParseException, JsonMappingException, IOException { + public void getConfigWithPolicyNameTest() throws IOException { PolicyClientImpl client = Mockito.spy(PolicyClientImpl.class); ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); |