Search in sources :

Example 1 with ServicePluginLocator

use of org.ligoj.app.resource.ServicePluginLocator in project plugin-prov by ligoj.

the class TerraformResourceTest method newResource.

private TerraformResource newResource(final Terraforming providerResource, final BiFunction<Subscription, String[], File> toFile, final boolean dryRun, final String... customArgs) {
    final TerraformResource resource = new TerraformResource() {

        @Override
        protected File toFile(final Subscription subscription, final String file) {
            return toFile.apply(subscription, new String[] { file });
        }

        /**
         * Prepare the Terraform environment to apply the new environment. Note there is no concurrency check.
         */
        @Override
        protected File applyTerraform(final Subscription entity, final Terraforming terra, final QuoteVo configuration) throws IOException, InterruptedException {
            if (dryRun) {
                // Ignore this call
                return null;
            }
            return super.applyTerraform(entity, terra, configuration);
        }
    };
    super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    // Replace the plugin locator
    final ServicePluginLocator locator = Mockito.mock(ServicePluginLocator.class);
    resource.locator = locator;
    // Replace the runner
    resource.runner = new TerraformRunnerResource();
    super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource.runner);
    Mockito.when(locator.getResource("service:prov:test:account", Terraforming.class)).thenReturn(providerResource);
    final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
    Mockito.when(classLoader.getHomeDirectory()).thenReturn(MOCK_PATH.toPath());
    // Replace the CLI runner
    resource.terraformUtils = new TerraformUtils() {

        @Override
        public ProcessBuilder newBuilder(String... args) {
            return new ProcessBuilder(ArrayUtils.addAll(new String[] { "java", "-cp", MOCK_PATH.getParent(), "org.ligoj.app.plugin.prov.terraform.Main" }, customArgs.length > 0 ? customArgs : args));
        }

        @Override
        protected boolean isInstalled() {
            return true;
        }

        @Override
        protected PluginsClassLoader getClassLoader() {
            return classLoader;
        }

        public String getLastestVersion() {
            return "2.0.0";
        }
    };
    return resource;
}
Also used : QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) Subscription(org.ligoj.app.model.Subscription) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator)

Example 2 with ServicePluginLocator

use of org.ligoj.app.resource.ServicePluginLocator in project ligoj-api by ligoj.

the class NodeResourceTest method prepareEvent.

/**
 * Mock the servers for event test
 */
