<commit_before>// Copyright © 2015 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package handler<commit_msg>[test-handler] Write mock handler component for testing<commit_after>// Copyright © 2015 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package handler

import (
	//	"os"
	//	"reflect"
	"testing"
	//	"time"
	//
	. "github.com/TheThingsNetwork/ttn/core"
	//	"github.com/TheThingsNetwork/ttn/utils/errors"
	. "github.com/TheThingsNetwork/ttn/utils/errors/checks"
	//	"github.com/TheThingsNetwork/ttn/utils/pointer"
	. "github.com/TheThingsNetwork/ttn/utils/testing"
	"github.com/brocaar/lorawan"
)

func TestRegister(t *testing.T) {
	{
		Desc(t, "Registration valid HRegistration")

		devStorage := newMockDevStorage()
		pktStorage := newMockPktStorage()
		handler := New(devStorage, pktStorage, GetLogger(t, "Handler"))
		an := newMockAckNacker()
		r := newTestRegistration(
			[8]byte{1, 1, 1, 1, 1, 1, 1, 1},
			[8]byte{2, 2, 2, 2, 2, 2, 2, 2},
		)

		err := handler.Register(r, an)
		CheckErrors(t, nil, err)
	}
}

// ----- TYPE utilities

//
// MOCK DEV STORAGE
//
type mockDevStorage struct {
	Failures     map[string]error
	LookupEntry  devEntry
	Personalized HRegistration
	Activated    HRegistration
}

func newMockDevStorage(failures ...string) *mockDevStorage {
	return &mockDevStorage{
		Failures: make(map[string]error),
	}
}

func (s mockDevStorage) Lookup(appEUI lorawan.EUI64, devEUI lorawan.EUI64) (devEntry, error) {
	if s.Failures["Lookup"] != nil {
		return devEntry{}, s.Failures["Lookup"]
	}
	return s.LookupEntry, nil
}

func (s *mockDevStorage) StorePersonalized(r HRegistration) error {
	s.Personalized = r
	return s.Failures["StorePersonalized"]
}

func (s *mockDevStorage) StoreActivated(r HRegistration) error {
	s.Activated = r
	return s.Failures["StoreActivated"]
}

func (s mockDevStorage) Close() error {
	return s.Failures["Close"]
}

//
// MOCK PKT STORAGE
//
type mockPktStorage struct {
	Failures  map[string]error
	PullEntry APacket
	Pushed    APacket
}

func newMockPktStorage() *mockPktStorage {
	return &mockPktStorage{
		Failures: make(map[string]error),
	}
}

func (s *mockPktStorage) Push(p APacket) error {
	s.Pushed = p
	return s.Failures["Push"]
}

func (s *mockPktStorage) Pull(appEUI lorawan.EUI64, devEUI lorawan.EUI64) (APacket, error) {
	if s.Failures["Pull"] != nil {
		return nil, s.Failures["Pull"]
	}
	return s.PullEntry, nil
}

func (s *mockPktStorage) Close() error {
	return s.Failures["Close"]
}

//
// MOCK ACK/NACKER
//
type mockAckNacker struct{}

func newMockAckNacker() *mockAckNacker {
	return &mockAckNacker{}
}

func (an mockAckNacker) Ack(p Packet) error {
	return nil
}

func (an mockAckNacker) Nack() error {
	return nil
}
{'fraction_non_alphanumeric': 0.13138429024304332, 'fraction_numerical': 0.011976047904191617, 'mean_word_length': 4.201465201465202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3002211', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1037, 'n_words': 262}