e7c3ad5d72c08137cd537f75edd2ddd460216053
[policy/clamp.git] /
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.common.spring.utils;
20
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;
25
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;
36
37 class CustomImplicitNamingStrategyTest {
38
39     static CustomImplicitNamingStrategy strategy;
40
41     @Mock
42     static ImplicitJoinColumnNameSource source;
43
44     @BeforeAll
45     public static void setUpBeforeClass() {
46         strategy = new CustomImplicitNamingStrategy();
47         source = mock(ImplicitJoinColumnNameSource.class);
48     }
49
50     @Test
51     void testDetermineJoinColumnName() {
52         Identifier identifier = new Identifier("identifier", true);
53
54         MetadataBuildingContext buildingContextMock = mock(MetadataBuildingContext.class);
55         InFlightMetadataCollector flightCollectorMock = mock(InFlightMetadataCollector.class);
56         Database databaseMock = mock(Database.class);
57
58         when(flightCollectorMock.getDatabase()).thenReturn(databaseMock);
59         when(source.getReferencedColumnName()).thenReturn(identifier);
60         when(source.getBuildingContext()).thenReturn(buildingContextMock);
61         when(buildingContextMock.getMetadataCollector()).thenReturn(flightCollectorMock);
62
63         JdbcEnvironment environmentMock = mock(JdbcEnvironment.class);
64         when(databaseMock.getJdbcEnvironment()).thenReturn(environmentMock);
65
66         IdentifierHelper helperMock = mock(IdentifierHelper.class);
67         when(environmentMock.getIdentifierHelper()).thenReturn(helperMock);
68         when(helperMock.toIdentifier(anyString())).thenReturn(identifier);
69
70         Identifier result = strategy.determineJoinColumnName(source);
71         assertEquals(identifier, result);
72     }
73
74 }