예제 #1
0
파일: sample.go 프로젝트: logrusorgru/glr
func main() {
	Black := "Black"
	Red := "Red"
	Green := "Green"
	Brown := "Brown"
	Blue := "Blue"
	Magenta := "Magenta"
	Cyan := "Cyan"
	Gray := "Gray"
	BgBlack := "BgBlack"
	BgRed := "BgRed"
	BgGreen := "BgGreen"
	BgBrown := "BgBrown"
	BgBlue := "BgBlue"
	BgMagenta := "BgMagenta"
	BgCyan := "BgCyan"
	BgGray := "BgGray"
	Bold := "Bold"
	Reverse := "Reverse"

	fmt.Println(glr.Black(Black))
	fmt.Println(glr.Red(Red))
	fmt.Println(glr.Green(Green))
	fmt.Println(glr.Brown(Brown))
	fmt.Println(glr.Blue(Blue))
	fmt.Println(glr.Magenta(Magenta))
	fmt.Println(glr.Cyan(Cyan))
	fmt.Println(glr.Gray(Gray))
	fmt.Println(glr.BgBlack(BgBlack))
	fmt.Println(glr.BgRed(BgRed))
	fmt.Println(glr.BgGreen(BgGreen))
	fmt.Println(glr.BgBrown(BgBrown))
	fmt.Println(glr.BgBlue(BgBlue))
	fmt.Println(glr.BgMagenta(BgMagenta))
	fmt.Println(glr.BgCyan(BgCyan))
	fmt.Println(glr.BgGray(BgGray))
	fmt.Println(glr.Bold(Bold))
	fmt.Println(glr.Reverse(Reverse))

	fmt.Println(glr.Blue(glr.BgGreen("Blue and BgGreen")))

	fmt.Println(glr.Clear(glr.Blue(glr.BgGreen("Cleared: Blue and BgGreen"))))

}
예제 #2
0
func RunScenario(t *testing.T, persistance cqrs.EventStreamRepository) {
	typeRegistry := cqrs.NewTypeRegistry()
	bus := rabbit.NewEventBus("amqp://*****:*****@localhost:5672/", "example_test", "testing.example")
	repository := cqrs.NewRepositoryWithPublisher(persistance, bus, typeRegistry)
	repository.GetTypeRegistry().RegisterAggregate(&Account{})
	repository.GetTypeRegistry().RegisterEvents(AccountCreatedEvent{}, EmailAddressChangedEvent{}, AccountCreditedEvent{}, AccountDebitedEvent{}, PasswordChangedEvent{})
	accountID := "5058e029-d329-4c4b-b111-b042e48b0c5f"

	readModel := NewReadModelAccounts()

	usersModel := NewUsersModel()

	eventDispatcher := cqrs.NewVersionedEventDispatchManager(bus, typeRegistry)

	eventDispatcher.RegisterEventHandler(AccountCreatedEvent{}, func(event cqrs.VersionedEvent) error {
		readModel.UpdateViewModel([]cqrs.VersionedEvent{event})
		usersModel.UpdateViewModel([]cqrs.VersionedEvent{event})
		return nil
	})

	eventDispatcher.RegisterEventHandler(AccountCreditedEvent{}, func(event cqrs.VersionedEvent) error {
		readModel.UpdateViewModel([]cqrs.VersionedEvent{event})
		return nil
	})

	eventDispatcher.RegisterEventHandler(AccountDebitedEvent{}, func(event cqrs.VersionedEvent) error {
		readModel.UpdateViewModel([]cqrs.VersionedEvent{event})
		return nil
	})

	eventDispatcher.RegisterEventHandler(EmailAddressChangedEvent{}, func(event cqrs.VersionedEvent) error {
		readModel.UpdateViewModel([]cqrs.VersionedEvent{event})
		usersModel.UpdateViewModel([]cqrs.VersionedEvent{event})
		return nil
	})

	eventDispatcher.RegisterEventHandler(PasswordChangedEvent{}, func(event cqrs.VersionedEvent) error {
		usersModel.UpdateViewModel([]cqrs.VersionedEvent{event})
		return nil
	})

	stopChannel := make(chan bool)
	go eventDispatcher.Listen(stopChannel)

	testCorrelationID := uuid.New()
	readModel.LoadAccounts(persistance)

	usersModel.LoadUsers(persistance)

	log.Println("Loaded accounts")
	log.Println(readModel)

	log.Println("Loaded Users")
	log.Println(usersModel)

	log.Println("Create or find an account")
	readModelAccount := readModel.Accounts[accountID]

	log.Println(readModelAccount)

	log.Println("Create or find a user")
	user := usersModel.Users[accountID]

	log.Println(user)

	var account *Account

	if readModelAccount == nil {
		log.Println("Get hash for user...")

		hashedPassword, err := GetHashForPassword("$ThisIsMyPassword1")

		if err != nil {
			t.Fatal("Error: ", err)
		}

		log.Println("Get hash for user...")

		log.Println("Create new account...")
		account = NewAccount("John", "Snow", "*****@*****.**", hashedPassword, 0.0)

		log.Println("Set ID...")
		account.SetID(accountID)

		log.Println(account)
	} else {
		account, _ = NewAccountFromHistory(accountID, repository)
	}

	log.Println(account)
	log.Println(readModel)
	log.Println(usersModel)

	account.ChangePassword("$ThisIsANOTHERPassword")

	if !account.CheckPassword("$ThisIsANOTHERPassword") {
		t.Fatal("Password is incorrect for account")
	}

	log.Println("Change email address and credit the account")
	account.ChangeEmailAddress("*****@*****.**")
	account.Credit(50)
	account.Credit(50)
	log.Println(account)
	log.Println(readModel)

	log.Println("Persist the account")
	repository.Save(account, "")
	log.Println(readModel)
	log.Println(usersModel)

	log.Println("Load the account from history")
	account, error := NewAccountFromHistory(accountID, repository)
	if error != nil {
		t.Fatal(error)
	}

	log.Println(account)
	log.Println(readModel)

	log.Println("Change the email address, credit 150, debit 200")
	lastEmailAddress := "*****@*****.**"
	account.ChangeEmailAddress(lastEmailAddress)
	account.Credit(150)
	account.Debit(200)
	log.Println(account)
	log.Println(readModel)

	log.Println("Persist the account")
	repository.Save(account, testCorrelationID)
	log.Println(readModel)
	log.Println(usersModel)

	log.Println("Load the account from history")
	account, error = NewAccountFromHistory(accountID, repository)
	if error != nil {
		t.Fatal(error)
	}

	time.Sleep(100 * time.Millisecond)
	// All events should have been replayed and the email address should be the latest
	log.Println(account)
	log.Println(readModel)
	// log.Println(usersModel)
	if account.EmailAddress != lastEmailAddress {
		t.Fatal("Expected emailaddress to be ", lastEmailAddress)
	}

	log.Println("Stop channel")
	stopChannel <- true

	log.Println("GetIntegrationEventsByCorrelationID")
	correlationEvents, err := persistance.GetIntegrationEventsByCorrelationID(testCorrelationID)
	if err != nil {
		t.Fatal(err)
	}

	if len(correlationEvents) == 0 {
		t.Fatal("Expected correlation events")
	}

	for _, correlationEvent := range correlationEvents {
		log.Println(glr.Green(fmt.Sprintf("%v", correlationEvent.Event)))
	}
}
예제 #3
0
func RegisterCommandHandlers(commandDispatcher *cqrs.CommandDispatchManager, repository cqrs.EventSourcingRepository) {
	commandDispatcher.RegisterCommandHandler(CreateAccountCommand{}, func(command cqrs.Command) error {
		createAccountCommand := command.Body.(CreateAccountCommand)
		log.Println(glr.Green("Processing command - Create account"))
		account := NewAccount(createAccountCommand.FirstName,
			createAccountCommand.LastName,
			createAccountCommand.EmailAddress,
			createAccountCommand.PasswordHash,
			createAccountCommand.InitialBalance)

		log.Println(glr.Green("Set ID..."))
		account.SetID(accountID)
		log.Println(account)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(ChangeEmailAddressCommand{}, func(command cqrs.Command) error {
		changeEmailAddressCommand := command.Body.(ChangeEmailAddressCommand)
		log.Println(glr.Green("Processing command - Change email address"))
		account, err := NewAccountFromHistory(changeEmailAddressCommand.AccountID, repository)
		if err != nil {
			return err
		}

		account.ChangeEmailAddress(changeEmailAddressCommand.NewEmailAddress)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(ChangePasswordCommand{}, func(command cqrs.Command) error {
		changePasswordCommand := command.Body.(ChangePasswordCommand)
		log.Println(glr.Green("Processing command - Change password"))
		account, err := NewAccountFromHistory(changePasswordCommand.AccountID, repository)
		if err != nil {
			return err
		}

		account.ChangePassword(changePasswordCommand.NewPassword)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(CreditAccountCommand{}, func(command cqrs.Command) error {
		creditAccountCommand := command.Body.(CreditAccountCommand)
		log.Println(glr.Green("Processing command - Credit account"))
		account, err := NewAccountFromHistory(creditAccountCommand.AccountID, repository)
		if err != nil {
			return err
		}

		account.Credit(creditAccountCommand.Amount)
		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})

	commandDispatcher.RegisterCommandHandler(DebitAccountCommand{}, func(command cqrs.Command) error {
		debitAccountCommand := command.Body.(DebitAccountCommand)
		log.Println(glr.Green("Processing command - Debit account"))
		account, err := NewAccountFromHistory(debitAccountCommand.AccountID, repository)
		if err != nil {
			return err
		}

		if err := account.Debit(debitAccountCommand.Amount); err != nil {
			return err
		}

		log.Println(glr.Green("Persist the account"))
		repository.Save(account, command.CorrelationID)
		log.Println(glr.Green(account.String()))
		return nil
	})
}
예제 #4
0
파일: glr_test.go 프로젝트: logrusorgru/glr
func TestGreen(t *testing.T) {
	r := "\033[32mGreen\033[0m"
	if s := glr.Green("Green"); s != r {
		t.Error("Expected '", r, "', got '", s, "'")
	}
}
예제 #5
0
파일: glr_test.go 프로젝트: logrusorgru/glr
func ExampleGreen() {
	fmt.Println(glr.Green("Green"))
	// Output:
	// Green
}