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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Support for matching and generating content based on content types
use std::collections::HashMap;
use std::str::from_utf8;

use anyhow::anyhow;
use bytes::Bytes;
use log::{debug, error};
use maplit::hashmap;
use pact_models::bodies::OptionalBody;
use pact_models::content_types::ContentTypeHint;
use pact_models::matchingrules::{Category, MatchingRule, MatchingRuleCategory, RuleList};
use pact_models::path_exp::DocPath;
use pact_models::prelude::{ContentType, Generator, GeneratorCategory, Generators, RuleLogic};
use serde_json::Value;

use crate::catalogue_manager::{CatalogueEntry, CatalogueEntryProviderType};
use crate::plugin_manager::lookup_plugin;
use crate::plugin_models::{PactPluginManifest, PluginInteractionConfig, PactPluginRpc};
use crate::proto::{
  Body,
  CompareContentsRequest,
  ConfigureInteractionRequest,
  GenerateContentRequest,
  PluginConfiguration as ProtoPluginConfiguration
};
use crate::proto::body;
use crate::proto::interaction_response::MarkupType;
use crate::utils::{proto_struct_to_json, proto_struct_to_map, to_proto_struct};

/// Matcher for contents based on content type
#[derive(Clone, Debug)]
pub struct ContentMatcher {
  /// Catalogue entry for this content matcher
  pub catalogue_entry: CatalogueEntry
}

impl ContentMatcher {
  /// Plugin details for this content matcher
  pub fn plugin(&self) -> Option<PactPluginManifest> {
    self.catalogue_entry.plugin.clone()
  }
}

/// Mismatch result
#[derive(Clone, Debug)]
pub struct ContentMismatch {
  /// Expected value in string format
  // TODO: change to bytes
  pub expected: String,
  /// Actual value in string format
  // TODO: change to bytes
  pub actual: String,
  /// Mismatch description
  pub mismatch: String,
  /// Path to the mismatch
  pub path: String,
  /// Optional diff of the expected and actual values
  pub diff: Option<String>
}

/// Interaction contents setup by the plugin
#[derive(Clone, Debug)]
pub struct InteractionContents {
  /// Description of what part this interaction belongs to (in the case of there being more than
  /// one, for instance, request/response messages)
  pub part_name: String,

  /// Body/Contents of the interaction
  pub body: OptionalBody,

  /// Matching rules to apply
  pub rules: Option<MatchingRuleCategory>,

  /// Generators to apply
  pub generators: Option<Generators>,

  /// Message metadata
  pub metadata: Option<HashMap<String, Value>>,

  /// Plugin configuration data to apply to the interaction
  pub plugin_config: PluginConfiguration,

  /// Markup for the interaction to display in any UI
  pub interaction_markup: String,

  /// The type of the markup (CommonMark or HTML)
  pub interaction_markup_type: String
}

impl Default for InteractionContents {
  fn default() -> Self {
    InteractionContents {
      part_name: Default::default(),
      body: Default::default(),
      rules: None,
      generators: None,
      metadata: None,
      plugin_config: Default::default(),
      interaction_markup: Default::default(),
      interaction_markup_type: Default::default()
    }
  }
}

/// Plugin data to persist into the Pact file
#[derive(Clone, Debug)]
pub struct PluginConfiguration {
  /// Data to perist on the interaction
  pub interaction_configuration: HashMap<String, Value>,
  /// Data to persist in the Pact metadata
  pub pact_configuration: HashMap<String, Value>
}

impl PluginConfiguration {
  /// Plugin data is empty when the interaction and pact data is empty
  pub fn is_empty(&self) -> bool {
    self.pact_configuration.is_empty() && self.interaction_configuration.is_empty()
  }
}

impl Default for PluginConfiguration {
  fn default() -> Self {
    PluginConfiguration {
      interaction_configuration: Default::default(),
      pact_configuration: Default::default()
    }
  }
}

impl From<ProtoPluginConfiguration> for PluginConfiguration {
  fn from(config: ProtoPluginConfiguration) -> Self {
    PluginConfiguration {
      interaction_configuration: config.interaction_configuration.as_ref().map(|c| proto_struct_to_map(c)).unwrap_or_default(),
      pact_configuration: config.pact_configuration.as_ref().map(|c| proto_struct_to_map(c)).unwrap_or_default()
    }
  }
}

