SQOOP-3309: Implement HiveServer2 client
[sqoop.git] / src / test / org / apache / sqoop / hive / TestHiveMiniCluster.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.sqoop.hive;
20
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.hadoop.security.UserGroupInformation;
23 import org.apache.sqoop.db.JdbcConnectionFactory;
24 import org.apache.sqoop.hive.minicluster.AuthenticationConfiguration;
25 import org.apache.sqoop.hive.minicluster.HiveMiniCluster;
26 import org.apache.sqoop.hive.minicluster.KerberosAuthenticationConfiguration;
27 import org.apache.sqoop.hive.minicluster.NoAuthenticationConfiguration;
28 import org.apache.sqoop.hive.minicluster.PasswordAuthenticationConfiguration;
29 import org.apache.sqoop.infrastructure.kerberos.MiniKdcInfrastructureRule;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.ClassRule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.Parameterized;
36 import org.junit.runners.Parameterized.Parameters;
37
38 import java.sql.Connection;
39 import java.sql.PreparedStatement;
40 import java.sql.ResultSet;
41 import java.sql.SQLException;
42 import java.util.Arrays;
43
44 import static org.junit.Assert.assertEquals;
45
46 @RunWith(Parameterized.class)
47 public class TestHiveMiniCluster {
48
49 @ClassRule
50 public static MiniKdcInfrastructureRule miniKdcInfrastructure = new MiniKdcInfrastructureRule();
51
52 private static final String TEST_USERNAME = "sqoop";
53
54 private static final String TEST_PASSWORD = "secret";
55
56 @Parameters(name = "config = {0}")
57 public static Iterable<? extends Object> authenticationParameters() {
58 return Arrays.asList(new NoAuthenticationConfiguration(),
59 new PasswordAuthenticationConfiguration(TEST_USERNAME, TEST_PASSWORD),
60 new KerberosAuthenticationConfiguration(miniKdcInfrastructure));
61 }
62
63 private static final String CREATE_TABLE_SQL = "CREATE TABLE TestTable (id int)";
64
65 private static final String INSERT_SQL = "INSERT INTO TestTable VALUES (?)";
66
67 private static final String SELECT_SQL = "SELECT * FROM TestTable";
68
69 private static final int TEST_VALUE = 10;
70
71 private final AuthenticationConfiguration authenticationConfiguration;
72
73 private HiveMiniCluster hiveMiniCluster;
74
75 private JdbcConnectionFactory connectionFactory;
76
77 public TestHiveMiniCluster(AuthenticationConfiguration authenticationConfiguration) {
78 this.authenticationConfiguration = authenticationConfiguration;
79 }
80
81 @Before
82 public void before() throws SQLException {
83 hiveMiniCluster = new HiveMiniCluster(authenticationConfiguration);
84 hiveMiniCluster.start();
85
86 connectionFactory = authenticationConfiguration.decorateConnectionFactory(new HiveServer2ConnectionFactory(hiveMiniCluster.getUrl(), TEST_USERNAME, TEST_PASSWORD));
87 }
88
89 @Test
90 public void testInsertedRowCanBeReadFromTable() throws Exception {
91 createTestTable();
92 insertRowIntoTestTable();
93
94 assertEquals(TEST_VALUE, getDataFromTestTable());
95 }
96
97 private void insertRowIntoTestTable() throws SQLException {
98 try (Connection conn = connectionFactory.createConnection(); PreparedStatement stmnt = conn.prepareStatement(INSERT_SQL)) {
99 stmnt.setInt(1, TEST_VALUE);
100 stmnt.executeUpdate();
101 }
102 }
103
104 private int getDataFromTestTable() throws SQLException {
105 try (Connection conn = connectionFactory.createConnection(); PreparedStatement stmnt = conn.prepareStatement(SELECT_SQL)) {
106 ResultSet resultSet = stmnt.executeQuery();
107 resultSet.next();
108 return resultSet.getInt(1);
109 }
110 }
111
112 private void createTestTable() throws SQLException {
113 try (Connection conn = connectionFactory.createConnection(); PreparedStatement stmnt = conn.prepareStatement(CREATE_TABLE_SQL)) {
114 stmnt.executeUpdate();
115 }
116 }
117
118 @After
119 public void after() {
120 hiveMiniCluster.stop();
121 UserGroupInformation.setConfiguration(new Configuration());
122 }
123
124 }