diff options
author | 2025-02-26 13:56:15 +0530 | |
---|---|---|
committer | 2025-02-26 17:23:23 +0530 | |
commit | e1ac25f7398b1fb4d29acd46bbe88bf1452ab45b (patch) | |
tree | 9f31d354dca98d955c74c98b1c3b3b39d6df1425 /src/test | |
parent | 6e23edfb1bad0b4d30986518adfab24644584aaa (diff) |
- to improve test coverage for model-loader <=80%
Issue-ID: AAI-4140
Change-Id: I69565e8d27ebccbfc4b75277f9da83b3e3fad40a
Signed-off-by: nisha.gangore <nisha.gangore@accenture.com>
Diffstat (limited to 'src/test')
7 files changed, 427 insertions, 6 deletions
diff --git a/src/test/java/org/onap/aai/modelloader/notification/CompDoneStatusMessageBuilderTest.java b/src/test/java/org/onap/aai/modelloader/notification/CompDoneStatusMessageBuilderTest.java new file mode 100644 index 0000000..22d9038 --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/notification/CompDoneStatusMessageBuilderTest.java @@ -0,0 +1,104 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. 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.aai.modelloader.notification; + + import org.junit.jupiter.api.Test; + import org.onap.sdc.api.IDistributionClient; + import org.onap.sdc.api.consumer.IComponentDoneStatusMessage; + import org.onap.sdc.api.consumer.IConfiguration; + import org.onap.sdc.api.notification.INotificationData; + import org.onap.sdc.utils.DistributionStatusEnum; + import org.springframework.boot.test.context.SpringBootTest; + import org.springframework.test.context.TestPropertySource; + import static org.mockito.Mockito.*; + import static org.junit.jupiter.api.Assertions.*; + + @SpringBootTest + @TestPropertySource(properties = {"CONFIG_HOME=src/test/resources"}) + public class CompDoneStatusMessageBuilderTest { + + @Test + public void testBuild() { + IDistributionClient mockClient = mock(IDistributionClient.class); + INotificationData mockData = mock(INotificationData.class); + + IConfiguration mockConfig = mock(IConfiguration.class); + when(mockClient.getConfiguration()).thenReturn(mockConfig); + when(mockConfig.getConsumerID()).thenReturn("consumer123"); + + when(mockData.getDistributionID()).thenReturn("distID456"); + + DistributionStatusEnum status = DistributionStatusEnum.DEPLOY_OK; + IComponentDoneStatusMessage result = CompDoneStatusMessageBuilder.build(mockClient, mockData, status); + + assertNotNull(result); + assertTrue(result instanceof CompDoneStatusMsg); + CompDoneStatusMsg statusMsg = (CompDoneStatusMsg) result; + + assertEquals("distID456", statusMsg.getDistributionID()); + assertEquals("consumer123", statusMsg.getConsumerID()); + assertEquals(DistributionStatusEnum.DEPLOY_OK, statusMsg.getStatus()); + } + + @Test + public void testBuildWithFailureStatus() { + IDistributionClient mockClient = mock(IDistributionClient.class); + INotificationData mockData = mock(INotificationData.class); + + IConfiguration mockConfig = mock(IConfiguration.class); + when(mockClient.getConfiguration()).thenReturn(mockConfig); + when(mockConfig.getConsumerID()).thenReturn("consumer123"); + + when(mockData.getDistributionID()).thenReturn("distID456"); + + DistributionStatusEnum status = DistributionStatusEnum.DEPLOY_ERROR; + IComponentDoneStatusMessage result = CompDoneStatusMessageBuilder.build(mockClient, mockData, status); + + assertNotNull(result); + assertTrue(result instanceof CompDoneStatusMsg); + CompDoneStatusMsg statusMsg = (CompDoneStatusMsg) result; + + assertEquals("distID456", statusMsg.getDistributionID()); + assertEquals("consumer123", statusMsg.getConsumerID()); + assertEquals(DistributionStatusEnum.DEPLOY_ERROR, statusMsg.getStatus()); + } + + @Test + public void testBuildHandlesNullValues() { + IDistributionClient mockClient = mock(IDistributionClient.class); + INotificationData mockData = mock(INotificationData.class); + + IConfiguration mockConfig = mock(IConfiguration.class); + when(mockClient.getConfiguration()).thenReturn(mockConfig); + when(mockConfig.getConsumerID()).thenReturn(null); + when(mockData.getDistributionID()).thenReturn(null); + + DistributionStatusEnum status = DistributionStatusEnum.DEPLOY_OK; + IComponentDoneStatusMessage result = CompDoneStatusMessageBuilder.build(mockClient, mockData, status); + + assertNotNull(result); + assertTrue(result instanceof CompDoneStatusMsg); + CompDoneStatusMsg statusMsg = (CompDoneStatusMsg) result; + + assertNull(statusMsg.getDistributionID()); + assertNull(statusMsg.getConsumerID()); + assertEquals(DistributionStatusEnum.DEPLOY_OK, statusMsg.getStatus()); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/aai/modelloader/notification/DistributionStatusMessageBuilderTest.java b/src/test/java/org/onap/aai/modelloader/notification/DistributionStatusMessageBuilderTest.java new file mode 100644 index 0000000..93456b7 --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/notification/DistributionStatusMessageBuilderTest.java @@ -0,0 +1,83 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. 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.aai.modelloader.notification; + +import org.junit.jupiter.api.Test; +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.consumer.IConfiguration; +import org.onap.sdc.api.consumer.IDistributionStatusMessage; +import org.onap.sdc.api.notification.IArtifactInfo; +import org.onap.sdc.api.notification.INotificationData; +import static org.mockito.Mockito.*; +import org.onap.sdc.utils.DistributionStatusEnum; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import static org.junit.jupiter.api.Assertions.*; +import org.springframework.test.context.TestPropertySource; + +@SpringBootTest +@TestPropertySource(properties = {"CONFIG_HOME=src/test/resources"}) +public class DistributionStatusMessageBuilderTest { + + @Test + public void testBuild() { + IDistributionClient mockClient = mock(IDistributionClient.class); + IConfiguration mockConfig = mock(IConfiguration.class); + when(mockClient.getConfiguration()).thenReturn(mockConfig); + when(mockConfig.getConsumerID()).thenReturn("testConsumerID"); + + INotificationData mockData = mock(INotificationData.class); + when(mockData.getDistributionID()).thenReturn("testDistributionID"); + + IArtifactInfo mockArtifact = mock(IArtifactInfo.class); + when(mockArtifact.getArtifactURL()).thenReturn("http://example.com/artifact"); + + DistributionStatusEnum status = DistributionStatusEnum.DEPLOY_OK; + IDistributionStatusMessage result = DistributionStatusMessageBuilder.build(mockClient, mockData, mockArtifact, status); + + assertNotNull(result); + assertEquals("testDistributionID", result.getDistributionID()); + assertEquals("testConsumerID", result.getConsumerID()); + assertEquals("http://example.com/artifact", result.getArtifactURL()); + assertEquals(DistributionStatusEnum.DEPLOY_OK, result.getStatus()); + } + + @Test + public void testBuildWithoutArtifactInfo() { + IDistributionClient mockClient = mock(IDistributionClient.class); + IConfiguration mockConfig = mock(IConfiguration.class); + when(mockClient.getConfiguration()).thenReturn(mockConfig); + when(mockConfig.getConsumerID()).thenReturn("testConsumerID"); + + INotificationData mockData = mock(INotificationData.class); + when(mockData.getDistributionID()).thenReturn("testDistributionID"); + + DistributionStatusEnum status = DistributionStatusEnum.DEPLOY_OK; + IDistributionStatusMessage result = DistributionStatusMessageBuilder.build(mockClient, mockData, status); + + assertNotNull(result); + assertEquals("testDistributionID", result.getDistributionID()); + assertEquals("testConsumerID", result.getConsumerID()); + assertEquals("", result.getArtifactURL()); + assertEquals(DistributionStatusEnum.DEPLOY_OK, result.getStatus()); + } +} + + diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java b/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java index ef346db..08fbb71 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java @@ -20,21 +20,22 @@ */ package org.onap.aai.modelloader.notification; -import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; - import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Properties; - import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.onap.aai.modelloader.entity.Artifact; +import org.onap.aai.modelloader.entity.ArtifactType; import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; import org.onap.aai.modelloader.service.ArtifactDeploymentManager; import org.onap.sdc.api.IDistributionClient; @@ -79,7 +80,7 @@ public class TestEventCallback { INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class) - )).thenThrow(DownloadFailureException.class); + )).thenThrow(DownloadFailureException.class); eventCallback.activateCallback(data); @@ -93,7 +94,7 @@ public class TestEventCallback { INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class))) - .thenReturn(Collections.emptyList()); + .thenReturn(Collections.emptyList()); when(mockArtifactDeploymentManager.deploy(any(String.class), any(List.class), any(List.class))) .thenReturn(true); @@ -103,4 +104,21 @@ public class TestEventCallback { verify(mockArtifactDownloadManager).downloadArtifacts(any(INotificationData.class), any(List.class)); verify(mockArtifactDeploymentManager).deploy(any(String.class), any(List.class), any(List.class)); } + @Test + public void testActivateCallback_withVnfCatalogArtifacts() throws Exception { + INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile(); + + List<Artifact> downloadedArtifacts = new ArrayList<>(); + downloadedArtifacts.add(new Artifact(ArtifactType.MODEL)); + downloadedArtifacts.add(new Artifact(ArtifactType.VNF_CATALOG)); + + when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class))) + .thenReturn(downloadedArtifacts); + + eventCallback.activateCallback(data); + + verify(mockArtifactDeploymentManager).deploy(eq("ID"), + argThat(list -> list.stream().anyMatch(a -> a.getType() == ArtifactType.MODEL)), + anyList()); + } } diff --git a/src/test/java/org/onap/aai/modelloader/restclient/BabelServiceClientExceptionTest.java b/src/test/java/org/onap/aai/modelloader/restclient/BabelServiceClientExceptionTest.java new file mode 100644 index 0000000..ceb9391 --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/restclient/BabelServiceClientExceptionTest.java @@ -0,0 +1,44 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. 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.aai.modelloader.restclient; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class BabelServiceClientExceptionTest { + + @Test + public void testConstructorWithMessage() { + String expectedMessage = "This is a test error message"; + BabelServiceClientException exception = new BabelServiceClientException(expectedMessage); + assertNotNull(exception); + assertEquals(expectedMessage, exception.getMessage(), "The exception message should match."); + } + + @Test + public void testConstructorWithException() { + Exception underlyingException = new Exception("Underlying exception"); + BabelServiceClientException exception = new BabelServiceClientException(underlyingException); + + // Assert + assertNotNull(exception); + assertEquals(underlyingException, exception.getCause(), "The cause should match the passed exception."); + } +} diff --git a/src/test/java/org/onap/aai/modelloader/service/EchoServiceTest.java b/src/test/java/org/onap/aai/modelloader/service/EchoServiceTest.java new file mode 100644 index 0000000..ea3ecd0 --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/service/EchoServiceTest.java @@ -0,0 +1,51 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. 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.aai.modelloader.service; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; + +@WebMvcTest(EchoService.class) +public class EchoServiceTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void testEcho() throws Exception { + String input = "hello"; + mockMvc.perform(get("/services/model-loader/v1/echo-service/echo/{input}", input)) + .andExpect(status().isOk()) + .andExpect(content().string(input)); + } + + @Test + public void testEchoSpecialCharacters() throws Exception { + String input = "!@#$%^&*()"; + mockMvc.perform(get("/services/model-loader/v1/echo-service/echo/{input}", input)) + .andExpect(status().isOk()) + .andExpect(content().string(input)); + } +} diff --git a/src/test/java/org/onap/aai/modelloader/service/TestSdcConnectionJob.java b/src/test/java/org/onap/aai/modelloader/service/TestSdcConnectionJob.java new file mode 100644 index 0000000..ac9d7ad --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/service/TestSdcConnectionJob.java @@ -0,0 +1,122 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2025 Deutsche Telekom. 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.aai.modelloader.service; + +import static org.mockito.Mockito.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; +import org.onap.aai.cl.api.Logger; +import org.onap.aai.cl.eelf.LoggerFactory; +import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.notification.EventCallback; +import org.onap.sdc.api.IDistributionClient; +import org.onap.sdc.api.results.IDistributionClientResult; +import org.onap.sdc.utils.DistributionActionResultEnum; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; + +import java.util.Timer; + +@SpringBootTest +@TestPropertySource(properties = {"CONFIG_HOME=src/test/resources",}) +public class TestSdcConnectionJob { + + @Mock + private IDistributionClient client; + + @Mock + private ModelLoaderConfig config; + + @Mock + private EventCallback callback; + + @Mock + private Timer timer; + + @Mock + private IDistributionClientResult clientResult; + + @Mock + private Logger logger; + + private SdcConnectionJob connectionJob; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + LoggerFactory loggerFactorySpy = mock(LoggerFactory.class); + when(loggerFactorySpy.getLogger(SdcConnectionJob.class.getName())).thenReturn(logger); + connectionJob = new SdcConnectionJob(client, config, callback, timer); + } + + @Test + public void testRunWhenASDCConnectionDisabled() { + when(config.getASDCConnectionDisabled()).thenReturn(true); + connectionJob.run(); + verify(client, never()).init(any(), any()); + verify(client, never()).start(); + verify(config, times(1)).getASDCConnectionDisabled(); + } + + @Test + public void testRunInitializationFails() { + when(config.getASDCConnectionDisabled()).thenReturn(false); + when(client.init(config, callback)).thenReturn(clientResult); + when(clientResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.FAIL); + connectionJob.run(); + verify(client).init(config, callback); + verify(client, never()).start(); + verify(config, times(1)).getASDCConnectionDisabled(); + } + + @Test + public void testRunInitializationSucceedsButStartFails() { + when(config.getASDCConnectionDisabled()).thenReturn(false); + when(client.init(config, callback)).thenReturn(clientResult); + when(clientResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS); + + IDistributionClientResult startResult = mock(IDistributionClientResult.class); + when(client.start()).thenReturn(startResult); + when(startResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.FAIL); + + connectionJob.run(); + verify(client,times(1)).start(); + verify(timer, never()).cancel(); + verify(config, times(1)).getASDCConnectionDisabled(); + } + + @Test + public void testRunInitializationAndStartBothSucceed() { + when(config.getASDCConnectionDisabled()).thenReturn(false); + when(client.init(config, callback)).thenReturn(clientResult); + when(clientResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS); + + IDistributionClientResult startResult = mock(IDistributionClientResult.class); + when(client.start()).thenReturn(startResult); + when(startResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS); + + connectionJob.run(); + verify(client).init(config, callback); + verify(client,times(1)).start(); + verify(timer).cancel(); + } + +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 69f4ff7..f3aeb10 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -3,7 +3,6 @@ spring.kafka.consumer.auto-offset-reset=earliest spring.kafka.consumer.group-id=aai spring.kafka.consumer.client-id=aai-model-loader topics.distribution.notification=SDC-DISTR-NOTIF-TOPIC-AUTO - spring.sleuth.enabled=false ml.distribution.connection.enabled=false # avoid having the distribution client running in the background (requires active kafka) |