diff options
Diffstat (limited to 'common/src/test/java')
4 files changed, 159 insertions, 104 deletions
diff --git a/common/src/test/java/org/onap/so/client/dmaap/rest/DMaaPRestClientTest.java b/common/src/test/java/org/onap/so/client/dmaap/rest/DMaaPRestClientTest.java index 8006423e75..ca5b5da2da 100644 --- a/common/src/test/java/org/onap/so/client/dmaap/rest/DMaaPRestClientTest.java +++ b/common/src/test/java/org/onap/so/client/dmaap/rest/DMaaPRestClientTest.java @@ -85,6 +85,7 @@ public class DMaaPRestClientTest { throw new RuntimeException(e); } + MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, null); DMaaPRestClient client = new DMaaPRestClient(url, contentType, auth, key); Map<String, String> map = new HashMap<>(); client.initializeHeaderMap(map); 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); diff --git a/common/src/test/java/org/onap/so/logger/ScheduledTasksMDCSetupTest.java b/common/src/test/java/org/onap/so/logger/ScheduledTasksMDCSetupTest.java new file mode 100644 index 0000000000..f232781871 --- /dev/null +++ b/common/src/test/java/org/onap/so/logger/ScheduledTasksMDCSetupTest.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 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.so.logger; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.junit.After; +import org.junit.Test; +import org.onap.logging.filter.base.Constants; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.onap.so.utils.Components; +import org.onap.so.utils.UUIDChecker; +import org.slf4j.MDC; + +public class ScheduledTasksMDCSetupTest { + private ScheduledTasksMDCSetup tasksMDCSetup = new ScheduledTasksMDCSetup(); + + @After + public void tearDown() { + MDC.clear(); + System.clearProperty("partnerName"); + } + + @Test + public void mdcSetupTest() { + System.setProperty("partnerName", Components.APIH.toString()); + tasksMDCSetup.mdcSetup(Components.APIH, "mdcSetupTest"); + + assertTrue(UUIDChecker.isValidUUID(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID))); + assertEquals(Components.APIH.toString(), MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY)); + assertEquals(Components.APIH.toString(), MDC.get(ONAPLogConstants.MDCs.PARTNER_NAME)); + assertEquals("mdcSetupTest", MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME)); + assertEquals(Constants.DefaultValues.UNKNOWN, MDC.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME)); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP)); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.ELAPSED_TIME)); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.LOG_TIMESTAMP)); + assertNotNull(MDC.get(ONAPLogConstants.MDCs.SERVER_FQDN)); + } + + @Test + public void errorMDCSetupTest() { + tasksMDCSetup.errorMDCSetup(ErrorCode.UnknownError, "Error"); + + assertEquals("900", MDC.get(ONAPLogConstants.MDCs.ERROR_CODE)); + assertEquals("Error", MDC.get(ONAPLogConstants.MDCs.ERROR_DESC)); + } + + @Test + public void setStatusCodeTest() { + tasksMDCSetup.setStatusCode(); + + assertEquals(ONAPLogConstants.ResponseStatus.COMPLETE.toString(), + MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); + } + + @Test + public void setStatusCodeErrorTest() { + MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.ERROR.toString()); + tasksMDCSetup.setStatusCode(); + + assertEquals(ONAPLogConstants.ResponseStatus.ERROR.toString(), + MDC.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE)); + } +} diff --git a/common/src/test/java/org/onap/so/utils/ExternalTaskServiceUtilsTest.java b/common/src/test/java/org/onap/so/utils/ExternalTaskServiceUtilsTest.java deleted file mode 100644 index b2db986d02..0000000000 --- a/common/src/test/java/org/onap/so/utils/ExternalTaskServiceUtilsTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.onap.so.utils; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.doReturn; -import org.camunda.bpm.client.ExternalTaskClient; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.core.env.Environment; - -@RunWith(MockitoJUnitRunner.class) -public class ExternalTaskServiceUtilsTest { - - @Spy - @InjectMocks - private ExternalTaskServiceUtils utils = new ExternalTaskServiceUtils(); - - @Mock - private Environment mockEnv; - - @Mock - private ExternalTaskClient mockClient; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - doReturn("3").when(mockEnv).getProperty("workflow.topics.maxClients", "3"); - doReturn("07a7159d3bf51a0e53be7a8f89699be7").when(mockEnv).getRequiredProperty("mso.msoKey"); - doReturn("6B466C603A260F3655DBF91E53CE54667041C01406D10E8CAF9CC24D8FA5388D06F90BFE4C852052B436").when(mockEnv) - .getRequiredProperty("mso.auth"); - doReturn("someid").when(mockEnv).getRequiredProperty("mso.config.cadi.aafId"); - doReturn("http://camunda.com").when(mockEnv).getRequiredProperty("mso.workflow.endpoint"); - } - - @Test - public void testCreateExternalTaskClient() throws Exception { - ExternalTaskClient actualClient = utils.createExternalTaskClient(); - Assert.assertNotNull(actualClient); - } - - @Test - public void testGetAuth() throws Exception { - String actual = utils.getAuth(); - String expected = "Att32054Life!@"; - assertEquals(expected, actual); - } - - @Test - public void testGetMaxClients() throws Exception { - int actual = utils.getMaxClients(); - int expected = 3; - assertEquals(expected, actual); - } - -} |