private int prepareEvent() throws Exception {
    final ServicePluginLocator servicePluginLocator = resourceMock.locator;
    // 1 : service is down
    final JiraPluginResource jira = Mockito.mock(JiraPluginResource.class);
    Mockito.when(servicePluginLocator.getResource(ArgumentMatchers.endsWith(":jira"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jira);
    Mockito.when(servicePluginLocator.getResourceExpected(ArgumentMatchers.endsWith(":jira"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jira);
    Mockito.when(jira.checkStatus(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenReturn(false);
    // 2 : service is up
    final SonarPluginResource sonar = Mockito.mock(SonarPluginResource.class);
    Mockito.when(servicePluginLocator.getResource(ArgumentMatchers.contains(":sonar"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(sonar);
    Mockito.when(servicePluginLocator.getResourceExpected(ArgumentMatchers.contains(":sonar"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(sonar);
    Mockito.when(sonar.checkStatus(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenReturn(true);
    // 3 : service throw an exception (down)
    final JenkinsPluginResource jenkins = Mockito.mock(JenkinsPluginResource.class);
    Mockito.when(servicePluginLocator.getResource(ArgumentMatchers.contains(":jenkins"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jenkins);
    Mockito.when(servicePluginLocator.getResourceExpected(ArgumentMatchers.contains(":jenkins"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jenkins);
    Mockito.when(jenkins.checkStatus(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenThrow(new TechnicalException("junit"));
    final int nbNodes = repository.findAllInstance().size();
    // Jirax2, Confluence, LDAP,
    Assertions.assertTrue(nbNodes >= 6);
    // SonarQube
    return nbNodes;
}
Also used : TechnicalException(org.ligoj.bootstrap.core.resource.TechnicalException) JenkinsPluginResource(org.ligoj.app.resource.node.sample.JenkinsPluginResource) JiraPluginResource(org.ligoj.app.resource.node.sample.JiraPluginResource) SonarPluginResource(org.ligoj.app.resource.node.sample.SonarPluginResource) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) ToolPlugin(org.ligoj.app.api.ToolPlugin)

Example 3 with ServicePluginLocator

use of org.ligoj.app.resource.ServicePluginLocator in project ligoj-api by ligoj.

the class NodeResourceTest method prepareSubscriptionsEvent.

private long prepareSubscriptionsEvent() throws Exception {
    // Check previous status
    final long eventsCount = eventRepository.count();
    Assertions.assertEquals(5, eventsCount);
    final ServicePluginLocator servicePluginLocator = resourceMock.locator;
    // Service is up --> SONAR
    final SonarPluginResource sonar = Mockito.mock(SonarPluginResource.class);
    Mockito.when(servicePluginLocator.getResource(ArgumentMatchers.anyString(), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(sonar);
    Mockito.when(servicePluginLocator.getResourceExpected(ArgumentMatchers.anyString(), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(sonar);
    Mockito.when(sonar.checkSubscriptionStatus(ArgumentMatchers.anyInt(), ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenReturn(new SubscriptionStatusWithData());
    Mockito.when(sonar.checkStatus(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenReturn(true);
    // Service is down --> JIRA
    final JiraPluginResource jira = Mockito.mock(JiraPluginResource.class);
    Mockito.when(servicePluginLocator.getResource(ArgumentMatchers.contains(":jira"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jira);
    Mockito.when(servicePluginLocator.getResourceExpected(ArgumentMatchers.contains(":jira"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jira);
    Mockito.when(jira.checkSubscriptionStatus(ArgumentMatchers.anyInt(), ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenReturn(new SubscriptionStatusWithData(false));
    // Service throw an exception --> JENKINS
    final JenkinsPluginResource jenkins = Mockito.mock(JenkinsPluginResource.class);
    Mockito.when(servicePluginLocator.getResource(ArgumentMatchers.contains(":jenkins"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jenkins);
    Mockito.when(servicePluginLocator.getResourceExpected(ArgumentMatchers.contains(":jenkins"), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jenkins);
    Mockito.when(jenkins.checkSubscriptionStatus(ArgumentMatchers.anyInt(), ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenThrow(new TechnicalException("junit"));
    return eventsCount;
}
Also used : SubscriptionStatusWithData(org.ligoj.app.api.SubscriptionStatusWithData) TechnicalException(org.ligoj.bootstrap.core.resource.TechnicalException) JenkinsPluginResource(org.ligoj.app.resource.node.sample.JenkinsPluginResource) JiraPluginResource(org.ligoj.app.resource.node.sample.JiraPluginResource) SonarPluginResource(org.ligoj.app.resource.node.sample.SonarPluginResource) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) ToolPlugin(org.ligoj.app.api.ToolPlugin)

Example 4 with ServicePluginLocator

use of org.ligoj.app.resource.ServicePluginLocator in project ligoj-api by ligoj.

the class NodeResourceTest method mockApplicationContext.

@SuppressWarnings("unchecked")
public void mockApplicationContext() {
    final NodeResource resource = new NodeResource();
    super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
    resource.self = resource;
    final ApplicationContext applicationContext = Mockito.mock(ApplicationContext.class);
    SpringUtils.setSharedApplicationContext(applicationContext);
    final ServicePluginLocator servicePluginLocator = Mockito.mock(ServicePluginLocator.class);
    // Replace the plug-in locator
    resource.locator = servicePluginLocator;
    Mockito.when(applicationContext.getBean(ArgumentMatchers.any(Class.class))).thenAnswer(invocation -> {
        if (invocation.getArgument(0).equals(ServicePluginLocator.class)) {
            return servicePluginLocator;
        }
        if (invocation.getArgument(0).equals(NodeResource.class)) {
            return resource;
        }
        return super.applicationContext.getBean((Class<?>) invocation.getArgument(0));
    });
    this.resourceMock = resource;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator)

Example 5 with ServicePluginLocator

use of org.ligoj.app.resource.ServicePluginLocator in project ligoj-api by ligoj.

the class NodeResourceTest method checkSubscriptionStatusException.

@Test
public void checkSubscriptionStatusException() throws Exception {
    mockApplicationContext();
    final NodeResource resource = resourceMock;
    final ServicePluginLocator servicePluginLocator = resourceMock.locator;
    // data
    final Node jiraNode = repository.findByName("JIRA 4");
    // subscription throw an exception
    final JenkinsPluginResource jenkins = Mockito.mock(JenkinsPluginResource.class);
    Mockito.when(servicePluginLocator.getResource(ArgumentMatchers.anyString(), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jenkins);
    Mockito.when(servicePluginLocator.getResourceExpected(ArgumentMatchers.anyString(), ArgumentMatchers.eq(ToolPlugin.class))).thenReturn(jenkins);
    Mockito.when(jenkins.checkSubscriptionStatus(ArgumentMatchers.anyString(), ArgumentMatchers.anyMap())).thenThrow(new TechnicalException("junit"));
    // check status
    final long eventsCount = eventRepository.count();
    resource.checkSubscriptionStatus(jiraNode, NodeStatus.UP);
    // 1 subscription
    Assertions.assertEquals(eventsCount + 1, eventRepository.count());
}
Also used : TechnicalException(org.ligoj.bootstrap.core.resource.TechnicalException) JenkinsPluginResource(org.ligoj.app.resource.node.sample.JenkinsPluginResource) DelegateNode(org.ligoj.app.model.DelegateNode) TaskSampleNode(org.ligoj.app.model.TaskSampleNode) Node(org.ligoj.app.model.Node) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator) ToolPlugin(org.ligoj.app.api.ToolPlugin) Test(org.junit.jupiter.api.Test) AbstractAppTest(org.ligoj.app.AbstractAppTest)

Aggregations

ServicePluginLocator (org.ligoj.app.resource.ServicePluginLocator)6 ToolPlugin (org.ligoj.app.api.ToolPlugin)3 JenkinsPluginResource (org.ligoj.app.resource.node.sample.JenkinsPluginResource)3 TechnicalException (org.ligoj.bootstrap.core.resource.TechnicalException)3 Test (org.junit.jupiter.api.Test)2 AbstractAppTest (org.ligoj.app.AbstractAppTest)2 Subscription (org.ligoj.app.model.Subscription)2 QuoteVo (org.ligoj.app.plugin.prov.QuoteVo)2 JiraPluginResource (org.ligoj.app.resource.node.sample.JiraPluginResource)2 SonarPluginResource (org.ligoj.app.resource.node.sample.SonarPluginResource)2 IOException (java.io.IOException)1 SubscriptionStatusWithData (org.ligoj.app.api.SubscriptionStatusWithData)1 DelegateNode (org.ligoj.app.model.DelegateNode)1 Node (org.ligoj.app.model.Node)1 TaskSampleNode (org.ligoj.app.model.TaskSampleNode)1 ProvResource (org.ligoj.app.plugin.prov.ProvResource)1 PluginsClassLoader (org.ligoj.app.resource.plugin.PluginsClassLoader)1 ApplicationContext (org.springframework.context.ApplicationContext)1