X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fconfiguration%2FApplicationConfigTest.java;h=96abd1c81e3810613a90d93b7fbfa8187bb39edb;hb=6a5e9e710f27997073db0c78b574681aa18189aa;hp=6a4f8b3681bc69b3d4223d8346f9900f10e54223;hpb=fb4bc7967a4733d10775351440a3af14327d5f20;p=nonrtric.git diff --git a/policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigTest.java b/policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigTest.java index 6a4f8b36..96abd1c8 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/configuration/ApplicationConfigTest.java @@ -2,7 +2,7 @@ * ========================LICENSE_START================================= * O-RAN-SC * %% - * Copyright (C) 2019 Nordix Foundation + * Copyright (C) 2020 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,187 +20,109 @@ package org.oransc.policyagent.configuration; -import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.read.ListAppender; - -import com.google.common.base.Charsets; -import com.google.common.io.Resources; -import com.google.gson.JsonIOException; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.nio.charset.StandardCharsets; + import java.util.Arrays; +import java.util.HashMap; import java.util.Properties; import java.util.Vector; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.ImmutableEnvProperties; +import org.oransc.policyagent.configuration.ApplicationConfig.RicConfigUpdate; +import org.oransc.policyagent.configuration.ApplicationConfigParser.ConfigParserResult; import org.oransc.policyagent.exceptions.ServiceException; -import org.oransc.policyagent.utils.LoggingUtils; - -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; -import reactor.test.StepVerifier; @ExtendWith(MockitoExtension.class) -@RunWith(MockitoJUnitRunner.class) public class ApplicationConfigTest { - private ApplicationConfig appConfigUnderTest; - - @Mock - CbsClient cbsClient; - - public static final ImmutableRicConfig CORRECT_RIC_CONIFG = ImmutableRicConfig.builder() // + private static final ImmutableRicConfig RIC_CONFIG_1 = ImmutableRicConfig.builder() // .name("ric1") // - .baseUrl("http://localhost:8080/") // - .managedElementIds(new Vector(Arrays.asList("kista_1", "kista_2"))) // + .baseUrl("ric1_url") // + .managedElementIds(new Vector<>()) // + .controllerName("") // .build(); - private static EnvProperties properties() { - return ImmutableEnvProperties.builder() // - .consulHost("host") // - .consulPort(123) // - .cbsName("cbsName") // - .appName("appName") // + ConfigParserResult configParserResult(RicConfig... rics) { + return ImmutableConfigParserResult.builder() // + .ricConfigs(Arrays.asList(rics)) // + .dmaapConsumerConfig(new Properties()) // + .dmaapPublisherConfig(new Properties()) // + .controllerConfigs(new HashMap<>()) // .build(); } @Test - public void whenTheConfigurationFits() throws IOException, ServiceException { - - appConfigUnderTest = spy(ApplicationConfig.class); - appConfigUnderTest.systemEnvironment = new Properties(); - // When - doReturn(getCorrectJson()).when(appConfigUnderTest).createInputStream(any()); - doReturn("fileName").when(appConfigUnderTest).getLocalConfigurationFilePath(); - appConfigUnderTest.initialize(); - - // Then - verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(); - - Iterable ricConfigs = appConfigUnderTest.getRicConfigs(); - RicConfig ricConfig = ricConfigs.iterator().next(); - assertThat(ricConfigs).isNotNull(); - assertThat(ricConfig).isEqualTo(CORRECT_RIC_CONIFG); - } + public void gettingNotAddedRicShouldThrowException() { + ApplicationConfig appConfigUnderTest = new ApplicationConfig(); - @Test - public void whenFileIsExistsButJsonIsIncorrect() throws IOException, ServiceException { - - appConfigUnderTest = spy(ApplicationConfig.class); - appConfigUnderTest.systemEnvironment = new Properties(); + appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)); - // When - doReturn(getIncorrectJson()).when(appConfigUnderTest).createInputStream(any()); - doReturn("fileName").when(appConfigUnderTest).getLocalConfigurationFilePath(); - appConfigUnderTest.loadConfigurationFromFile(); + Exception exception = assertThrows(ServiceException.class, () -> { + appConfigUnderTest.getRic("name"); + }); - // Then - verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(); - Assertions.assertEquals(0, appConfigUnderTest.getRicConfigs().size()); + assertEquals("Could not find ric configuration: name", exception.getMessage()); } @Test - public void whenPeriodicConfigRefreshNoEnvironmentVariables() { + public void addRicShouldNotifyAllObserversOfRicAdded() throws Exception { + ApplicationConfig appConfigUnderTest = new ApplicationConfig(); - appConfigUnderTest = spy(ApplicationConfig.class); - appConfigUnderTest.systemEnvironment = new Properties(); + RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst(); + assertEquals(RicConfigUpdate.Type.ADDED, update.getType()); + assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric not added to configurations."); - final ListAppender logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class); - Flux task = appConfigUnderTest.createRefreshTask(); + assertEquals(RIC_CONFIG_1, appConfigUnderTest.getRic(RIC_CONFIG_1.name()), + "Not correct Ric retrieved from configurations."); - StepVerifier.create(task).expectSubscription().verifyComplete(); + update = appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)).blockFirst(); + assertNull(update, "Nothing should be updated"); + assertTrue(appConfigUnderTest.getRicConfigs().contains(RIC_CONFIG_1), "Ric should remain."); - assertTrue(logAppender.list.toString().contains("$CONSUL_HOST environment has not been defined")); } @Test - public void whenPeriodicConfigRefreshNoConsul() { - appConfigUnderTest = spy(ApplicationConfig.class); - appConfigUnderTest.systemEnvironment = new Properties(); + public void changedRicShouldNotifyAllObserversOfRicChanged() throws Exception { + ApplicationConfig appConfigUnderTest = new ApplicationConfig(); - EnvProperties props = properties(); - doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any()); + appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1)); - doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props); - Flux err = Flux.error(new IOException()); - doReturn(err).when(cbsClient).updates(any(), any(), any()); - - final ListAppender logAppender = LoggingUtils.getLogListAppender(ApplicationConfig.class); - Flux task = appConfigUnderTest.createRefreshTask(); + ImmutableRicConfig changedRicConfig = ImmutableRicConfig.builder() // + .name("ric1") // + .baseUrl("changed_ric1_url") // + .managedElementIds(new Vector<>()) // + .controllerName("") // + .build(); - StepVerifier // - .create(task) // - .expectSubscription() // - .verifyComplete(); + RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(changedRicConfig)).blockFirst(); - assertTrue( - logAppender.list.toString().contains("Could not refresh application configuration java.io.IOException")); + assertEquals(RicConfigUpdate.Type.CHANGED, update.getType()); + assertEquals(changedRicConfig, appConfigUnderTest.getRic(RIC_CONFIG_1.name()), + "Changed Ric not retrieved from configurations."); } @Test - public void whenPeriodicConfigRefreshSuccess() throws JsonIOException, JsonSyntaxException, IOException { - appConfigUnderTest = spy(ApplicationConfig.class); - appConfigUnderTest.systemEnvironment = new Properties(); - - EnvProperties props = properties(); - doReturn(Mono.just(props)).when(appConfigUnderTest).getEnvironment(any()); - doReturn(Mono.just(cbsClient)).when(appConfigUnderTest).createCbsClient(props); - - Flux json = Flux.just(getJsonRootObject()); - doReturn(json).when(cbsClient).updates(any(), any(), any()); - - Flux task = appConfigUnderTest.createRefreshTask(); - - StepVerifier // - .create(task) // - .expectSubscription() // - .expectNext(appConfigUnderTest) // - .verifyComplete(); - - Assertions.assertNotNull(appConfigUnderTest.getRicConfigs()); - } + public void removedRicShouldNotifyAllObserversOfRicRemoved() { + ApplicationConfig appConfigUnderTest = new ApplicationConfig(); + + ImmutableRicConfig ricConfig2 = ImmutableRicConfig.builder() // + .name("ric2") // + .baseUrl("ric2_url") // + .managedElementIds(new Vector<>()) // + .controllerName("") // + .build(); - private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException { - JsonObject rootObject = (new JsonParser()).parse(new InputStreamReader(getCorrectJson())).getAsJsonObject(); - return rootObject; - } + appConfigUnderTest.setConfiguration(configParserResult(RIC_CONFIG_1, ricConfig2)); - private static InputStream getCorrectJson() throws IOException { - URL url = ApplicationConfigParser.class.getClassLoader().getResource("test_application_configuration.json"); - String string = Resources.toString(url, Charsets.UTF_8); - return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8))); - } + RicConfigUpdate update = appConfigUnderTest.setConfiguration(configParserResult(ricConfig2)).blockFirst(); - private static InputStream getIncorrectJson() { - String string = "{" + // - " \"config\": {" + // - " \"ric\": {"; // - return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8))); + assertEquals(RicConfigUpdate.Type.REMOVED, update.getType()); + assertEquals(1, appConfigUnderTest.getRicConfigs().size(), "Ric not deleted from configurations."); } }