Skip to content

Commit d229538

Browse files
authored
Merge pull request #867 from simonladen/FISH-12118-unit-test-azure
FISH-12118 unit test azure
2 parents 61b75f8 + 0e18abb commit d229538

File tree

7 files changed

+621
-0
lines changed

7 files changed

+621
-0
lines changed

AzureServiceBus/AzureSBJCAAPI/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,32 @@
2323
<artifactId>azure-management</artifactId>
2424
<version>0.8.0</version>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-api</artifactId>
29+
<version>5.13.4</version>
30+
<scope>test</scope>
31+
<type>jar</type>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.mockito</groupId>
35+
<artifactId>mockito-junit-jupiter</artifactId>
36+
<version>5.20.0</version>
37+
<scope>test</scope>
38+
</dependency>
2639
</dependencies>
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-surefire-plugin</artifactId>
45+
<version>3.2.5</version>
46+
<configuration>
47+
<includes>
48+
<include>**/*Test.java</include>
49+
</includes>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
2754
</project>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2025 Payara Foundation and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package fish.payara.cloud.connectors.azuresb.api.inbound;
41+
42+
import jakarta.resource.spi.InvalidPropertyException;
43+
import jakarta.resource.spi.ResourceAdapter;
44+
import org.junit.jupiter.api.Test;
45+
46+
import static org.junit.jupiter.api.Assertions.*;
47+
import static org.mockito.Mockito.*;
48+
49+
class AzureSBActivationSpecTest {
50+
51+
@Test
52+
void testSettersAndGetters() {
53+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
54+
spec.setQueueName("queue1");
55+
spec.setSasKey("sas-key-value");
56+
spec.setSasKeyName("sas-key-name");
57+
spec.setNameSpace("test-namespace");
58+
assertEquals("queue1", spec.getQueueName());
59+
assertEquals("sas-key-value", spec.getSasKey());
60+
assertEquals("sas-key-name", spec.getSasKeyName());
61+
assertEquals("test-namespace", spec.getNameSpace());
62+
}
63+
64+
@Test
65+
void testValidateThrowsIfQueueNameNull() {
66+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
67+
spec.setSasKey("sas-key-value");
68+
spec.setSasKeyName("sas-key-name");
69+
spec.setNameSpace("test-namespace");
70+
assertThrows(InvalidPropertyException.class, spec::validate);
71+
}
72+
73+
@Test
74+
void testValidateThrowsIfSasKeyNull() {
75+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
76+
spec.setQueueName("queue1");
77+
spec.setSasKeyName("sas-key-name");
78+
spec.setNameSpace("test-namespace");
79+
assertThrows(InvalidPropertyException.class, spec::validate);
80+
}
81+
82+
@Test
83+
void testValidateThrowsIfSasKeyNameNull() {
84+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
85+
spec.setQueueName("queue1");
86+
spec.setSasKey("sas-key-value");
87+
spec.setNameSpace("test-namespace");
88+
assertThrows(InvalidPropertyException.class, spec::validate);
89+
}
90+
91+
@Test
92+
void testValidateThrowsIfNamespaceNull() {
93+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
94+
spec.setQueueName("queue1");
95+
spec.setSasKey("sas-key-value");
96+
spec.setSasKeyName("sas-key-name");
97+
assertThrows(InvalidPropertyException.class, spec::validate);
98+
}
99+
100+
@Test
101+
void testValidateSucceedsIfAllSet() {
102+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
103+
spec.setQueueName("queue1");
104+
spec.setSasKey("sas-key-value");
105+
spec.setSasKeyName("sas-key-name");
106+
spec.setNameSpace("test-namespace");
107+
assertDoesNotThrow(spec::validate);
108+
}
109+
110+
@Test
111+
void testResourceAdapterSetAndGet() throws Exception {
112+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
113+
ResourceAdapter adapter = mock(ResourceAdapter.class);
114+
spec.setResourceAdapter(adapter);
115+
assertEquals(adapter, spec.getResourceAdapter());
116+
}
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2025 Payara Foundation and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package fish.payara.cloud.connectors.azuresb.api.inbound;
41+
42+
import jakarta.resource.ResourceException;
43+
import jakarta.resource.spi.ActivationSpec;
44+
import jakarta.resource.spi.BootstrapContext;
45+
import jakarta.resource.spi.ResourceAdapterInternalException;
46+
import jakarta.resource.spi.endpoint.MessageEndpointFactory;
47+
import java.util.Timer;
48+
import javax.transaction.xa.XAResource;
49+
import org.junit.jupiter.api.BeforeEach;
50+
import org.junit.jupiter.api.Test;
51+
52+
import static org.junit.jupiter.api.Assertions.*;
53+
import org.junit.jupiter.api.Disabled;
54+
import static org.mockito.Mockito.*;
55+
56+
class AzureSBResourceAdapterTest {
57+
58+
private AzureSBResourceAdapter adapter;
59+
private BootstrapContext bootstrapContext;
60+
private MessageEndpointFactory endpointFactory;
61+
private ActivationSpec activationSpec;
62+
private AzureSBListener listener;
63+
private Timer poller;
64+
65+
@BeforeEach
66+
void setUp() throws ResourceAdapterInternalException, ResourceException {
67+
adapter = new AzureSBResourceAdapter();
68+
bootstrapContext = mock(BootstrapContext.class);
69+
endpointFactory = mock(MessageEndpointFactory.class);
70+
activationSpec = mock(ActivationSpec.class);
71+
listener = mock(AzureSBListener.class);
72+
poller = mock(Timer.class);
73+
74+
when(endpointFactory.getEndpointClass()).thenAnswer(invocation -> Object.class);
75+
when(bootstrapContext.getWorkManager()).thenReturn(mock(jakarta.resource.spi.work.WorkManager.class));
76+
adapter.start(bootstrapContext);
77+
}
78+
79+
@Test
80+
void testStartSetsContext() throws Exception {
81+
var contextField = AzureSBResourceAdapter.class.getDeclaredField("context");
82+
contextField.setAccessible(true);
83+
assertEquals(bootstrapContext, contextField.get(adapter));
84+
}
85+
86+
@Disabled("Cannot mock poller.cancel() in AzureSBResourceAdapter")
87+
@Test
88+
void testStopDoesNotThrow() {
89+
assertDoesNotThrow(() -> adapter.stop());
90+
}
91+
92+
@Disabled("Cannot mock sbListener.subscribe() in AzureSBResourceAdapter")
93+
@Test
94+
void testEndpointActivationAndDeactivation() {
95+
AzureSBActivationSpec spec = new AzureSBActivationSpec();
96+
spec.setQueueName("queue1");
97+
spec.setSasKey("sas-key-value");
98+
spec.setSasKeyName("sas-key-name");
99+
spec.setNameSpace("test-namespace");
100+
assertDoesNotThrow(() -> adapter.endpointActivation(endpointFactory, spec));
101+
assertDoesNotThrow(() -> adapter.endpointDeactivation(endpointFactory, spec));
102+
}
103+
104+
@Test
105+
void testGetXAResourcesReturnsNull() throws Exception {
106+
XAResource[] result = adapter.getXAResources(new ActivationSpec[0]);
107+
assertNull(result);
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2025 Payara Foundation and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package fish.payara.cloud.connectors.azuresb.api.outbound;
41+
42+
import fish.payara.cloud.connectors.azuresb.api.AzureSBConnection;
43+
import jakarta.resource.ResourceException;
44+
import jakarta.resource.spi.ConnectionManager;
45+
import org.junit.jupiter.api.BeforeEach;
46+
import org.junit.jupiter.api.Test;
47+
48+
import static org.junit.jupiter.api.Assertions.*;
49+
import static org.mockito.Mockito.*;
50+
51+
class AzureSBConnectionFactoryImplTest {
52+
53+
private ConnectionManager connectionManager;
54+
private AzureSBManagedConnectionFactory managedConnectionFactory;
55+
private AzureSBConnectionFactoryImpl factoryImpl;
56+
57+
@BeforeEach
58+
void setUp() {
59+
connectionManager = mock(ConnectionManager.class);
60+
managedConnectionFactory = mock(AzureSBManagedConnectionFactory.class);
61+
}
62+
63+
@Test
64+
void testGetConnectionWithConnectionManager() throws ResourceException {
65+
AzureSBConnectionImpl mockConnection = mock(AzureSBConnectionImpl.class);
66+
when(connectionManager.allocateConnection(managedConnectionFactory, null)).thenReturn(mockConnection);
67+
68+
factoryImpl = new AzureSBConnectionFactoryImpl(managedConnectionFactory, connectionManager);
69+
AzureSBConnection connection = factoryImpl.getConnection();
70+
71+
assertNotNull(connection);
72+
assertEquals(mockConnection, connection);
73+
verify(connectionManager).allocateConnection(managedConnectionFactory, null);
74+
}
75+
76+
@Test
77+
void testGetConnectionWithoutConnectionManager() throws ResourceException {
78+
var managedConnection = mock(AzureSBManagedConnection.class);
79+
when(managedConnectionFactory.createManagedConnection(null, null)).thenReturn(managedConnection);
80+
81+
factoryImpl = new AzureSBConnectionFactoryImpl(managedConnectionFactory, null);
82+
AzureSBConnection connection = factoryImpl.getConnection();
83+
84+
assertNotNull(connection);
85+
assertEquals(managedConnection, connection);
86+
verify(managedConnectionFactory).createManagedConnection(null, null);
87+
}
88+
}

0 commit comments

Comments
 (0)