Skip to content

Commit d5901f0

Browse files
feat: add tests to connection binding on gateway objects
1 parent 4cf52c9 commit d5901f0

3 files changed

Lines changed: 199 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.mx.path.gateway.configuration
2+
3+
import com.mx.path.core.common.accessor.PathResponseStatus
4+
import com.mx.path.core.common.collection.ObjectMap
5+
import com.mx.path.core.common.gateway.GatewayException
6+
import com.mx.testing.binding.ConnectionWithBoundConfiguration
7+
8+
import spock.lang.Specification
9+
10+
class ConnectionBinderTest extends Specification {
11+
12+
ObjectMap configuration
13+
ConnectionBinder subject
14+
15+
def setup() {
16+
subject = new ConnectionBinder()
17+
configuration = new ObjectMap()
18+
}
19+
20+
def "build connection"() {
21+
given:
22+
def configuration = new ObjectMap().tap {
23+
createMap("TestConnection").tap {
24+
createMap("configurations").tap {
25+
put("clientId", "clientId")
26+
}
27+
put("baseUrl", "url")
28+
put("certificateAlias", "alias")
29+
put("keystorePath", "path")
30+
put("keystorePassword", "password")
31+
}
32+
}
33+
34+
when:
35+
def connection = (ConnectionWithBoundConfiguration) subject.build(ConnectionWithBoundConfiguration.class, configuration, "TestConnection")
36+
37+
then:
38+
verifyAll (connection) {
39+
baseUrl == "url"
40+
certificateAlias == "alias"
41+
keystorePath == "path"
42+
keystorePassword.toString() == "password"
43+
}
44+
}
45+
46+
def "build connection throw on missing client id"() {
47+
given:
48+
def configuration = new ObjectMap().tap {
49+
createMap("TestConnection").tap {
50+
createMap("configurations").tap {
51+
put("clientId", null)
52+
}
53+
put("baseUrl", "url")
54+
put("certificateAlias", "alias")
55+
put("keystorePath", "path")
56+
put("keystorePassword", "password")
57+
}
58+
}
59+
60+
when:
61+
subject.build(ConnectionWithBoundConfiguration.class, configuration, "TestConnection")
62+
63+
then:
64+
def e = thrown(ConfigurationError)
65+
e.message == "Client ID not provided for connection TestConnection at connections.TestConnection"
66+
}
67+
68+
def "build connection settings"() {
69+
given:
70+
def configuration = new ObjectMap().tap {
71+
createMap("TestConnection").tap {
72+
put("baseUrl", "url")
73+
put("certificateAlias", "alias")
74+
put("keystorePath", "path")
75+
put("keystorePassword", "password")
76+
}
77+
}
78+
79+
when:
80+
def connection = subject.buildConnection(configuration, "TestConnection")
81+
82+
then:
83+
verifyAll (connection) {
84+
baseUrl == "url"
85+
certificateAlias == "alias"
86+
keystorePath == "path"
87+
keystorePassword.toString() == "password"
88+
}
89+
}
90+
91+
def "build connection and fail validation"() {
92+
given:
93+
def configuration = new ObjectMap().tap {
94+
createMap("TestConnection").tap {
95+
put("baseUrl", "url")
96+
put("certificateAlias", "")
97+
put("keystorePath", "path")
98+
put("keystorePassword", "password")
99+
}
100+
}
101+
102+
when:
103+
subject.buildConnection(configuration, "TestConnection")
104+
105+
then:
106+
def e = thrown(GatewayException)
107+
e.status == PathResponseStatus.INTERNAL_ERROR
108+
e.message == "Invalid connection details. Missing certificateAlias"
109+
}
110+
}

