Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests | Code Coverage improvement for DataClassification class + Fix conditional tests #1041

Merged
merged 3 commits into from
Apr 30, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand All @@ -17,13 +18,16 @@
import com.microsoft.sqlserver.jdbc.RandomUtil;
import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.jdbc.dataclassification.InformationType;
import com.microsoft.sqlserver.jdbc.dataclassification.Label;
import com.microsoft.sqlserver.jdbc.dataclassification.SensitivityProperty;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Constants;


@RunWith(JUnitPlatform.class)
@Tag(Constants.xAzureSQLDB)
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xSQLv12)
@Tag(Constants.xSQLv14)
Expand All @@ -37,13 +41,11 @@ public class DataClassificationTest extends AbstractTest {
*/
@Test
public void testDataClassificationMetadata() throws Exception {
// Run this test only with newer SQL Servers (version>=2018) that support Data Classification
try (Statement stmt = connection.createStatement();) {
if (TestUtils.serverSupportsDataClassification(stmt)) {
createTable(connection, stmt);
runTestsForServer(stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
}
assert (TestUtils.serverSupportsDataClassification(stmt));
createTable(connection, stmt);
runTestsForServer(stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
}
}

Expand Down Expand Up @@ -119,7 +121,7 @@ private void runTestsForServer(Statement stmt) throws Exception {
}

/**
* Verifies resultset received to contain data classification information as set.
* Verifies ResultSet received to contain data classification information as set.
*
* @param rs
* @throws SQLException
Expand All @@ -130,20 +132,36 @@ private void verifySensitivityClassification(SQLServerResultSet rs) throws SQLEx
columnPos++) {
for (SensitivityProperty sp : rs.getSensitivityClassification().getColumnSensitivities().get(columnPos)
.getSensitivityProperties()) {

List<InformationType> infoTypes = rs.getSensitivityClassification().getInformationTypes();
assert (infoTypes.size() == 3);
for (int i = 0; i < infoTypes.size(); i++) {
verifyInfoType(infoTypes.get(i), i + 1);
}

List<Label> labels = rs.getSensitivityClassification().getLabels();
assert (labels.size() == 1);
verifyLabel(labels.get(0));

if (columnPos == 1 || columnPos == 2 || columnPos == 6 || columnPos == 7) {
assert (sp.getLabel() != null);
assert (sp.getLabel().getId().equalsIgnoreCase("L1"));
assert (sp.getLabel().getName().equalsIgnoreCase("PII"));

assert (sp.getInformationType() != null);
assert (sp.getInformationType().getId()
.equalsIgnoreCase(columnPos == 1 ? "COMPANY" : (columnPos == 2 ? "NAME" : "CONTACT")));
assert (sp.getInformationType().getName().equalsIgnoreCase(
columnPos == 1 ? "Company name"
: (columnPos == 2 ? "Person Name" : "Contact Information")));
verifyLabel(sp.getLabel());
verifyInfoType(sp.getInformationType(), columnPos);
}
}
}
}
}

private void verifyInfoType(InformationType informationType, int i) {
assert (informationType != null);
assert (informationType.getId().equalsIgnoreCase(i == 1 ? "COMPANY" : (i == 2 ? "NAME" : "CONTACT")));
assert (informationType.getName()
.equalsIgnoreCase(i == 1 ? "Company name" : (i == 2 ? "Person Name" : "Contact Information")));
}

private void verifyLabel(Label label) {
assert (label != null);
assert (label.getId().equalsIgnoreCase("L1"));
assert (label.getName().equalsIgnoreCase("PII"));
}
}
109 changes: 51 additions & 58 deletions src/test/java/com/microsoft/sqlserver/jdbc/unit/UTF8SupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,27 @@ public class UTF8SupportTest extends AbstractTest {
*/
@Test
public void testChar() throws SQLException {
if (TestUtils.serverSupportsUTF8(connection)) {
createTable("char(10)");
validate("teststring");
// This is 10 UTF-8 bytes. D1 82 D0 B5 D1 81 D1 82 31 32
validate("тест12");
// E2 95 A1 E2 95 A4 E2 88 9E 2D
validate("╡╤∞-");

createTable("char(4000)");
validate(String.join("", Collections.nCopies(400, "teststring")));
validate(String.join("", Collections.nCopies(400, "тест12")));
validate(String.join("", Collections.nCopies(400, "╡╤∞-")));

createTable("char(4001)");
validate(String.join("", Collections.nCopies(400, "teststring")) + "1");
validate(String.join("", Collections.nCopies(400, "тест12")) + "1");
validate(String.join("", Collections.nCopies(400, "╡╤∞-")) + "1");

createTable("char(8000)");
validate(String.join("", Collections.nCopies(800, "teststring")));
validate(String.join("", Collections.nCopies(800, "тест12")));
validate(String.join("", Collections.nCopies(800, "╡╤∞-")));
}
createTable("char(10)");
validate("teststring");
// This is 10 UTF-8 bytes. D1 82 D0 B5 D1 81 D1 82 31 32
validate("тест12");
// E2 95 A1 E2 95 A4 E2 88 9E 2D
validate("╡╤∞-");

createTable("char(4000)");
validate(String.join("", Collections.nCopies(400, "teststring")));
validate(String.join("", Collections.nCopies(400, "тест12")));
validate(String.join("", Collections.nCopies(400, "╡╤∞-")));

createTable("char(4001)");
validate(String.join("", Collections.nCopies(400, "teststring")) + "1");
validate(String.join("", Collections.nCopies(400, "тест12")) + "1");
validate(String.join("", Collections.nCopies(400, "╡╤∞-")) + "1");

createTable("char(8000)");
validate(String.join("", Collections.nCopies(800, "teststring")));
validate(String.join("", Collections.nCopies(800, "тест12")));
validate(String.join("", Collections.nCopies(800, "╡╤∞-")));
}

/**
Expand All @@ -80,50 +78,45 @@ public void testChar() throws SQLException {
*/
@Test
public void testVarchar() throws SQLException {
if (TestUtils.serverSupportsUTF8(connection)) {
createTable("varchar(10)");
validate("teststring");
validate("тест12");
validate("╡╤∞-");

createTable("varchar(4000)");
validate(String.join("", Collections.nCopies(400, "teststring")));
validate(String.join("", Collections.nCopies(400, "тест12")));
validate(String.join("", Collections.nCopies(400, "╡╤∞-")));

createTable("varchar(4001)");
validate(String.join("", Collections.nCopies(400, "teststring")) + "1");
validate(String.join("", Collections.nCopies(400, "тест12")) + "1");
validate(String.join("", Collections.nCopies(400, "╡╤∞-")) + "1");

createTable("varchar(8000)");
validate(String.join("", Collections.nCopies(800, "teststring")));
validate(String.join("", Collections.nCopies(800, "тест12")));
validate(String.join("", Collections.nCopies(800, "╡╤∞-")));

createTable("varchar(MAX)");
validate(String.join("", Collections.nCopies(800, "teststring")));
validate(String.join("", Collections.nCopies(800, "тест12")));
validate(String.join("", Collections.nCopies(800, "╡╤∞-")));
}
createTable("varchar(10)");
validate("teststring");
validate("тест12");
validate("╡╤∞-");

createTable("varchar(4000)");
validate(String.join("", Collections.nCopies(400, "teststring")));
validate(String.join("", Collections.nCopies(400, "тест12")));
validate(String.join("", Collections.nCopies(400, "╡╤∞-")));

createTable("varchar(4001)");
validate(String.join("", Collections.nCopies(400, "teststring")) + "1");
validate(String.join("", Collections.nCopies(400, "тест12")) + "1");
validate(String.join("", Collections.nCopies(400, "╡╤∞-")) + "1");

createTable("varchar(8000)");
validate(String.join("", Collections.nCopies(800, "teststring")));
validate(String.join("", Collections.nCopies(800, "тест12")));
validate(String.join("", Collections.nCopies(800, "╡╤∞-")));

createTable("varchar(MAX)");
validate(String.join("", Collections.nCopies(800, "teststring")));
validate(String.join("", Collections.nCopies(800, "тест12")));
validate(String.join("", Collections.nCopies(800, "╡╤∞-")));
}

@BeforeAll
public static void setUp() throws ClassNotFoundException, SQLException {
if (TestUtils.serverSupportsUTF8(connection)) {
databaseName = RandomUtil.getIdentifier("UTF8Database");
tableName = RandomUtil.getIdentifier("UTF8Table");
createDatabaseWithUTF8Collation();
connection.setCatalog(databaseName);
}
assert (TestUtils.serverSupportsUTF8(connection));
databaseName = RandomUtil.getIdentifier("UTF8Database");
tableName = RandomUtil.getIdentifier("UTF8Table");
createDatabaseWithUTF8Collation();
connection.setCatalog(databaseName);
}

@AfterAll
public static void cleanUp() throws SQLException {
try (Statement stmt = connection.createStatement()) {
if (TestUtils.serverSupportsUTF8(connection)) {
TestUtils.dropDatabaseIfExists(databaseName, stmt);
}
TestUtils.dropDatabaseIfExists(databaseName, stmt);
}
}

Expand Down