2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.common.spring.utils;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import org.hibernate.boot.model.naming.Identifier;
27 import org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource;
28 import org.hibernate.boot.model.relational.Database;
29 import org.hibernate.boot.spi.InFlightMetadataCollector;
30 import org.hibernate.boot.spi.MetadataBuildingContext;
31 import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
32 import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.Mock;
37 class CustomImplicitNamingStrategyTest {
39 static CustomImplicitNamingStrategy strategy;
42 static ImplicitJoinColumnNameSource source;
45 public static void setUpBeforeClass() {
46 strategy = new CustomImplicitNamingStrategy();
47 source = mock(ImplicitJoinColumnNameSource.class);
51 void testDetermineJoinColumnName() {
52 Identifier identifier = new Identifier("identifier", true);
54 MetadataBuildingContext buildingContextMock = mock(MetadataBuildingContext.class);
55 InFlightMetadataCollector flightCollectorMock = mock(InFlightMetadataCollector.class);
56 Database databaseMock = mock(Database.class);
58 when(flightCollectorMock.getDatabase()).thenReturn(databaseMock);
59 when(source.getReferencedColumnName()).thenReturn(identifier);
60 when(source.getBuildingContext()).thenReturn(buildingContextMock);
61 when(buildingContextMock.getMetadataCollector()).thenReturn(flightCollectorMock);
63 JdbcEnvironment environmentMock = mock(JdbcEnvironment.class);
64 when(databaseMock.getJdbcEnvironment()).thenReturn(environmentMock);
66 IdentifierHelper helperMock = mock(IdentifierHelper.class);
67 when(environmentMock.getIdentifierHelper()).thenReturn(helperMock);
68 when(helperMock.toIdentifier(anyString())).thenReturn(identifier);
70 Identifier result = strategy.determineJoinColumnName(source);
71 assertEquals(identifier, result);