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
7 changes: 7 additions & 0 deletions plugin-kylin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<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>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* 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.kylin;

import org.apache.ranger.plugin.client.HadoopException;
import org.apache.ranger.plugin.model.RangerService;
import org.apache.ranger.plugin.model.RangerServiceDef;
import org.apache.ranger.plugin.service.ResourceLookupContext;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import java.util.Collections;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @generated by Cursor
* @description : Unit Test cases for RangerServiceKylin
*/
@TestMethodOrder(MethodOrderer.MethodName.class)
class TestRangerServiceKylin {
@Test
void test01_validateConfig_whenConfigsNull_returnsEmptyMap() {
RangerServiceKylin svc = new RangerServiceKylin();
svc.setConfigs(null);

Map<String, Object> res = svc.validateConfig();

assertNotNull(res);
assertTrue(res.isEmpty());
}

@Test
void test02_validateConfig_whenConfigsEmptyMap_propagatesHadoopException() {
RangerServiceKylin svc = new RangerServiceKylin();
svc.setServiceName("svc");
svc.setConfigs(Collections.emptyMap());

assertThrows(HadoopException.class, svc::validateConfig);
}

@Test
void test03_lookupResource_whenContextNull_returnsEmptyList() {
RangerServiceKylin svc = new RangerServiceKylin();
svc.setServiceName("svc");
svc.setConfigs(kylinClientConfigs("http://127.0.0.1:13"));

List<String> res = svc.lookupResource(null);

assertTrue(res.isEmpty());
}

@Test
void test04_lookupResource_whenConnectionConfigEmpty_returnsNull() {
RangerServiceKylin svc = new RangerServiceKylin();
svc.setServiceName("svc");
svc.setConfigs(Collections.emptyMap());
ResourceLookupContext ctx = new ResourceLookupContext();
ctx.setUserInput("any");

List<String> res = svc.lookupResource(ctx);

assertNull(res);
}

@Test
void test05_init_whenServiceDefAndServicePresent_populatesBaseStateFromService() {
RangerServiceKylin svc = new RangerServiceKylin();
RangerServiceDef def = new RangerServiceDef();
Map<String, String> cfg = new HashMap<>();
cfg.put("kylin.url", "http://localhost:7070");
RangerService rs = new RangerService("kylin", "kylin-svc", null, null, cfg);

svc.init(def, rs);

assertSame(def, svc.getServiceDef());
assertSame(rs, svc.getService());
assertEquals(cfg, svc.getConfigs());
assertEquals("kylin-svc", svc.getServiceName());
assertEquals("kylin", svc.getServiceType());
}

@Test
void test06_init_whenServiceNull_throwsNullPointerException() {
RangerServiceKylin svc = new RangerServiceKylin();

assertThrows(NullPointerException.class, () -> svc.init(new RangerServiceDef(), null));
}

@Test
void test07_lookupResource_whenGetKylinResourcesThrows_propagatesSameException() {
RangerServiceKylin svc = new RangerServiceKylin();
svc.setServiceName("svc");
svc.setConfigs(kylinClientConfigs("http://127.0.0.1:13"));
ResourceLookupContext ctx = new ResourceLookupContextThrowsOnResources();
ctx.setUserInput("q");

IllegalStateException ex =
assertThrows(IllegalStateException.class, () -> svc.lookupResource(ctx));

assertEquals("simulated resource lookup failure", ex.getMessage());
}

private static Map<String, String> kylinClientConfigs(String kylinUrl) {
Map<String, String> configs = new HashMap<>();
configs.put("kylin.url", kylinUrl);
configs.put("username", "u");
configs.put("password", "p");
configs.put("authtype", "simple");
configs.put("hadoop.security.authentication", "simple");
return configs;
}

private static final class ResourceLookupContextThrowsOnResources extends ResourceLookupContext {
@Override
public Map<String, List<String>> getResources() {
throw new IllegalStateException("simulated resource lookup failure");
}
}
}
Loading