fix constructor ref to a non existing bean
[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.Version;
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     Version version = Version.V11;
78     OxmEntityLookup entityLookup = new OxmEntityLookup();
79     entityLookup.addEntityDescriptor("pserver", desc);
80
81     AttributeUpdater updater =
82         new AttributeUpdater(new OxmModelLoader(version, null, null), entityLookup, aaiAdapter);
83     Map<String, Object> attributes = new HashMap<>();
84     attributes.put("prov-status", "PREPROV");
85     attributes.put("in-maint", "true");
86     OperationResult result = updater.updateObjectAttribute(
87         "cloud-infrastructure/pservers/pserver/something", attributes, "someid");
88     assertEquals(Status.FORBIDDEN.getStatusCode(), result.getResultCode());
89   }
90
91
92   // This needs the OXM file in place to work.
93   /**
94    * Test get edit object from uri.
95    *
96    * @throws Exception the exception
97    */
98   @Test(expected = NullPointerException.class)
99   public void testGetEditObjectFromUri() throws Exception {
100     Version version = Version.V11;
101     OxmModelLoader loader = new OxmModelLoader(version, null, null);
102
103
104     OxmEntityDescriptor desc = new OxmEntityDescriptor();
105     desc.addPrimaryKeyName("hostname");
106     desc.setEntityName("pserver");
107
108     OxmEntityLookup entityLookup = new OxmEntityLookup();
109     entityLookup.addEntityDescriptor("pserver", desc);
110
111
112     // DynamicType mockType = Mockito.mock(DynamicType.class);
113     // Class<? extends DynamicEntity> mockDynamicEntity = Mockito.mock(DynamicEntity.class);
114
115     // Mockito.when(mockType.getJavaClass()).thenReturn(mockDynamicEntity);
116
117
118
119     HashMap<String, DynamicType> typeLookup = new HashMap<String, DynamicType>();
120     // typeLookup.put("pserver", mockType);
121
122     entityLookup.setEntityTypeLookup(typeLookup);
123
124
125     AttributeUpdater updater =
126         new AttributeUpdater(new OxmModelLoader(version, null, null), entityLookup, aaiAdapter);
127     AaiEditObject result = updater.getEditObjectFromUri(null);
128     assertEquals("Pserver", result.getObjectType());
129     assertEquals("pserver", result.getRootElement());
130     assertEquals("hostname", result.getKeyName());
131     assertEquals("mtznjtax101", result.getKeyValue());
132   }
133
134   /**
135    * Test get relative uri.
136    *
137    * @throws Exception the exception
138    */
139
140   @Test
141   public void testGetRelativeUri() throws Exception {
142
143     OxmEntityDescriptor desc = new OxmEntityDescriptor();
144     desc.addPrimaryKeyName("hostname");
145     desc.setEntityName("pserver");
146
147     OxmEntityLookup entityLookup = new OxmEntityLookup();
148     entityLookup.addEntityDescriptor("pserver", desc);
149
150     AttributeUpdater updater = new AttributeUpdater(new OxmModelLoader(null,null), entityLookup, aaiAdapter);
151     // Test entity uri without "/aai/version/"
152     String result = updater.getRelativeUri("cloud-infrastructure/pservers/pserver/mtznjtax101");
153     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
154     result = updater.getRelativeUri("/aai/v8/cloud-infrastructure/pservers/pserver/mtznjtax101");
155     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
156
157     result = updater.getRelativeUri("/v8/cloud-infrastructure/pservers/pserver/mtznjtax101");
158     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
159
160     result = updater.getRelativeUri("aai/v88/cloud-infrastructure/pservers/pserver/mtznjtax101");
161     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
162
163     result = updater.getRelativeUri("/cloud-infrastructure/pservers/pserver/mtznjtax101");
164     assertEquals("/cloud-infrastructure/pservers/pserver/mtznjtax101", result);
165   }
166
167 }