// ĠCopyright Ġ 2 0 1 9 ĠAmazon . com , ĠInc . Ġor Ġits Ġaffiliates . ĠAll ĠRights ĠReserved . Ċ // ĠSPDX - License - Identifier : ĠApache - 2 . 0 Ċ Ċ package Ġsoftware . aws . tool k its . jetbrains . core . credentials Ċ Ċ import Ġcom . intellij . openapi . action System . DataContext Ċ import Ġcom . intellij . openapi . file Editor . File Editor Manager Ċ import Ġcom . intellij . openapi . file Types . File Types Ċ import Ġcom . intellij . openapi . ui . Messages Ċ import Ġcom . intellij . openapi . ui . Test Dialog Ċ import Ġcom . intellij . test Framework . Project Rule Ċ import Ġcom . intellij . test Framework . Test ActionEvent Ċ import Ġcom . intellij . test Framework . run In Ed t And Wait Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . any Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . do Answer Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Zero Interactions Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . junit . After Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . rules . Temporary Folder Ċ import Ġjava . io . File Ċ Ċ class ĠCreate OrUpdate Credential Profiles Action Test Ġ{ ĊĊĠĠĠ Ġ@ Rule ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġval Ġfolder Rule Ġ= ĠTemporary Folder () ĊĊĠĠĠ Ġ@ Rule ĊĠĠĠ Ġ@ Jvm Field ĊĠĠĠ Ġval Ġproject Rule Ġ= ĠProject Rule () ĊĊĠĠĠ Ġprivate Ġval Ġfile Editor Manager Ġ= ĠFile Editor Manager . getInstance ( project Rule . project ) ĊĊĠĠĠ Ġ@ After ĊĠĠĠ Ġfun Ġclean Up () Ġ{ ĊĠĠĠĠĠĠĠ Ġrun In Ed t And Wait Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfile Editor Manager . open Files . forEach Ġ{ Ġfile Editor Manager . close File ( it ) Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġconfirm ConfigFile Created _ both Files Do NotExist () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwriter Ġ= Ġmock < ConfigFile Writer > Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġon Ġ{ Ġcreate File ( any ()) Ġ}. do Answer Ġ{ Ġit . get Argument < File >( 0 ). write Text (" hello ") Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġconfig File Ġ= ĠFile ( folder Rule . new Folder (), Ġ" config ") ĊĠĠĠĠĠĠĠ Ġval Ġcred File Ġ= ĠFile ( folder Rule . new Folder (), Ġ" credentials ") ĊĊĠĠĠĠĠĠĠ Ġval Ġsut Ġ= ĠCreate OrUpdate Credential Profiles Action ( writer , Ġconfig File , Ġcred File ) ĊĠĠĠĠĠĠĠ ĠMessages . set Test Dialog ( Test Dialog . OK ) ĊĊĠĠĠĠĠĠĠ Ġsut . action Performed ( Test ActionEvent ( DataContext Ġ{ Ġproject Rule . project Ġ})) ĊĊĠĠĠĠĠĠĠ Ġverify ( writer ). create File ( configFile ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( file Editor Manager . open Files ). has Only One Element Satisf ying Ġ{ ĠassertThat ( it . name ). isEqualTo (" config ") Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġboth Files Opened _ both Files Exists () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwriter Ġ= Ġmock < ConfigFile Writer >() ĊĊĠĠĠĠĠĠĠ Ġval Ġconfig File Ġ= Ġfolder Rule . new File (" config ") ĊĠĠĠĠĠĠĠ Ġval Ġcred File Ġ= Ġfolder Rule . new File (" credentials ") ĊĠĠĠĠĠĠĠ Ġ// ĠIDE Ġinterpre ts Ġblank Ġfiles Ġwith Ġno Ġextension Ġas Ġbinary ĊĠĠĠĠĠĠĠ Ġconfig File . write Text (" config ") ĊĠĠĠĠĠĠĠ Ġcred File . write Text (" cred ") ĊĊĠĠĠĠĠĠĠ Ġval Ġsut Ġ= ĠCreate OrUpdate Credential Profiles Action ( writer , Ġconfig File , Ġcred File ) ĊĠĠĠĠĠĠĠ ĠMessages . set Test Dialog ( Test Dialog . OK ) ĊĊĠĠĠĠĠĠĠ Ġsut . action Performed ( Test ActionEvent ( DataContext Ġ{ Ġproject Rule . project Ġ})) ĊĊĠĠĠĠĠĠĠ Ġverify Zero Interactions ( writer ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( file Editor Manager . open Files ). has Size ( 2 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. any Satisf y Ġ{ ĠassertThat ( it . name ). isEqualTo (" config ") Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. any Satisf y Ġ{ ĠassertThat ( it . name ). isEqualTo (" credentials ") Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġconfig File Opened _ only Config Exists () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwriter Ġ= Ġmock < ConfigFile Writer >() ĊĊĠĠĠĠĠĠĠ Ġval Ġconfig File Ġ= Ġfolder Rule . new File (" config ") ĊĠĠĠĠĠĠĠ Ġval Ġcred File Ġ= ĠFile ( folder Rule . new Folder (), Ġ" credentials ") ĊĠĠĠĠĠĠĠ Ġconfig File . write Text (" config ") ĊĊĠĠĠĠĠĠĠ Ġval Ġsut Ġ= ĠCreate OrUpdate Credential Profiles Action ( writer , Ġconfig File , Ġcred File ) ĊĠĠĠĠĠĠĠ ĠMessages . set Test Dialog ( Test Dialog . OK ) ĊĊĠĠĠĠĠĠĠ Ġsut . action Performed ( Test ActionEvent ( DataContext Ġ{ Ġproject Rule . project Ġ})) ĊĊĠĠĠĠĠĠĠ Ġverify Zero Interactions ( writer ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( file Editor Manager . open Files ). has Only One Element Satisf ying Ġ{ ĠassertThat ( it . name ). isEqualTo (" config ") Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġcredential File Opened _ only Credentials Exists () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwriter Ġ= Ġmock < ConfigFile Writer >() ĊĊĠĠĠĠĠĠĠ Ġval Ġconfig File Ġ= ĠFile ( folder Rule . new Folder (), Ġ" config ") ĊĠĠĠĠĠĠĠ Ġval Ġcred File Ġ= Ġfolder Rule . new File (" credentials ") ĊĠĠĠĠĠĠĠ Ġcred File . write Text (" cred ") ĊĊĠĠĠĠĠĠĠ Ġval Ġsut Ġ= ĠCreate OrUpdate Credential Profiles Action ( writer , Ġconfig File , Ġcred File ) ĊĠĠĠĠĠĠĠ ĠMessages . set Test Dialog ( Test Dialog . OK ) ĊĊĠĠĠĠĠĠĠ Ġsut . action Performed ( Test ActionEvent ( DataContext Ġ{ Ġproject Rule . project Ġ})) ĊĊĠĠĠĠĠĠĠ Ġverify Zero Interactions ( writer ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( file Editor Manager . open Files ). has Only One Element Satisf ying Ġ{ ĠassertThat ( it . name ). isEqualTo (" credentials ") Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġempty File Can Be Opened As Plain Text () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwriter Ġ= Ġmock < ConfigFile Writer >() ĊĊĠĠĠĠĠĠĠ Ġval Ġconfig File Ġ= ĠFile ( folder Rule . new Folder (), Ġ" config ") ĊĠĠĠĠĠĠĠ Ġval Ġcred File Ġ= Ġfolder Rule . new File (" credentials ") ĊĊĠĠĠĠĠĠĠ Ġval Ġsut Ġ= ĠCreate OrUpdate Credential Profiles Action ( writer , Ġconfig File , Ġcred File ) ĊĠĠĠĠĠĠĠ ĠMessages . set Test Dialog ( Test Dialog . OK ) ĊĊĠĠĠĠĠĠĠ Ġsut . action Performed ( Test ActionEvent ( DataContext Ġ{ Ġproject Rule . project Ġ})) ĊĊĠĠĠĠĠĠĠ Ġverify Zero Interactions ( writer ) ĊĊĠĠĠĠĠĠĠ ĠassertThat ( file Editor Manager . open Files ). has Size ( 1 ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. all Satisf y () Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( it . name ). isEqualTo (" credentials ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( it . fileType ). isEqualTo ( File Types . PLAIN _ TEXT ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġnegative Confirmation DoesNot Create File () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġwriter Ġ= Ġmock < ConfigFile Writer >() ĊĊĠĠĠĠĠĠĠ Ġval Ġconfig File Ġ= ĠFile ( folder Rule . new Folder (), Ġ" config ") ĊĠĠĠĠĠĠĠ Ġval Ġcred File Ġ= ĠFile ( folder Rule . new Folder (), Ġ" credentials ") ĊĊĠĠĠĠĠĠĠ Ġval Ġsut Ġ= ĠCreate OrUpdate Credential Profiles Action ( writer , Ġconfig File , Ġcred File ) ĊĠĠĠĠĠĠĠ ĠMessages . set Test Dialog ( Test Dialog . NO ) ĊĊĠĠĠĠĠĠĠ Ġsut . action Performed ( Test ActionEvent ( DataContext Ġ{ Ġproject Rule . project Ġ})) ĊĊĠĠĠĠĠĠĠ Ġverify Zero Interactions ( writer ) ĊĠĠĠ Ġ} Ċ } Ċ