1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! Models for Pact interactions

use std::fmt::{self, Debug, Display, Formatter};
use std::sync::{Arc, Mutex};

use serde_json::Value;

use crate::bodies::OptionalBody;
use crate::content_types::ContentType;
use crate::matchingrules::MatchingRules;
use crate::message::Message;
use crate::PactSpecification;
use crate::provider_states::ProviderState;
use crate::sync_interaction::RequestResponseInteraction;
use crate::v4::async_message::AsynchronousMessage;
use crate::v4::interaction::{interaction_from_json, V4Interaction};
use crate::v4::sync_message::SynchronousMessage;
use crate::v4::synch_http::SynchronousHttp;

/// Struct that defined an interaction conflict
#[derive(Debug, Clone)]
pub struct PactConflict {
  /// Description of the interactions
  pub interaction: String,
  /// Conflict description
  pub description: String
}

/// Interaction Trait
pub trait Interaction: Debug {
  /// The type of the interaction
  fn type_of(&self) -> String;

  /// If this is a request/response interaction
  fn is_request_response(&self) -> bool;

  /// Returns the request/response interaction if it is one
  fn as_request_response(&self) -> Option<RequestResponseInteraction>;

  /// If this is a message interaction
  fn is_message(&self) -> bool;

  /// Returns the message interaction if it is one
  fn as_message(&self) -> Option<Message>;

  /// Interaction ID. This will only be set if the Pact file was fetched from a Pact Broker
  fn id(&self) -> Option<String>;

  /// Description of this interaction. This needs to be unique in the pact file.
  fn description(&self) -> String;

  /// Set the Interaction ID
  fn set_id(&mut self, id: Option<String>);

  /// Set the description of this interaction. This needs to be unique in the pact file.
  fn set_description(&mut self, description: &str);

  /// Optional provider states for the interaction.
  /// See `<https://docs.pact.io/getting_started/provider_states>` for more info on provider states.
  fn provider_states(&self) -> Vec<ProviderState>;

  /// Mutable Optional provider states for the interaction.
  /// See `<https://docs.pact.io/getting_started/provider_states>` for more info on provider states.
  fn provider_states_mut(&mut self) -> &mut Vec<ProviderState>;

  /// Body of the response or message
  #[deprecated(
  since = "0.1.0",
  note = "Some interactions have multiple contents (like request/response), so it is impossible \
      to know which to return for this method"
  )]
  fn contents(&self) -> OptionalBody;

  /// The contents of the part to use for verification. For example, with HTTP interactions, this
  /// will be the response body
  fn contents_for_verification(&self) -> OptionalBody;

  /// Determine the content type of the interaction. If a `Content-Type` header or metadata value is present, the
  /// value of that value will be returned. Otherwise, the contents will be inspected.
  #[deprecated(
  since = "0.1.0",
  note = "Some interactions have multiple contents (like request/response), so it is impossible \
      to know which to return for this method"
  )]
  fn content_type(&self) -> Option<ContentType>;

  /// If this is a V4 interaction
  fn is_v4(&self) -> bool;

  /// Returns the interaction in V4 format
  fn as_v4(&self) -> Option<Box<dyn V4Interaction>>;

  /// Returns a mutable reference for the  interaction. If the interaction is not a V4 format,
  /// will return None. The `as_v4` method can convert to V4 format (via cloning the data).
  fn as_v4_mut(&mut self) -> Option<&mut dyn V4Interaction>;

  /// If the interaction is V4 HTTP
  fn is_v4_http(&self) -> bool { false }

  /// Returns the interaction in V4 format
  fn as_v4_http(&self) -> Option<SynchronousHttp>;

  /// Returns the interaction in V4 format
  fn as_v4_async_message(&self) -> Option<AsynchronousMessage>;

  /// If the interaction is a V4 message
  fn is_v4_async_message(&self) -> bool { false }

  /// Returns the interaction in V4 format
  fn as_v4_sync_message(&self) -> Option<SynchronousMessage>;

  /// Returns the interaction in V4 format
  fn as_v4_http_mut(&mut self) -> Option<&mut SynchronousHttp>;

  /// If the interaction is a V4 synchronous request/response message
  fn is_v4_sync_message(&self) -> bool { false }

  /// Returns the interaction in V4 format
  fn as_v4_async_message_mut(&mut self) -> Option<&mut AsynchronousMessage>;

  /// Returns the interaction in V4 format
  fn as_v4_sync_message_mut(&mut self) -> Option<&mut SynchronousMessage>;

  /// Clones this interaction and wraps it in a Box
  fn boxed(&self) -> Box<dyn Interaction + Send + Sync>;

  /// Clones this interaction and wraps it in an Arc
  fn arced(&self) -> Arc<dyn Interaction + Send + Sync>;

  /// Clones this interaction and wraps it in an Arc and Mutex
  fn thread_safe(&self) -> Arc<Mutex<dyn Interaction + Send + Sync>>;

  /// Returns the matching rules associated with this interaction (if there are any)
  #[deprecated(
  since = "0.2.1",
  note = "Some interactions have multiple contents (like request/response), so it is impossible \
      to know which to return for this method"
  )]
  fn matching_rules(&self) -> Option<MatchingRules>;

  /// If this interaction is pending (V4 only)
  fn pending(&self) -> bool { false }
}