impl ContentMatcher {
  /// If this is a core framework matcher
  pub fn is_core(&self) -> bool {
    self.catalogue_entry.provider_type == CatalogueEntryProviderType::CORE
  }

  /// Catalogue entry key for this matcher
  pub fn catalogue_entry_key(&self) -> String {
    if self.is_core() {
      format!("core/content-matcher/{}", self.catalogue_entry.key)
    } else {
      format!("plugin/{}/content-matcher/{}", self.plugin_name(), self.catalogue_entry.key)
    }
  }

  /// Plugin name that provides this matcher
  pub fn plugin_name(&self) -> String {
    self.catalogue_entry.plugin.as_ref()
      .map(|p| p.name.clone())
      .unwrap_or("core".to_string())
  }

  /// Plugin version that provides this matcher
  pub fn plugin_version(&self) -> String {
    self.catalogue_entry.plugin.as_ref()
      .map(|p| p.version.clone())
      .unwrap_or_default()
  }

  /// Get the plugin to configure the interaction contents for the interaction part based on the
  /// provided definition
  pub async fn configure_interation(
    &self,
    content_type: &ContentType,
    definition: HashMap<String, Value>
  ) -> anyhow::Result<(Vec<InteractionContents>, Option<PluginConfiguration>)> {
    debug!("Sending ConfigureContents request to plugin {:?}", self.catalogue_entry);
    let request = ConfigureInteractionRequest {
      content_type: content_type.to_string(),
      contents_config: Some(to_proto_struct(definition)),
    };

    let plugin_manifest = self.catalogue_entry.plugin.as_ref()
      .expect("Plugin type is required");
    match lookup_plugin(&plugin_manifest.as_dependency()) {
      Some(plugin) => match plugin.configure_interaction(request).await {
        Ok(response) => {
          debug!("Got response: {:?}", response);
          if response.error.is_empty() {
            let mut results = vec![];

            for response in response.interaction {
              let body = match &response.contents {
                Some(body) => {
                  let returned_content_type = ContentType::parse(body.content_type.as_str()).ok();
                  let contents = body.content.as_ref().cloned().unwrap_or_default();
                  OptionalBody::Present(Bytes::from(contents), returned_content_type,
                                        Some(match body.content_type_hint() {
                                          body::ContentTypeHint::Text => ContentTypeHint::TEXT,
                                          body::ContentTypeHint::Binary => ContentTypeHint::BINARY,
                                          body::ContentTypeHint::Default => ContentTypeHint::DEFAULT,
                                        }))
                },
                None => OptionalBody::Missing
              };

              let rules = if !response.rules.is_empty() {
                Some(MatchingRuleCategory {
                  name: Category::BODY,
                  rules: response.rules.iter().map(|(k, rules)| {
                    // TODO: This is unwrapping the DocPath
                    (DocPath::new(k).unwrap(), RuleList {
                      rules: rules.rule.iter().map(|rule| {
                        // TODO: This is unwrapping the MatchingRule
                        MatchingRule::create(rule.r#type.as_str(), &rule.values.as_ref().map(|rule| {
                          proto_struct_to_json(rule)
                        }).unwrap_or_default()).unwrap()
                      }).collect(),
                      rule_logic: RuleLogic::And,
                      cascaded: false
                    })
                  }).collect()
                })
              } else {
                None
              };

              let generators = if !response.generators.is_empty() {
                Some(Generators {
                  categories: hashmap! {
                    GeneratorCategory::BODY => response.generators.iter().map(|(k, gen)| {
                      // TODO: This is unwrapping the DocPath
                      // TODO: This is unwrapping the Generator
                      (DocPath::new(k).unwrap(), Generator::create(gen.r#type.as_str(),
                        &gen.values.as_ref().map(|attr| proto_struct_to_json(attr)).unwrap_or_default()).unwrap())
                    }).collect()
                  }
                })
              } else {
                None
              };

              let metadata = response.message_metadata.as_ref().map(|md| proto_struct_to_map(md));

              let plugin_config = if let Some(plugin_configuration) = &response.plugin_configuration {
                PluginConfiguration {
                  interaction_configuration: plugin_configuration.interaction_configuration.as_ref()
                    .map(|val| proto_struct_to_map(val)).unwrap_or_default(),
                  pact_configuration: plugin_configuration.pact_configuration.as_ref()
                    .map(|val| proto_struct_to_map(val)).unwrap_or_default()
                }
              } else {
                PluginConfiguration::default()
              };

              debug!("body={}", body);
              debug!("rules={:?}", rules);
              debug!("generators={:?}", generators);
              debug!("metadata={:?}", metadata);
              debug!("pluginConfig={:?}", plugin_config);

              results.push(InteractionContents {
                part_name: response.part_name.clone(),
                body,
                rules,
                generators,
                metadata,
                plugin_config,
                interaction_markup: response.interaction_markup.clone(),
                interaction_markup_type: match response.interaction_markup_type() {
                  MarkupType::Html => "HTML".to_string(),
                  _ => "COMMON_MARK".to_string(),
                }
              })
            }

            Ok((results, response.plugin_configuration.map(|config| PluginConfiguration::from(config))))
          } else {
            Err(anyhow!("Request to configure interaction failed: {}", response.error))
          }
        }
        Err(err) => {
          error!("Call to plugin failed - {}", err);
          Err(anyhow!("Call to plugin failed - {}", err))
        }
      },
      None => {
        error!("Plugin for {:?} was not found in the plugin register", self.catalogue_entry);
        Err(anyhow!("Plugin for {:?} was not found in the plugin register", self.catalogue_entry))
      }
    }
  }

  /// Get the plugin to match the contents against the expected contents returning all the mismatches.
  /// Note that it is an error to call this with a non-plugin (core) content matcher.
  ///
  /// panics:
  /// If called with a core content matcher
  pub async fn match_contents(
    &self,
    expected: &OptionalBody,
    actual: &OptionalBody,
    context: &MatchingRuleCategory,
    allow_unexpected_keys: bool,
    plugin_config: Option<PluginInteractionConfig>
  ) -> Result<(), HashMap<String, Vec<ContentMismatch>>> {
    let request = CompareContentsRequest {
      expected: Some(Body {
        content_type: expected.content_type().unwrap_or_default().to_string(),
        content: expected.value().map(|b| b.to_vec()),
        content_type_hint: body::ContentTypeHint::Default as i32
      }),
      actual: Some(Body {
        content_type: actual.content_type().unwrap_or_default().to_string(),
        content: actual.value().map(|b| b.to_vec()),
        content_type_hint: body::ContentTypeHint::Default as i32
      }),
      allow_unexpected_keys,
      rules: context.rules.iter().map(|(k, r)| {
        (k.to_string(), crate::proto::MatchingRules {
          rule: r.rules.iter().map(|rule|{
            crate::proto::MatchingRule {
              r#type: rule.name(),
              values: Some(to_proto_struct(rule.values().iter().map(|(k, v)| (k.to_string(), v.clone())).collect())),
            }
          }).collect()
        })
      }).collect(),
      plugin_configuration: plugin_config.map(|config| ProtoPluginConfiguration {
        interaction_configuration: Some(to_proto_struct(config.interaction_configuration)),
        pact_configuration: Some(to_proto_struct(config.pact_configuration))
      })
    };

    let plugin_manifest = self.catalogue_entry.plugin.as_ref()
      .expect("Plugin type is required");
    match lookup_plugin(&plugin_manifest.as_dependency()) {
      Some(plugin) => match plugin.compare_contents(request).await {
        Ok(response) => if let Some(mismatch) = response.type_mismatch {
          Err(hashmap!{
            String::default() => vec![
              ContentMismatch {
                expected: mismatch.expected.clone(),
                actual: mismatch.actual.clone(),
                mismatch: format!("Expected content type '{}' but got '{}'", mismatch.expected, mismatch.actual),
                path: "".to_string(),
                diff: None
              }
            ]
          })
        } else if !response.error.is_empty() {
          Err(hashmap! {
            String::default() => vec![
              ContentMismatch {
                expected: Default::default(),
                actual: Default::default(),
                mismatch: response.error.clone(),
                path: "".to_string(),
                diff: None
              }
            ]
          })
        } else if !response.results.is_empty() {
          Err(response.results.iter().map(|(k, v)| {
            (k.clone(), v.mismatches.iter().map(|mismatch| {
              ContentMismatch {
                expected: mismatch.expected.as_ref()
                  .map(|e| from_utf8(&e).unwrap_or_default().to_string())
                  .unwrap_or_default(),
                actual: mismatch.actual.as_ref()
                  .map(|a| from_utf8(&a).unwrap_or_default().to_string())
                  .unwrap_or_default(),
                mismatch: mismatch.mismatch.clone(),
                path: mismatch.path.clone(),
                diff: if mismatch.diff.is_empty() {
                  None
                } else {
                  Some(mismatch.diff.clone())
                }
              }
            }).collect())
          }).collect())
        } else {
          Ok(())
        }
        Err(err) => {
          error!("Call to plugin failed - {}", err);
          Err(hashmap! {
            String::default() => vec![
              ContentMismatch {
                expected: "".to_string(),
                actual: "".to_string(),
                mismatch: format!("Call to plugin failed = {}", err),
                path: "".to_string(),
                diff: None
              }
            ]
          })
        }
      },
      None => {
        error!("Plugin for {:?} was not found in the plugin register", self.catalogue_entry);
        Err(hashmap! {
          String::default() => vec![
            ContentMismatch {
              expected: "".to_string(),
              actual: "".to_string(),
              mismatch: format!("Plugin for {:?} was not found in the plugin register", self.catalogue_entry),
              path: "".to_string(),
              diff: None
            }
          ]
        })
      }
    }
  }
}

