Skip to content

Commit

Permalink
Update test case and remove Dependency of Jupiter (#39)
Browse files Browse the repository at this point in the history
* Update test case  and remove Dependecy of jupiter

* Added UT fixes
  • Loading branch information
pawankashyapollion authored and taherkl committed Jan 2, 2025
1 parent cf0f5ac commit ed49ccc
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 167 deletions.
6 changes: 0 additions & 6 deletions v2/spanner-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.teleport.v2.spanner.migrations.metadata;

import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
Expand All @@ -23,13 +24,15 @@
import com.datastax.oss.driver.api.core.cql.Row;
import com.google.cloud.teleport.v2.spanner.migrations.schema.Schema;
import java.util.Arrays;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

class CassandraSourceMetadataTest {
@RunWith(JUnit4.class)
public class CassandraSourceMetadataTest {

@Mock private ResultSet mockResultSet;
@Mock private Row mockRow1;
Expand All @@ -38,24 +41,24 @@ class CassandraSourceMetadataTest {

private CassandraSourceMetadata.Builder builder;

@BeforeEach
void setUp() {
@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
builder = new CassandraSourceMetadata.Builder();
}

@Test
void testBuilderSetSchemaAndResultSet() {
public void testBuilderSetSchemaAndResultSet() {
CassandraSourceMetadata metadata =
builder.setResultSet(mockResultSet).setSchema(mockSchema).build();
Assertions.assertNotNull(metadata, "CassandraSourceMetadata should not be null");
assertNotNull("CassandraSourceMetadata should not be null", metadata);
}

@Test
void testGenerateSourceSchema() {
public void testGenerateSourceSchema() {
doAnswer(
invocation -> {
Iterable<?> iterable = Arrays.asList(mockRow1, mockRow2);
Iterable<Row> iterable = Arrays.asList(mockRow1, mockRow2);
iterable.forEach(invocation.getArgument(0));
return null;
})
Expand All @@ -74,5 +77,7 @@ void testGenerateSourceSchema() {

CassandraSourceMetadata metadata =
builder.setResultSet(mockResultSet).setSchema(mockSchema).build();

assertNotNull("Metadata should be generated successfully", metadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import java.util.Map;
import java.util.NoSuchElementException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SchemaTest {

@Test
Expand All @@ -40,6 +43,17 @@ public void verifyTableInSessionTestCorrect() {
}
}

@Test
public void verifyTableInSessionTestAndPopulationCorrect() {
Schema schema = getAllSchemaObject();
schema.generateMappings();
try {
schema.verifyTableInSession("cart");
} catch (Exception e) {
fail("No exception should have been thrown for this case");
}
}

@Test(expected = IllegalArgumentException.class)
public void verifyTableInSessionTestMissingSrcTable() throws Exception {
Schema schema = getSchemaObject();
Expand Down Expand Up @@ -95,6 +109,21 @@ private static Schema getSchemaObject() {
return expectedSchema;
}

private static Schema getAllSchemaObject() {
// Add SrcSchema.
Map<String, SourceTable> srcSchema = getSampleSrcSchema();
// Add SpSchema.
Map<String, SpannerTable> spSchema = getSampleSpSchema();
Schema expectedSchema = new Schema();
expectedSchema.setSrcSchema(srcSchema);
expectedSchema.setSpSchema(spSchema);
expectedSchema.setToSpanner(new HashMap<String, NameAndCols>());
expectedSchema.setToSource(new HashMap<String, NameAndCols>());
expectedSchema.setSrcToID(new HashMap<String, NameAndCols>());
expectedSchema.setSpannerToID(new HashMap<String, NameAndCols>());
return expectedSchema;
}

private static Map<String, SyntheticPKey> getSyntheticPks() {
Map<String, SyntheticPKey> syntheticPKeys = new HashMap<String, SyntheticPKey>();
syntheticPKeys.put("t2", new SyntheticPKey("c6", 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,74 @@
*/
package com.google.cloud.teleport.v2.spanner.migrations.shard;

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

import com.datastax.oss.driver.api.core.config.OptionsMap;
import com.datastax.oss.driver.api.core.config.TypedDriverOption;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

@RunWith(JUnit4.class)
public class CassandraShardTest {

private OptionsMap optionsMap;
private List<String> contactPoints;

@BeforeEach
@Before
public void setUp() {
contactPoints = List.of("127.0.0.1:9042");
optionsMap = Mockito.mock(OptionsMap.class);
contactPoints = List.of("127.0.0.1:9042");

Mockito.when(optionsMap.get(TypedDriverOption.CONTACT_POINTS)).thenReturn(contactPoints);
Mockito.when(optionsMap.get(TypedDriverOption.SESSION_KEYSPACE)).thenReturn("test_keyspace");
}

@Test
public void testConstructor_Valid() {
CassandraShard shard = new CassandraShard(optionsMap);
assertNotNull(shard);
assertEquals("test_keyspace", shard.getKeySpaceName());
assertEquals(contactPoints, shard.getContactPoints());
assertNotNull("CassandraShard should be created successfully", shard);
assertEquals("Keyspace name should match", "test_keyspace", shard.getKeySpaceName());
assertEquals("Contact points should match", contactPoints, shard.getContactPoints());
}

@Test
public void testConstructor_InvalidContactPoints() {
Mockito.when(optionsMap.get(TypedDriverOption.CONTACT_POINTS)).thenReturn(null);
assertThrows(IllegalArgumentException.class, () -> new CassandraShard(optionsMap));
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> new CassandraShard(optionsMap));
assertEquals("CONTACT_POINTS cannot be null or empty.", exception.getMessage());
}

@Test
public void testConstructor_InvalidKeySpace() {
Mockito.when(optionsMap.get(TypedDriverOption.SESSION_KEYSPACE)).thenReturn(null);
assertThrows(IllegalArgumentException.class, () -> new CassandraShard(optionsMap));
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> new CassandraShard(optionsMap));
assertEquals("SESSION_KEYSPACE cannot be null or empty.", exception.getMessage());
}

@Test
public void testExtractAndSetHostAndPort_Valid() {
CassandraShard shard = new CassandraShard(optionsMap);
assertEquals("127.0.0.1", shard.getHost());
assertEquals("9042", shard.getPort());
assertEquals("Host should be extracted correctly", "127.0.0.1", shard.getHost());
assertEquals("Port should be extracted correctly", "9042", shard.getPort());
}

@Test
public void testGetters() {
CassandraShard shard = new CassandraShard(optionsMap);
assertEquals(contactPoints, shard.getContactPoints());
assertEquals("test_keyspace", shard.getKeySpaceName());
assertEquals(
"Contact points getter should return correct value",
contactPoints,
shard.getContactPoints());
assertEquals(
"Keyspace getter should return correct value", "test_keyspace", shard.getKeySpaceName());
}

@Test
Expand All @@ -80,14 +92,15 @@ public void testToString() {
String.format(
"CassandraShard{logicalShardId='%s', contactPoints=%s, keyspace='%s', host='%s', port='%s'}",
shard.getLogicalShardId(), contactPoints, "test_keyspace", "127.0.0.1", "9042");
assertEquals(expected, shard.toString());
assertEquals("toString should return the expected representation", expected, shard.toString());
}

@Test
public void testEqualsAndHashCode_Equal() {
CassandraShard shard1 = new CassandraShard(optionsMap);
CassandraShard shard2 = new CassandraShard(optionsMap);
assertEquals(shard1, shard2);
assertEquals(shard1.hashCode(), shard2.hashCode());
assertEquals("Equal shards should be considered equal", shard1, shard2);
assertEquals(
"Equal shards should have the same hash code", shard1.hashCode(), shard2.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2025 Google LLC
*
* Licensed 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 com.google.cloud.teleport.v2.spanner.migrations.spanner;

import com.google.cloud.teleport.v2.spanner.ddl.Ddl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class SpannerSchemaTest {

@Test
public void testSpannerSchemaPopulation() throws Exception {
Ddl ddl =
Ddl.builder()
.createTable("Users")
.column("id")
.int64()
.notNull()
.endColumn()
.column("first_name")
.string()
.size(10)
.endColumn()
.column("last_name")
.type(com.google.cloud.teleport.v2.spanner.type.Type.string())
.max()
.endColumn()
.primaryKey()
.asc("id")
.end()
.endTable()
.createTable("Account")
.column("id")
.int64()
.notNull()
.endColumn()
.column("balanceId")
.int64()
.notNull()
.endColumn()
.column("balance")
.float64()
.notNull()
.endColumn()
.primaryKey()
.asc("id")
.end()
.interleaveInParent("Users")
.onDeleteCascade()
.endTable()
.build();
SpannerSchema.convertDDLTableToSpannerTable(ddl.allTables());
SpannerSchema.convertDDLTableToSpannerNameAndColsTable(ddl.allTables());
}
}
Loading

0 comments on commit ed49ccc

Please sign in to comment.