5eb5611ff1aaf492eddf610747ee9a2f0fd92ce1
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestGroupData.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.models.pdp.concepts.PdpGroup;
31
32 public class TestGroupData {
33     private static final String NAME = "my-name";
34
35     private PdpGroup oldGroup;
36     private PdpGroup newGroup;
37     private GroupData data;
38
39     /**
40      * Sets up.
41      */
42     @Before
43     public void setUp() {
44         oldGroup = new PdpGroup();
45         oldGroup.setName(NAME);
46
47         newGroup = new PdpGroup(oldGroup);
48
49         data = new GroupData(oldGroup);
50     }
51
52     @Test
53     public void testNew() {
54         data = new GroupData(oldGroup, true);
55         assertSame(oldGroup, data.getGroup());
56
57         assertFalse(data.isUnchanged());
58         assertTrue(data.isNew());
59         assertFalse(data.isUpdated());
60
61         data.update(newGroup);
62         assertFalse(data.isUnchanged());
63         assertTrue(data.isNew());
64         assertFalse(data.isUpdated());
65         assertSame(newGroup, data.getGroup());
66
67         // repeat with a new group
68         newGroup = new PdpGroup(oldGroup);
69         data.update(newGroup);
70         assertFalse(data.isUnchanged());
71         assertTrue(data.isNew());
72         assertFalse(data.isUpdated());
73         assertSame(newGroup, data.getGroup());
74     }
75
76     @Test
77     public void testUpdateOnly() {
78         assertTrue(data.isUnchanged());
79         assertFalse(data.isUpdated());
80         assertSame(oldGroup, data.getGroup());
81
82         data.update(newGroup);
83
84         assertFalse(data.isUnchanged());
85         assertTrue(data.isUpdated());
86         assertFalse(data.isNew());
87         assertSame(newGroup, data.getGroup());
88
89         // repeat
90         newGroup = new PdpGroup(oldGroup);
91         data.update(newGroup);
92         assertFalse(data.isUnchanged());
93         assertTrue(data.isUpdated());
94         assertFalse(data.isNew());
95         assertSame(newGroup, data.getGroup());
96
97         // incorrect name
98         newGroup = new PdpGroup(oldGroup);
99         newGroup.setName("other");
100         assertThatIllegalArgumentException().isThrownBy(() -> data.update(newGroup))
101                         .withMessage("expected group my-name, but received other");
102     }
103 }