gateway/src/test/groovy/com/mx/path/gateway/configuration/GatewayObjectConfiguratorTest.groovy

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.mx.path.gateway.behavior.GatewayBehavior
66
import com.mx.path.gateway.service.GatewayService
77
import com.mx.testing.MessageBrokerImpl
88
import com.mx.testing.binding.BehaviorWithConfiguration
9+
import com.mx.testing.binding.BehaviorWithConfigurationAndConnection
910
import com.mx.testing.binding.BindedConfigGatewayService
1011

1112
import spock.lang.Specification
@@ -64,6 +65,45 @@ class GatewayObjectConfiguratorTest extends Specification {
6465
}
6566
}
6667

68+
def "builds and binds a GatewayBehavior with Connection"() {
69+
given:
70+
def node = new ObjectMap().tap {
71+
put("class", BehaviorWithConfigurationAndConnection.getCanonicalName())
72+
put("configurations", new ObjectMap().tap {
73+
put("active", true)
74+
put("actionFilter", "put")
75+
})
76+
createMap("connections").tap {
77+
createMap("connection").tap {
78+
createMap("configurations").tap {
79+
put("clientId", "clientId")
80+
}
81+
put("baseUrl", "url")
82+
put("certificateAlias", "alias")
83+
put("keystorePath", "path")
84+
put("keystorePassword", "password")
85+
}
86+
}
87+
}
88+
89+
when:
90+
def result = subject.buildFromNode(node, "client1", GatewayBehavior)
91+
92+
then:
93+
result instanceof BehaviorWithConfigurationAndConnection
94+
verifyAll((BehaviorWithConfigurationAndConnection) result) {
95+
connectionWithBoundConfiguration
96+
behaviorConfiguration.active
97+
initialized
98+
verifyAll (connectionWithBoundConfiguration) {
99+
baseUrl == "url"
100+
certificateAlias == "alias"
101+
keystorePath == "path"
102+
keystorePassword.toString() == "password"
103+
}
104+
}
105+
}
106+
67107
def "builds and binds a Facility"() {
68108
given:
69109
def node = new ObjectMap().tap {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.mx.testing.binding;
2+
3+
import lombok.Getter;
4+
5+
import com.mx.path.core.common.collection.ObjectMap;
6+
import com.mx.path.core.common.configuration.Configuration;
7+
import com.mx.path.core.common.configuration.ConfigurationField;
8+
import com.mx.path.gateway.accessor.AccessorResponse;
9+
import com.mx.path.gateway.behavior.GatewayBehavior;
10+
import com.mx.path.gateway.configuration.Configurable;
11+
import com.mx.path.gateway.configuration.annotations.Connection;
12+
import com.mx.path.gateway.context.GatewayRequestContext;
13+
14+
public class BehaviorWithConfigurationAndConnection extends GatewayBehavior implements Configurable {
15+
@Getter
16+
private final BehaviorConfiguration behaviorConfiguration;
17+
18+
@Getter
19+
private final ConnectionWithBoundConfiguration connectionWithBoundConfiguration;
20+
21+
@Getter
22+
boolean initialized = false;
23+
24+
public static class BehaviorConfiguration {
25+
@Getter
26+
@ConfigurationField("active")
27+
private boolean active;
28+
29+
@Getter
30+
@ConfigurationField("actionFilter")
31+
private String actionFilter;
32+
}
33+
34+
public BehaviorWithConfigurationAndConnection(ObjectMap configurations, @Configuration BehaviorConfiguration behaviorConfiguration, @Connection("connection") ConnectionWithBoundConfiguration connection) {
35+
super(configurations);
36+
this.connectionWithBoundConfiguration = connection;
37+
this.behaviorConfiguration = behaviorConfiguration;
38+
}
39+
40+
@Override
41+
protected <T> AccessorResponse<T> call(Class<T> resultType, GatewayRequestContext request, GatewayBehavior terminatingBehavior) {
42+
return null;
43+
}
44+
45+
@Override
46+
public void initialize() {
47+
this.initialized = true;
48+
}
49+
}

0 commit comments

Comments
 (0)