高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
@isTest
private class NFM001TriggerTest {
 
    // calloutのtriggerなので、assertがなにもできないです。
    // NFM001.trigger、NFM001Controller、Nfm001Sync のカバー率を確認
    static testMethod void testInsert() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }
        
        // insert
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name = 'Katsu テスト';
        insert company;
        System.assertEquals('', NFM001Controller.debug_msg);
        
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert section;
        System.assertEquals('', NFM001Controller.debug_msg);
 
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM001TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert depart;
        //System.assertEquals('1,NFM001_callout_insert_*', NFM001Controller.debug_msg);
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[0].Id;
        depart2.Name         = 'NFM001TestDepart2';
        depart2.Department_Name__c  = 'NFM002TestDepart';
        depart2.ParentId            = section.Id;
        depart2.Department_Class__c = section.Id;
        depart2.Hospital__c         = company.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert depart2;
        System.assertEquals('', NFM001Controller.debug_msg);
 
        // 診療科更新
        depart.Name         = 'Katsu診療科';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update depart;
        System.assertEquals('', NFM001Controller.debug_msg);
        System.Test.startTest();
        // 診療科departを削除
        depart.Is_Active__c = '無効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '1,NFM001_callout_logic_deletesssss';
        update depart;
        System.assertEquals(true, NFM001Controller.debug_msg.startsWith('1,NFM001_callout_logic_delete'));
        String assertDateTo = NFM001Controller.debug_msg.substring('1,NFM001_callout_logic_delete'.length() + 1);
        Account departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart.Id];
        System.assertEquals(null, departAssert.DepartmentEffectiveDateTo__c);
        
        // 病院を削除
        company.Is_Active__c = '無効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '2,NFM001_callout_logic_deletessss';
        update company;
        System.assertEquals(true, NFM001Controller.debug_msg.startsWith('2,NFM001_callout_logic_delete'));
        assertDateTo = NFM001Controller.debug_msg.substring('2,NFM001_callout_logic_delete'.length() + 1);
        departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart2.Id];
        System.assertEquals(null, departAssert.DepartmentEffectiveDateTo__c);
 
        // 病院をundelete
        company.Is_Active__c = '有効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update company;
        departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart.Id];
        System.assertEquals('草案中', departAssert.Is_Active__c);
        departAssert = [Select Id, Name, Is_Active__c, DepartmentEffectiveDateTo__c from Account where Id = :depart2.Id];
        System.assertEquals('有効', departAssert.Is_Active__c);
        System.Test.stopTest();
    }
    
    // 病院更新1
    static testMethod void testUpdate1_1() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Name = 'Katsu テスト Name';
        acc.Is_Active__c = '有効';
        update acc;
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Is_Active__c = '無効';
        //update acc;
        //System.assertEquals(false, NFM001Controller.debug_msg.startsWith('1,NFM001_callout_logic_delete'));
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Is_Active__c = '有効';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Site = 'Katsu テスト Site';
        //acc.Is_Active__c = '有効';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Phone = '0312345678';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.Fax = '0387654321';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        //NFM001Controller.isRunning = false;
        //NFM001Controller.debug_msg = '';
        //acc.OCM_Category__c = 'Katsu テスト OCM_Category__c';
        //update acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
    }
    static testMethod void testUpdate1_2() {
        List<RecordType> rects1_2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects1_2.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects1_2[0].Id;
        acc.Name = 'Katsu KatsuKatsuテスト';
        acc.Site = 'koKatsu';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Site = 'Katsu テスト Site';
        acc.Is_Active__c = '有効';
        update acc;
        Test.stopTest();
    }
    static testMethod void testUpdate1_3() {
         List<RecordType> rects1_3 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects1_3.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects1_3[0].Id;
        acc.Name = 'Katsu テKatsuスト';
        acc.Site = 'kook';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Fax = '0387654321';
        acc.Is_Active__c = '有効';
        update acc;
        //System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    static testMethod void testUpdate1_4() {
         List<RecordType> rects1_4 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects1_4.size() == 0) {
            return;
        } 
        Account acc = new Account();
        acc.RecordTypeId = rects1_4[0].Id;
        acc.Name = 'Katsu atsu テステスト';
        acc.Site = 'kook';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Phone = '0312345678';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    
 
    // 病院更新2
    static testMethod void testUpdate2_1() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        acc.Postal_Code__c = '1-3-55';
        acc.Town__c = 'Katsu own__c';
        acc.Street__c = 'Katsu Street__c';
        insert acc;
        //System.assertEquals('', NFM001Controller.debug_msg);
        Test.startTest();
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Postal_Code__c = '1-3-56';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
 
    static testMethod void testUpdate2_2() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        acc.Postal_Code__c = '1-3-55';
        acc.Town__c = 'Katsu own__c';
        acc.Street__c = 'Katsu Street__c';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Town__c = 'Katsu Town__c';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    static testMethod void testUpdate2_3() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        acc.Postal_Code__c = '1-3-55';
        acc.Town__c = 'Katsu own__c';
        acc.Street__c = 'Katsu Street__c';
        insert acc;
        Test.startTest();
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Street__c = 'Katsu テスト Street__c';
        acc.Is_Active__c = '有効';
        update acc;
        System.assertEquals('1,NFM001_callout_logic_undelete', NFM001Controller.debug_msg);
        Test.stopTest();
    }
    
    // 病院.Name更新
    static testMethod void testUpdate3() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'Katsu テスト1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'Katsu テスト2';
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
 
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
 
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert new Account[] {depart1, depart2};
 
        // update
        company1.Name = 'Katsu1';
        company2.Name = 'Katsu2';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update hps;
 
        // assert
        company1 = [Select Id, Name,Sys_Dept_Name_Change_Chk__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name,Sys_Dept_Name_Change_Chk__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, Sys_Dept_Name_Change_Chk__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, Sys_Dept_Name_Change_Chk__c from Account where Id = :depart2.Id];
        System.assertEquals('Katsu1', company1.Name);
        //System.assertEquals(company1.Name + ' ' + dc1s[0].Department_Class_Label__c, dc1s[0].Name);
        //System.assertEquals(company1.Name + ' ' + dc1s[0].Department_Class_Label__c + ' ' + depart1.Department_Name__c, depart1.Name);
        //System.assertEquals(false, company1.Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, dc1s[0].Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, depart1.Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals('Katsu2', company2.Name);
        //System.assertEquals(company2.Name + ' ' + dc2s[1].Department_Class_Label__c, dc2s[1].Name);
        //System.assertEquals(company2.Name + ' ' + dc2s[1].Department_Class_Label__c + ' ' + depart2.Department_Name__c, depart2.Name);
        //System.assertEquals(false, company2.Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, dc2s[1].Sys_Dept_Name_Change_Chk__c);
        //System.assertEquals(false, depart2.Sys_Dept_Name_Change_Chk__c);
    }
    
    // 病院.OCM_Category__c と 診療科.OCM_Category__c の更新
    static testMethod void testUpdate4() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'Katsu テスト1';
        company1.OCM_Category__c = 'M1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'Katsu テスト2';
        company2.OCM_Category__c = 'M2';
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
 
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
 
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert new Account[] {depart1, depart2};
 
        // update HP の OCM_Category__c
        company1.OCM_Category__c = 'L1';
        company2.OCM_Category__c = 'L2';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update hps;
 
        // assert
        company1 = [Select Id, Name, OCM_Category__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name, OCM_Category__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, OCM_Category__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, OCM_Category__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart2.Id];
        System.assertEquals('L1', company1.OCM_Category__c);
        System.assertEquals('L2', company2.OCM_Category__c);
        System.assertEquals('M1', dc1s[0].OCM_Category__c);
        System.assertEquals('M2', dc2s[1].OCM_Category__c);
        System.assertEquals('M2', depart1.OCM_Category__c);
        System.assertEquals('M2', depart2.OCM_Category__c);
        System.assertEquals('', NFM001Controller.debug_msg);
        
        // update 診療科 の OCM_Category__c
        // 実際は 診療科を直接更新しない、ここは、NFM001 送信対象になってないことを確認
        depart1.OCM_Category__c = 'D1';
        depart2.OCM_Category__c = 'D2';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        update new Account[] {depart1, depart2};
        depart1 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, OCM_Category__c from Account where Id = :depart2.Id];
        
        // assert
        System.assertEquals('', NFM001Controller.debug_msg);
        System.assertEquals('D1', depart1.OCM_Category__c);
        System.assertEquals('D2', depart2.OCM_Category__c);
    }
    
    // State_Master__c 変更のテスト
    // 病院.State_Master__c と 診療科.State_Master__c の更新
    static testMethod void testUpdate5() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        // RecordType re = new RecordType (id = '01210000000QemGAAS');
        // List<RecordType> rectCo = new List<RecordType>();
        // rectCo.add(re);
        if (rectCo.size() == 0) {
            return;
        }
 
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Address_Level__c al1_1 = new Address_Level__c(
            Name = '四川省'
        );
        Address_Level__c al1_2 = new Address_Level__c(
            Name = '上海市'
        );
        insert new List<Address_Level__c> {al1_1, al1_2};
        
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'Katsu テスト1';
        company1.OCM_Category__c = 'M1';
        company1.State_Master__c = al1_1.Id;
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'Katsu テスト2';
        company2.OCM_Category__c = 'M2';
        company2.State_Master__c = al1_2.Id;
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
 
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
 
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        insert new Account[] {depart1, depart2};
 
        // assert
        company1 = [Select Id, Name, State_Text__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name, State_Text__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart2.Id];
        System.assertEquals('四川省', company1.State_Text__c);
        System.assertEquals('上海市', company2.State_Text__c);
        System.assertEquals('四川省', dc1s[0].State_Text__c);
        System.assertEquals('上海市', dc2s[1].State_Text__c);
        System.assertEquals('四川省', depart1.State_Text__c);
        System.assertEquals('上海市', depart2.State_Text__c);
 
        // update HP の State_Master__c
        company1.State_Master__c = al1_2.Id;
        company1.Is_Active__c = '有効';
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        hps = new Account[] {company1, company2};
        update hps;
 
        // assert
        company1 = [Select Id, Name, State_Text__c from Account where Id = :company1.Id];
        company2 = [Select Id, Name, State_Text__c from Account where Id = :company2.Id];
        dc1s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        dc2s = [Select Id, Name, Department_Class_Label__c, State_Text__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
        depart1 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart1.Id];
        depart2 = [Select Id, Name, Department_Name__c, State_Text__c from Account where Id = :depart2.Id];
        System.assertEquals('四川省', company1.State_Text__c);
        System.assertEquals('上海市', company2.State_Text__c);
        System.assertEquals('四川省', dc1s[0].State_Text__c);
        System.assertEquals('上海市', dc2s[1].State_Text__c);
        System.assertEquals('四川省', depart1.State_Text__c);
        System.assertEquals('上海市', depart2.State_Text__c);
        System.assertEquals('', NFM001Controller.debug_msg);
    }
    /**
     *@param
     *@description 经销商医院名字更新
     *@description 病院。コンタクト、ユーザーなどの初期データ作る
     *@return 
     */
    static testMethod void testUpdateAgencyName() {
        Account account1 = new Account();
        Account account2 = new Account();
        Contact contact2 = new Contact();
        User user = new User();
        Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
        // 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;
 
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;
        List<Account> acc2list=[select RecordTypeId, Name from Account];
        System.assertEquals(10,acc2list.size());
        // 取引先責任者
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;
        
        List<Contact> contact2list=[select Id, FirstName from Contact];
        System.assertEquals('責任者',contact2list[0].FirstName);
        // ユーザー用户
        Profile p = [select Id from Profile where Name = '901_经销商社区普通权限_2重验证(ET)'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com';
        user.Alias = 'テユ';
        user.responsibility__c = '华北营业一部';
        user.OCM_Management_Province__c = '北京';
        user.CommunityNickname = 'テストユーザー';
        insert user;
        List<user> users = [select Id, Name, LastName, FirstName from User where LastName='テスト'];
        System.assertEquals('ユーザー',users[0].FirstName );
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        insert agency_hospital_link;
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        account1.Name = 'test医院1';
        Test.startTest();        
        update account1;
        Test.stopTest();
        List<Agency_Hospital_Link__c> agencylistCheck = [select Id, Name from Agency_Hospital_Link__c ];
        System.assertEquals(1, agencylistCheck.size());
        System.assertEquals('test1医院', agencylistCheck[0].Name);        
 
    }
    // Resendtest
    static testMethod void resend_test() {
        // 病院 の recode type を取得
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        insert acc;
        System.assertEquals('', NFM001Controller.debug_msg);
        
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Name = 'Katsu テスト Name';
        System.Test.startTest();
        update acc;
        System.Test.stopTest();
        List<BatchIF_Log__c> rowbl = [Select Id,
                                            Log__c,
                                            Log2__c,
                                            ErrorLog__c,
                                            retry_cnt__c
                                        from BatchIF_Log__c
                                        where Type__c = 'NFM001'
                                        and RowDataFlg__c = true
                                        order by CreatedDate desc];
        System.assertEquals(0, rowbl.size());
        //System.assertEquals(1,rowbl[0].retry_cnt__c);
        System.debug('sizeis' + rowbl.size());
        //System.debug('countis' + rowbl[0].retry_cnt__c);
 
         //NFM001Controller.execute(rowbl[0],null);
        //List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
        //        Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //        from BatchIF_Log__c
        //        where Id = :rowbl[0].id];
        //System.assertEquals(2, bl[0].retry_cnt__c);
 
        //NFM001Controller.execute(rowbl[0],null);
        //bl = [Select Id, Is_Error__c,
        //        Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //        from BatchIF_Log__c
        //        where Id = :rowbl[0].id];
        //System.assertEquals(3, bl[0].retry_cnt__c);
    }
@isTest
    static void resend_testok() {
        // 病院 の recode type を取得
        Test.setMock(WebServiceMock.class, new NFMTest_Mock());
        System.debug('ccccc');
        List<RecordType> rects = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rects.size() == 0) {
            return;
        } 
        
        // insert
        Account acc = new Account();
        acc.RecordTypeId = rects[0].Id;
        acc.Name = 'Katsu テスト';
        System.debug('pppppp');
        insert acc;
        System.debug('oooooo');
        System.assertEquals('', NFM001Controller.debug_msg);
          System.debug('dddddd');
        // update
        NFM001Controller.isRunning = false;
        NFM001Controller.debug_msg = '';
        acc.Name = 'Katsu テスト Name';
  System.debug('eeeeee');
        System.Test.startTest();
        update acc;
        System.Test.stopTest();
  System.debug('ffffff');
        List<BatchIF_Log__c> rowbl = [Select Id,
                                            Log__c,
                                            Log2__c,
                                            ErrorLog__c,
                                            retry_cnt__c
                                        from BatchIF_Log__c
                                        where Type__c = 'NFM001'
                                        and RowDataFlg__c = true
                                        order by CreatedDate desc];
        System.assertEquals(0, rowbl.size());
        //System.assertEquals(0,rowbl[0].retry_cnt__c);
 
        //Test.setMock(HttpCalloutMock.class, new NFMTestMock());
        //NFM001Controller.execute(rowbl[0],null);
        //System.Test.stopTest();
        //System.debug(rowbl[0]);
        //List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
        //        Type__c, Log__c, ErrorLog__c,retry_cnt__c
        //        from BatchIF_Log__c
        //        where Id = :rowbl[0].id];
        //System.debug(bl[0]);
 
        //System.assertEquals(0, bl[0].retry_cnt__c);
 
    }
    @isTest
    static void accountUpdateName() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
 
        List<RecordType> rectContract = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCo.size() == 0) {
            return;
        }
 
        //经销商
        Account myAccount1 = new Account(name='Testaccount001',
                                        Dealer_discount__c =20,
                                        RecordTypeId = rectCo[0].Id,
                                        //start
                                        Delete_Flag__c = false,
                                        Sales_Shop_Class__c = '特约经销商(区域)',
                                        AgencyContract_Management_Code__c = '8075802',
                                        Business_Paper_Expiration_Date__c = Date.today().addDays(+1),
                                        Tax_Practice_Expiration_Date__c = Date.today().addDays(+1),
                                        Ban_On_Use_Date__c = Date.today().addDays(+1),
                                        DGLicenseTo__c = Date.today().addDays(+1),
                                        Ban_On_Use_DateDD__c = Date.today().addDays(+1),
                                        Medical_Equipment_Expiration_Date__c = Date.today().addDays(+1),
                                        Is_Active__c = '有効',
                                        OCM_Category__c = 'H0');
        insert myAccount1;
        myAccount1.AgencyContract_Management_Code__c = '8075803';
        myAccount1.Dealer_discount__c = 30;
        update myAccount1;
        //经销商有效合同
        Account myAccount2 = new Account(name='Testaccount002',
                                            RecordTypeId = rectContract[0].Id,
                                            Delete_Flag__c = false,
                                            Contract_Decide_Start_Date__c = Date.today().addDays(-1),
                                            Contract_Decide_End_Date__c =Date.today().addDays(+1),
                                            Agent_Ref__c =myAccount1.Id,
                                            ParentId =myAccount1.Id);
        insert myAccount2;
        myAccount2.Contract_Decide_Start_Date__c = Date.today().addDays(+2);
        update myAccount2;
 
        //NFM001Controller.isRunning = false;
        //Boolean isChanged = false;
        //Boolean needChild = true;
        myAccount1.Name = 'Testaccount0012';
        System.Test.StartTest();
        update myAccount1;
        Account acc = [select Name,OCM_Category__c,id,Agency_With_EffectiveContract__c,Sap2sfdcDealers_ModifycationLogo__c from Account where id = :myAccount1.Id ];
        Account acd = [select Is_Active_Formula__c from Account where id =:myAccount2.id];
        System.assertEquals('Testaccount0012',acc.Name);
        System.assertEquals('有效',acd.Is_Active_Formula__c);
        System.Test.StopTest();
 
    }
   
}