Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugin-nifi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,11 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.ranger.services.nifi;

import org.apache.ranger.plugin.service.ResourceLookupContext;
import org.apache.ranger.services.nifi.client.NiFiClient;
import org.apache.ranger.services.nifi.client.NiFiConfigs;
import org.apache.ranger.services.nifi.client.NiFiConnectionMgr;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
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.ArgumentMatchers.anyString;

/**
* @generated by Cursor
* @description : Unit Test cases for RangerServiceNiFi
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
class TestRangerServiceNiFi {
@Test
void validateConfig_throwsWhenConfigsNull() {
RangerServiceNiFi service = new RangerServiceNiFi();
service.setConfigs(null);

assertThrows(IllegalStateException.class, service::validateConfig);
}

@Test
void validateConfig_propagatesWhenConnectionTestThrows() {
RangerServiceNiFi service = new RangerServiceNiFi();
service.setServiceName("nifi-svc");
Map<String, String> configs = new HashMap<>();
configs.put(NiFiConfigs.NIFI_URL, "http://localhost:8080/nifi-api/resources");
configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, "NONE");
service.setConfigs(configs);

try (MockedStatic<NiFiConnectionMgr> mgr = Mockito.mockStatic(NiFiConnectionMgr.class)) {
mgr.when(() -> NiFiConnectionMgr.connectionTest(anyString(), any()))
.thenThrow(new RuntimeException("connection test failure"));

assertThrows(RuntimeException.class, service::validateConfig);
}
}

@Test
void lookupResource_returnsResourcesFromClient() throws Exception {
RangerServiceNiFi service = new RangerServiceNiFi();
service.setServiceName("nifi-svc");
Map<String, String> configs = new HashMap<>();
configs.put(NiFiConfigs.NIFI_URL, "http://localhost:8080/nifi-api/resources");
configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, "NONE");
service.setConfigs(configs);

ResourceLookupContext context = Mockito.mock(ResourceLookupContext.class);
NiFiClient client = Mockito.mock(NiFiClient.class);
Mockito.when(client.getResources(context)).thenReturn(List.of("/flow", "/system"));

try (MockedStatic<NiFiConnectionMgr> mgr = Mockito.mockStatic(NiFiConnectionMgr.class)) {
mgr.when(() -> NiFiConnectionMgr.getNiFiClient("nifi-svc", configs)).thenReturn(client);

List<String> resources = service.lookupResource(context);

assertEquals(2, resources.size());
assertTrue(resources.contains("/flow"));
}
}

@Test
void lookupResource_propagatesExceptionFromClient() throws Exception {
RangerServiceNiFi service = new RangerServiceNiFi();
service.setServiceName("nifi-svc");
Map<String, String> configs = new HashMap<>();
configs.put(NiFiConfigs.NIFI_URL, "http://localhost:8080/nifi-api/resources");
configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, "NONE");
service.setConfigs(configs);

ResourceLookupContext context = Mockito.mock(ResourceLookupContext.class);
NiFiClient client = Mockito.mock(NiFiClient.class);
Mockito.when(client.getResources(context)).thenThrow(new Exception("lookup failed"));

try (MockedStatic<NiFiConnectionMgr> mgr = Mockito.mockStatic(NiFiConnectionMgr.class)) {
mgr.when(() -> NiFiConnectionMgr.getNiFiClient("nifi-svc", configs)).thenReturn(client);

Exception ex = assertThrows(Exception.class, () -> service.lookupResource(context));
assertTrue(ex.getMessage().contains("lookup failed"));
}
}

@Test
void validateConfig_successPathWhenConnectionTestReturnsSuccessMap() {
RangerServiceNiFi service = new RangerServiceNiFi();
service.setServiceName("nifi-svc");
Map<String, String> configs = new HashMap<>();
configs.put(NiFiConfigs.NIFI_URL, "http://localhost:8080/nifi-api/resources");
configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, "NONE");
service.setConfigs(configs);

HashMap<String, Object> expected = new HashMap<>();
expected.put("connectivityStatus", true);
expected.put("message", "ok");

try (MockedStatic<NiFiConnectionMgr> mgr = Mockito.mockStatic(NiFiConnectionMgr.class)) {
mgr.when(() -> NiFiConnectionMgr.connectionTest(anyString(), any())).thenReturn(expected);

Map<String, Object> result = service.validateConfig();

assertTrue((Boolean) result.get("connectivityStatus"));
assertEquals("ok", result.get("message"));
}
}

@Test
void validateConfig_returnsFailureMapWhenConnectionTestReturnsFailureWithoutThrowing() {
RangerServiceNiFi service = new RangerServiceNiFi();
service.setServiceName("nifi-svc");
Map<String, String> configs = new HashMap<>();
configs.put(NiFiConfigs.NIFI_URL, "http://localhost:8080/nifi-api/resources");
configs.put(NiFiConfigs.NIFI_AUTHENTICATION_TYPE, "NONE");
service.setConfigs(configs);

HashMap<String, Object> expected = new HashMap<>();
expected.put("connectivityStatus", false);
expected.put("message", "Error creating NiFi client");

try (MockedStatic<NiFiConnectionMgr> mgr = Mockito.mockStatic(NiFiConnectionMgr.class)) {
mgr.when(() -> NiFiConnectionMgr.connectionTest(anyString(), any())).thenReturn(expected);

Map<String, Object> result = service.validateConfig();

assertFalse((Boolean) result.get("connectivityStatus"));
assertEquals("Error creating NiFi client", result.get("message"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.ranger.services.nifi.client;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* @generated by Cursor
* @description : Unit Test cases for NiFiAuthType
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
class TestNiFiAuthType {
@Test
void valueOfReturnsConstantForValidName() {
assertEquals(NiFiAuthType.NONE, NiFiAuthType.valueOf("NONE"));
assertEquals(NiFiAuthType.SSL, NiFiAuthType.valueOf("SSL"));
}

@Test
void valueOfThrowsForInvalidName() {
assertThrows(IllegalArgumentException.class, () -> NiFiAuthType.valueOf("UNKNOWN"));
}
}
Loading