package http import ( "context" "errors" "net/http" "net/http/httptest" "strings" "testing" "primaya-api/cpone-middleware/internal/repository" ) type fakePerusahaanRepository struct { page repository.PerusahaanPage detail *repository.PerusahaanDetail groupDetail *repository.PerusahaanGroupTarifDetail err error listParams repository.PerusahaanListParams detailID string groupID string groupTarifID string } func (f *fakePerusahaanRepository) ListPerusahaan(_ context.Context, params repository.PerusahaanListParams) (repository.PerusahaanPage, error) { f.listParams = params return f.page, f.err } func (f *fakePerusahaanRepository) GetPerusahaan(_ context.Context, id string) (*repository.PerusahaanDetail, error) { f.detailID = id return f.detail, f.err } func (f *fakePerusahaanRepository) GetPerusahaanGroupTarif(_ context.Context, id, groupTarifID string) (*repository.PerusahaanGroupTarifDetail, error) { f.groupID = id f.groupTarifID = groupTarifID return f.groupDetail, f.err } func TestListPerusahaanResponseParity(t *testing.T) { repo := &fakePerusahaanRepository{page: repository.PerusahaanPage{ Total: 16, Items: []repository.PerusahaanSummary{{ PerusahaanID: "AAL", KodePerusahaan: "INS-114", Nama: "ASURANSI ALLIANZ LIFE INDONESIA, PT", }}, }} handler := Handler{perusahaanRepo: repo} request := httptest.NewRequest(http.MethodGet, "http://localhost/api/cpone/perusahaan?search=allianz&page=1&kode_rs=AWALBROS_DEV", nil) recorder := httptest.NewRecorder() handler.ListPerusahaan(recorder, request) if recorder.Code != http.StatusOK { t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String()) } if repo.listParams.Search != "allianz" || repo.listParams.Page != 1 || repo.listParams.PerPage != 15 { t.Fatalf("params = %+v", repo.listParams) } for _, expected := range []string{ `"PerusahaanID":"AAL"`, `"KodePerusahaan":"INS-114"`, `"Nama":"ASURANSI ALLIANZ LIFE INDONESIA, PT"`, `"last_page":2`, `"next_page_url":"http://localhost/api/cpone/perusahaan?kode_rs=AWALBROS_DEV\u0026page=2"`, } { if !strings.Contains(recorder.Body.String(), expected) { t.Fatalf("response missing %s: %s", expected, recorder.Body.String()) } } } func TestGetPerusahaanByCode(t *testing.T) { detail := &repository.PerusahaanDetail{ PerusahaanID: "AAL", KodePerusahaan: "INS-114", Nama: "ALLIANZ", JenisPerusahaanID: 1, Penjamin: "Y", Asuransi: "Y", Provider: "N", COB: "N", MataUangID: "IDR", DefaultDiskon: "0.00", PPN: "0.00", FlagDiskon: "N", VerifikasiBilling: "N", NA: "N", } repo := &fakePerusahaanRepository{detail: detail} handler := Handler{perusahaanRepo: repo} request := httptest.NewRequest(http.MethodGet, "/api/cpone/perusahaan/INS-114", nil) request.SetPathValue("id", "INS-114") recorder := httptest.NewRecorder() handler.GetPerusahaan(recorder, request) if recorder.Code != http.StatusOK || repo.detailID != "INS-114" { t.Fatalf("status=%d id=%q body=%s", recorder.Code, repo.detailID, recorder.Body.String()) } for _, expected := range []string{`"success":true`, `"PerusahaanID":"AAL"`, `"ppn":"0.00"`, `"nomerRekening":null`} { if !strings.Contains(recorder.Body.String(), expected) { t.Fatalf("response missing %s: %s", expected, recorder.Body.String()) } } } func TestGetPerusahaanGroupTarif(t *testing.T) { name := "DARAH LENGKAP" detail := &repository.PerusahaanGroupTarifDetail{ PerusahaanID: "AAL", KodePerusahaan: "INS-114", NamaPerusahaan: "ALLIANZ", GroupTarif: []repository.PerusahaanGroupTarif{{ GroupTarifID: 27, NamaGroupTarif: "Tarif 2026", TanggalBerlaku: "2026-01-01", TanggalBerakhir: "2026-12-31", PeriodeTarifID: 10, Keterangan: "Tarif 2026", DetailLayanan: []repository.PerusahaanGroupTarifLayanan{{ TarifLayananID: 1, LayananID: "6413", NamaLayanan: &name, JenisLayananID: 1, Kelas: "Default", NamaKelas: "Default", Harga: "100000.00", }}, }}, } repo := &fakePerusahaanRepository{groupDetail: detail} handler := Handler{perusahaanRepo: repo} request := httptest.NewRequest(http.MethodGet, "/api/cpone/perusahaan/AAL/group-tarif?group_tarif_id=27", nil) request.SetPathValue("id", "AAL") recorder := httptest.NewRecorder() handler.GetPerusahaanGroupTarif(recorder, request) if recorder.Code != http.StatusOK || repo.groupID != "AAL" || repo.groupTarifID != "27" { t.Fatalf("status=%d company=%q group=%q body=%s", recorder.Code, repo.groupID, repo.groupTarifID, recorder.Body.String()) } for _, expected := range []string{`"GroupTarifID":27`, `"NamaGroupTarif":"Tarif 2026"`, `"DetailLayanan":[`, `"NamaKelas":"Default"`} { if !strings.Contains(recorder.Body.String(), expected) { t.Fatalf("response missing %s: %s", expected, recorder.Body.String()) } } } func TestPerusahaanNotFoundAndInternalError(t *testing.T) { tests := []struct { name string repo *fakePerusahaanRepository want int }{ {name: "not found", repo: &fakePerusahaanRepository{}, want: http.StatusNotFound}, {name: "error", repo: &fakePerusahaanRepository{err: errors.New("query failed")}, want: http.StatusInternalServerError}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { handler := Handler{perusahaanRepo: test.repo} request := httptest.NewRequest(http.MethodGet, "/api/cpone/perusahaan/UNKNOWN", nil) request.SetPathValue("id", "UNKNOWN") recorder := httptest.NewRecorder() handler.GetPerusahaan(recorder, request) if recorder.Code != test.want { t.Fatalf("status=%d want=%d body=%s", recorder.Code, test.want, recorder.Body.String()) } }) } }