update sparky with configurable features
[aai/sparky-be.git] / sparkybe-onap-service / src / test / java / org / onap / aai / sparky / editattributes / AttributeUpdaterTest.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright Â© 2017 AT&T Intellectual Property.
6  * Copyright Â© 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.onap.aai.sparky.editattributes;
27
28 import static org.junit.Assert.assertEquals;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import javax.ws.rs.core.Response.Status;
34
35 import org.eclipse.persistence.dynamic.DynamicType;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.onap.aai.restclient.client.OperationResult;
40 import org.onap.aai.setup.SchemaVersion;
41 import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor;
42 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
43 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
44 import org.onap.aai.sparky.dal.ActiveInventoryAdapter;
45 import org.onap.aai.sparky.editattributes.AttributeUpdater.AaiEditObject;
46
47
48
49 /**
50  * The Class AttributeUpdaterTest.
51  */
52 public class AttributeUpdaterTest {
53
54   private ActiveInventoryAdapter aaiAdapter;
55
56   /**
57    * Sets the up.
58    *
59    * @throws Exception the exception
60    */
61   @Before
62   public void setUp() throws Exception {
63
64     aaiAdapter = Mockito.mock(ActiveInventoryAdapter.class);
65   }
66
67   /**
68    * @throws Exception
69    */
70
71   @Test
72   public void testUpdateObjectAttribute() throws Exception {
73
74     OxmEntityDescriptor desc = new OxmEntityDescriptor();
75     desc.addPrimaryKeyName("hostname");
76     desc.setEntityName("pserver");
77     OxmEntityLookup entityLookup = new OxmEntityLookup();
78     entityLookup.addEntityDescriptor("pserver", desc);
79
80     AttributeUpdater updater =
81         new AttributeUpdater(new OxmModelLoader("v11", null, null), entityLookup, aaiAdapter,"aai");
82     Map<String, Object> attributes = new HashMap<>();
83     attributes.put("prov-status", "PREPROV");
84     attributes.put("in-maint", "true");
85     OperationResult result = updater.updateObjectAttribute(
86         "cloud-infrastructure/pservers/pserver/something", attributes, "someid");
87     assertEquals(Status.FORBIDDEN.getStatusCode(), result.getResultCode());
88   }
89
90
91   // This needs the OXM file in place to work.
92   /**
93    * Test get edit object from uri.
94    *
95    * @throws Exception the exception
96    */
97   @Test(expected = NullPointerException.class)
98   public void testGetEditObjectFromUri() throws Exception {
99     String version = "v11";
100     OxmModelLoader loader = new OxmModelLoader(version, null, null);
101
102
103     OxmEntityDescriptor desc = new OxmEntityDescriptor();
104     desc.addPrimaryKeyName("hostname");
105     desc.setEntityName("pserver");
106
107     OxmEntityLookup entityLookup = new OxmEntityLookup();
108     entityLookup.addEntityDescriptor("pserver", desc);
109
110
111     // DynamicType mockType = Mockito.mock(DynamicType.class);
112     // Class<? extends DynamicEntity> mockDynamicEntity = Mockito.mock(DynamicEntity.class);
113
114     // Mockito.when(mockType.getJavaClass()).thenReturn(mockDynamicEntity);
115
116
117
118     HashMap<String, DynamicType> typeLookup = new HashMap<String, DynamicType>();
119     // typeLookup.put("pserver", mockType);
120
121     entityLookup.setEntityTypeLookup(typeLookup);
122
123
124     AttributeUpdater updater =
125         new AttributeUpdater(new OxmModelLoader(version, null, null), entityLookup, aaiAdapter,"aai");
126     AaiEditObject result = updater.getEditObjectFromUri(null);
127     assertEquals("Pserver", result.getObjectType());
128     assertEquals("pserver", result.getRootElement());
129     assertEquals("hostname", result.getKeyName());
130     assertEquals("mtznjtax101", result.getKeyValue());
131   }
132
133   /**
134    * Test get relative uri.
135    *
136    * @throws Exception the exception
137    */
138
139   @Test
140   public void testGetRelativeUri() throws Exception {
141
142     OxmEntityDescriptor desc = new OxmEntityDescriptor();
143     desc.addPrimaryKeyName("hostname");
144     desc.setEntityName("pserver");
145
146     OxmEntityLookup entityLookup = new OxmEntityLookup();
147     entityLookup.addEntityDescriptor("pserver", desc);
148
149     AttributeUpdater updater = new AttributeUpdater(new OxmModelLoader("v11",null,null), entityLookup, aaiAdapter,"aai");
150     // Test entity uri without "/aai/version/"
151     String result = updater.getRelativeUri("cloud-infrastructure/pservers/pserver/mtznjtax101");
152     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
153     result = updater.getRelativeUri("/aai/v8/cloud-infrastructure/pservers/pserver/mtznjtax101");
154     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
155
156     result = updater.getRelativeUri("/v8/cloud-infrastructure/pservers/pserver/mtznjtax101");
157     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
158
159     result = updater.getRelativeUri("aai/v88/cloud-infrastructure/pservers/pserver/mtznjtax101");
160     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
161
162     result = updater.getRelativeUri("/cloud-infrastructure/pservers/pserver/mtznjtax101");
163     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
164   }
165
166 }