impl Display for dyn Interaction {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    if let Some(req_res) = self.as_request_response() {
      std::fmt::Display::fmt(&req_res, f)
    } else if let Some(mp) = self.as_message() {
      std::fmt::Display::fmt(&mp, f)
    } else if let Some(mp) = self.as_v4_http() {
      std::fmt::Display::fmt(&mp, f)
    } else if let Some(mp) = self.as_v4_async_message() {
      std::fmt::Display::fmt(&mp, f)
    } else {
      Err(fmt::Error)
    }
  }
}

impl Clone for Box<dyn Interaction> {
  fn clone(&self) -> Self {
    if self.is_v4() {
      if let Some(http) = self.as_v4_http() {
        Box::new(http)
      } else if let Some(message) = self.as_v4_async_message() {
        Box::new(message)
      } else {
        panic!("Internal Error - Tried to clone an interaction that was not valid")
      }
    } else if let Some(req_res) = self.as_request_response() {
      Box::new(req_res)
    } else if let Some(mp) = self.as_message() {
      Box::new(mp)
    } else {
      panic!("Internal Error - Tried to clone an interaction that was not valid")
    }
  }
}


/// Converts the JSON struct into an HTTP Interaction
pub fn http_interaction_from_json(source: &str, json: &Value, spec: &PactSpecification) -> anyhow::Result<Box<dyn Interaction + Send + Sync>> {
  match spec {
    PactSpecification::V4 => interaction_from_json(source, 0, json)
      .map(|i| i.boxed()),
    _ => Ok(Box::new(RequestResponseInteraction::from_json(0, json, spec)?))
  }
}

/// Converts the JSON struct into a Message Interaction
pub fn message_interaction_from_json(source: &str, json: &Value, spec: &PactSpecification) -> anyhow::Result<Box<dyn Interaction + Send + Sync>> {
  match spec {
    PactSpecification::V4 => interaction_from_json(source, 0, json)
      .map(|i| i.boxed()),
    _ => Message::from_json(0, json, spec).map(|i| i.boxed())
  }
}

pub(crate) fn parse_interactions(pact_json: &Value, spec_version: PactSpecification
) -> anyhow::Result<Vec<RequestResponseInteraction>> {
  if let Some(&Value::Array(ref array)) = pact_json.get("interactions") {
    array.iter().enumerate().map(|(index, ijson)| {
      RequestResponseInteraction::from_json(index, ijson, &spec_version)
    }).collect()
  }
  else {
    Ok(vec![])
  }
}