1 /*
2  * Copyright (c) 2018 John Mayfield
3  *
4  * Contact: cdk-devel@lists.sourceforge.net
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or (at
9  * your option) any later version. All we ask is that proper credit is given
10  * for our work, which includes - but is not limited to - adding the above
11  * copyright notice to the beginning of your source code files, and to any
12  * copyright notice that you may distribute with programs based on this work.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 U
22  */
23 package org.openscience.cdk.tools.manipulator;
24 
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.openscience.cdk.interfaces.IAtom;
28 import org.openscience.cdk.interfaces.IBond;
29 import org.openscience.cdk.interfaces.IChemObject;
30 import org.openscience.cdk.sgroup.Sgroup;
31 import org.openscience.cdk.sgroup.SgroupType;
32 
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 
38 import static org.hamcrest.CoreMatchers.is;
39 import static org.hamcrest.CoreMatchers.not;
40 import static org.hamcrest.CoreMatchers.sameInstance;
41 import static org.junit.Assert.*;
42 
43 public class SgroupManipulatorTest {
44 
45     @Test
copyNull()46     public void copyNull() throws Exception {
47         assertNull(SgroupManipulator.copy(null, null));
48     }
49 
50     @Test
copySgroups()51     public void copySgroups() throws Exception {
52         List<Sgroup> sgroups = new ArrayList<>();
53         IAtom a1 = Mockito.mock(IAtom.class);
54         IAtom a2 = Mockito.mock(IAtom.class);
55         IBond b1 = Mockito.mock(IBond.class);
56         IBond b2 = Mockito.mock(IBond.class);
57         Sgroup       sgroup       = new Sgroup();
58         sgroup.setType(SgroupType.CtabStructureRepeatUnit);
59         sgroup.setSubscript("n");
60         sgroup.addAtom(a1);
61         sgroup.addAtom(a2);
62         sgroup.addBond(b1);
63         sgroup.addBond(b2);
64         sgroups.add(sgroup);
65         List<Sgroup> copied = SgroupManipulator.copy(sgroups, null);
66         Sgroup copiedSgroup = copied.get(0);
67         assertThat(copiedSgroup, not(sameInstance(sgroup)));
68         assertThat(copiedSgroup.getType(), is(sgroup.getType()));
69         assertThat(copiedSgroup.getSubscript(), is(sgroup.getSubscript()));
70         assertThat(copiedSgroup.getAtoms(), is(sgroup.getAtoms()));
71         assertThat(copiedSgroup.getBonds(), is(sgroup.getBonds()));
72     }
73 
74 
75     @Test
copySgroups2()76     public void copySgroups2() throws Exception {
77         List<Sgroup>                 sgroups = new ArrayList<>();
78         Map<IChemObject,IChemObject> replace = new HashMap<>();
79 
80         IAtom                        a1      = Mockito.mock(IAtom.class);
81         IAtom                        a2      = Mockito.mock(IAtom.class);
82         IBond                        b1      = Mockito.mock(IBond.class);
83         IBond                        b2      = Mockito.mock(IBond.class);
84 
85         IAtom                        a1copy      = Mockito.mock(IAtom.class);
86         IAtom                        a2copy      = Mockito.mock(IAtom.class);
87         IBond                        b1copy      = Mockito.mock(IBond.class);
88         IBond                        b2copy      = Mockito.mock(IBond.class);
89 
90         replace.put(a1, a1copy);
91         replace.put(a2, a2copy);
92         replace.put(b1, b1copy);
93         replace.put(b2, b2copy);
94 
95         Sgroup                       sgroup  = new Sgroup();
96         sgroup.setType(SgroupType.CtabStructureRepeatUnit);
97         sgroup.setSubscript("n");
98         sgroup.addAtom(a1);
99         sgroup.addAtom(a2);
100         sgroup.addBond(b1);
101         sgroup.addBond(b2);
102         sgroups.add(sgroup);
103         List<Sgroup> copied = SgroupManipulator.copy(sgroups, replace);
104         Sgroup copiedSgroup = copied.get(0);
105         assertThat(copiedSgroup, not(sameInstance(sgroup)));
106         assertThat(copiedSgroup.getType(), is(sgroup.getType()));
107         assertThat(copiedSgroup.getSubscript(), is(sgroup.getSubscript()));
108         assertThat(copiedSgroup.getAtoms(), is(not(sgroup.getAtoms())));
109         assertThat(copiedSgroup.getBonds(), is(not(sgroup.getBonds())));
110         assertTrue(copiedSgroup.getAtoms().contains(a1copy));
111         assertTrue(copiedSgroup.getAtoms().contains(a2copy));
112         assertTrue(copiedSgroup.getBonds().contains(b1copy));
113         assertTrue(copiedSgroup.getBonds().contains(b2copy));
114     }
115 }