|
| 1 | +package com.coder.gateway |
| 2 | + |
| 3 | +import spock.lang.Shared |
| 4 | +import spock.lang.Specification |
| 5 | +import spock.lang.Unroll |
| 6 | + |
| 7 | +@Unroll |
| 8 | +class CoderGatewayConnectionProviderTest extends Specification { |
| 9 | + @Shared |
| 10 | + def agents = [ |
| 11 | + agent_name_3: "b0e4c54d-9ba9-4413-8512-11ca1e826a24", |
| 12 | + agent_name_2: "fb3daea4-da6b-424d-84c7-36b90574cfef", |
| 13 | + agent_name: "9a920eee-47fb-4571-9501-e4b3120c12f2", |
| 14 | + ] |
| 15 | + def oneAgent = [ |
| 16 | + agent_name_3: "b0e4c54d-9ba9-4413-8512-11ca1e826a24" |
| 17 | + ] |
| 18 | + |
| 19 | + def "gets matching agent"() { |
| 20 | + expect: |
| 21 | + def ws = DataGen.workspace("ws", agents) |
| 22 | + CoderGatewayConnectionProvider.getMatchingAgent(parameters, ws).agentID == UUID.fromString(expected) |
| 23 | + |
| 24 | + where: |
| 25 | + parameters | expected |
| 26 | + [agent: "agent_name"] | "9a920eee-47fb-4571-9501-e4b3120c12f2" |
| 27 | + [agent_id: "9a920eee-47fb-4571-9501-e4b3120c12f2"] | "9a920eee-47fb-4571-9501-e4b3120c12f2" |
| 28 | + [agent: "agent_name_2"] | "fb3daea4-da6b-424d-84c7-36b90574cfef" |
| 29 | + [agent_id: "fb3daea4-da6b-424d-84c7-36b90574cfef"] | "fb3daea4-da6b-424d-84c7-36b90574cfef" |
| 30 | + [agent: "agent_name_3"] | "b0e4c54d-9ba9-4413-8512-11ca1e826a24" |
| 31 | + [agent_id: "b0e4c54d-9ba9-4413-8512-11ca1e826a24"] | "b0e4c54d-9ba9-4413-8512-11ca1e826a24" |
| 32 | + |
| 33 | + // Prefer agent_id. |
| 34 | + [agent: "agent_name", agent_id: "b0e4c54d-9ba9-4413-8512-11ca1e826a24"] | "b0e4c54d-9ba9-4413-8512-11ca1e826a24" |
| 35 | + } |
| 36 | + |
| 37 | + def "fails to get matching agent"() { |
| 38 | + when: |
| 39 | + def ws = DataGen.workspace("ws", agents) |
| 40 | + CoderGatewayConnectionProvider.getMatchingAgent(parameters, ws) |
| 41 | + |
| 42 | + then: |
| 43 | + def err = thrown(expected) |
| 44 | + err.message.contains(message) |
| 45 | + |
| 46 | + where: |
| 47 | + parameters | expected | message |
| 48 | + [:] | MissingArgumentException | "Unable to determine" |
| 49 | + [agent: ""] | MissingArgumentException | "Unable to determine" |
| 50 | + [agent_id: ""] | MissingArgumentException | "Unable to determine" |
| 51 | + [agent: null] | MissingArgumentException | "Unable to determine" |
| 52 | + [agent_id: null] | MissingArgumentException | "Unable to determine" |
| 53 | + [agent: "ws"] | IllegalArgumentException | "agent named" |
| 54 | + [agent: "ws.agent_name"] | IllegalArgumentException | "agent named" |
| 55 | + [agent: "agent_name_4"] | IllegalArgumentException | "agent named" |
| 56 | + [agent_id: "not-a-uuid"] | IllegalArgumentException | "agent with ID" |
| 57 | + [agent_id: "ceaa7bcf-1612-45d7-b484-2e0da9349168"] | IllegalArgumentException | "agent with ID" |
| 58 | + |
| 59 | + // Will ignore agent if agent_id is set even if agent matches. |
| 60 | + [agent: "agent_name", agent_id: "ceaa7bcf-1612-45d7-b484-2e0da9349168"] | IllegalArgumentException | "agent with ID" |
| 61 | + } |
| 62 | + |
| 63 | + def "gets the first agent when workspace has only one"() { |
| 64 | + expect: |
| 65 | + def ws = DataGen.workspace("ws", oneAgent) |
| 66 | + CoderGatewayConnectionProvider.getMatchingAgent(parameters, ws).agentID == UUID.fromString("b0e4c54d-9ba9-4413-8512-11ca1e826a24") |
| 67 | + |
| 68 | + where: |
| 69 | + parameters << [ |
| 70 | + [:], |
| 71 | + [agent: ""], |
| 72 | + [agent_id: ""], |
| 73 | + [agent: null], |
| 74 | + [agent_id: null], |
| 75 | + ] |
| 76 | + } |
| 77 | + |
| 78 | + def "fails to get agent when workspace has only one"() { |
| 79 | + when: |
| 80 | + def ws = DataGen.workspace("ws", oneAgent) |
| 81 | + CoderGatewayConnectionProvider.getMatchingAgent(parameters, ws) |
| 82 | + |
| 83 | + then: |
| 84 | + def err = thrown(expected) |
| 85 | + err.message.contains(message) |
| 86 | + |
| 87 | + where: |
| 88 | + parameters | expected | message |
| 89 | + [agent: "ws"] | IllegalArgumentException | "agent named" |
| 90 | + [agent: "ws.agent_name_3"] | IllegalArgumentException | "agent named" |
| 91 | + [agent: "agent_name_4"] | IllegalArgumentException | "agent named" |
| 92 | + [agent_id: "ceaa7bcf-1612-45d7-b484-2e0da9349168"] | IllegalArgumentException | "agent with ID" |
| 93 | + } |
| 94 | + |
| 95 | + def "fails to get agent from workspace without agents"() { |
| 96 | + when: |
| 97 | + def ws = DataGen.workspace("ws") |
| 98 | + CoderGatewayConnectionProvider.getMatchingAgent(parameters, ws) |
| 99 | + |
| 100 | + then: |
| 101 | + def err = thrown(expected) |
| 102 | + err.message.contains(message) |
| 103 | + |
| 104 | + where: |
| 105 | + parameters | expected | message |
| 106 | + [:] | IllegalArgumentException | "has no agents" |
| 107 | + [agent: ""] | IllegalArgumentException | "has no agents" |
| 108 | + [agent_id: ""] | IllegalArgumentException | "has no agents" |
| 109 | + [agent: null] | IllegalArgumentException | "has no agents" |
| 110 | + [agent_id: null] | IllegalArgumentException | "has no agents" |
| 111 | + [agent: "agent_name"] | IllegalArgumentException | "has no agents" |
| 112 | + [agent_id: "9a920eee-47fb-4571-9501-e4b3120c12f2"] | IllegalArgumentException | "has no agents" |
| 113 | + } |
| 114 | +} |
0 commit comments