/// Generator for contents based on content type
#[derive(Clone, Debug)]
pub struct ContentGenerator {
  /// Catalogue entry for this content matcher
  pub catalogue_entry: CatalogueEntry
}

impl ContentGenerator {
  /// If this is a core framework generator
  pub fn is_core(&self) -> bool {
    self.catalogue_entry.provider_type == CatalogueEntryProviderType::CORE
  }

  /// Catalogue entry key for this generator
  pub fn catalogue_entry_key(&self) -> String {
    if self.is_core() {
      format!("core/content-generator/{}", self.catalogue_entry.key)
    } else {
      format!("plugin/{}/content-generator/{}", self.plugin_name(), self.catalogue_entry.key)
    }
  }

  /// Plugin name that provides this matcher
  pub fn plugin_name(&self) -> String {
    self.catalogue_entry.plugin.as_ref()
      .map(|p| p.name.clone())
      .unwrap_or("core".to_string())
  }

  /// Generate the content for the given content type and body
  // TODO need to pass in any plugin configuration
  pub async fn generate_content(
    &self,
    content_type: &ContentType,
    generators: &HashMap<String, Generator>,
    body: &OptionalBody
  ) -> anyhow::Result<OptionalBody> {
    let request = GenerateContentRequest {
      contents: Some(crate::proto::Body {
        content_type: content_type.to_string(),
        content: Some(body.value().unwrap_or_default().to_vec()),
        content_type_hint: body::ContentTypeHint::Default as i32
      }),
      generators: generators.iter().map(|(k, v)| {
        (k.clone(), crate::proto::Generator {
          r#type: v.name(),
          values: Some(to_proto_struct(v.values().iter()
            .map(|(k, v)| (k.to_string(), v.clone())).collect())),
        })
      }).collect(),
      plugin_configuration: None
    };

    let plugin_manifest = self.catalogue_entry.plugin.as_ref()
      .expect("Plugin type is required");
    match lookup_plugin(&plugin_manifest.as_dependency()) {
      Some(plugin) => {
        debug!("Sending generateContent request to plugin {:?}", plugin_manifest);
        match plugin.generate_content(request).await?.contents {
          Some(contents) => {
            Ok(OptionalBody::Present(
              Bytes::from(contents.content.unwrap_or_default()),
              ContentType::parse(contents.content_type.as_str()).ok(),
              None
            ))
          }
          None => Ok(OptionalBody::Empty)
        }
      },
      None => {
        error!("Plugin for {:?} was not found in the plugin register", self.catalogue_entry);
        Err(anyhow!("Plugin for {:?} was not found in the plugin register", self.catalogue_entry))
      }
    }
  }
}