Java tutorial
/* * Copyright 2016 Brigham Young University * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package edu.byu.mpn.test; import edu.byu.jwt.domain.CasUser; import edu.byu.jwt.domain.Wso2SubscribedClient; import edu.byu.jwt.spring.domain.JwtUserDetails; import edu.byu.mpn.da.interfaces.DeviceDao; import edu.byu.mpn.domain.Device; import edu.byu.mpn.rest.DeviceController; import edu.byu.mpn.test.helpers.TestMpnClient; import edu.byu.spring.ByuApplicationContextInitializer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import static org.junit.Assert.*; /** * Created by cwoodfie on 4/22/16. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:test-datasource-context.xml", "classpath:rest-servlet-context.xml", "classpath:client-context.xml", "classpath:test-da-context.xml", "classpath:mpn-context.xml" }, initializers = { ByuApplicationContextInitializer.class }) public class DeviceControllerIntegrationTest { private static final Logger LOG = LogManager.getLogger(DeviceControllerIntegrationTest.class); private static final Authentication AUTHENTICATION = new Authentication() { @Override public Collection<? extends GrantedAuthority> getAuthorities() { return null; } @Override public Object getCredentials() { return null; } @Override public Object getDetails() { return null; } @Override public Object getPrincipal() { Wso2SubscribedClient client = new Wso2SubscribedClient(); CasUser clientOwner = new CasUser(); clientOwner.setPersonId("admin"); CasUser resourceOwner = new CasUser(); resourceOwner.setPersonId("user"); ArrayList<GrantedAuthority> resourceOwnerAuthorities = new ArrayList<GrantedAuthority>(); resourceOwnerAuthorities.add(new GrantedAuthority() { @Override public String getAuthority() { return "GRO_BYU_MPN_ADMIN"; } }); return new JwtUserDetails(client, clientOwner, resourceOwner, null, resourceOwnerAuthorities); } @Override public boolean isAuthenticated() { return false; } @Override public void setAuthenticated(boolean b) throws IllegalArgumentException { } @Override public String getName() { return null; } }; private DeviceController controller; private DeviceDao deviceDao; @Autowired public void setController(DeviceController controller) { this.controller = controller; } @Autowired public void setDeviceDao(DeviceDao deviceDao) { this.deviceDao = deviceDao; } @Before public void setup() { SecurityContextHolder.getContext().setAuthentication(AUTHENTICATION); controller.setMpnClient(new TestMpnClient()); } @Test public void testUpdateDevice() { Date dateRegistered = new Date(); Date dateTimeUpdated = deviceDao.getDeviceById(1).getDateTimeUpdated(); // Tests that correct error responses are returned in cases with invalid devices assertEquals(400, controller.updateDevice(1, "personId", null).getStatus()); assertEquals(400, controller .updateDevice(1, "personId", new Device(null, "personId", "token", dateTimeUpdated)).getStatus()); assertEquals(400, controller.updateDevice(1, "personId", new Device(1, null, "token", dateTimeUpdated)).getStatus()); assertEquals(400, controller.updateDevice(1, "personId", new Device(1, "personId", null, dateTimeUpdated)) .getStatus()); assertEquals(400, controller.updateDevice(1, "personId", new Device(1, "personId", "token", null)).getStatus()); assertEquals(400, controller .updateDevice(1, "personId", new Device(100, "personId", "token", dateTimeUpdated)).getStatus()); assertEquals(400, controller .updateDevice(100, "personId", new Device(1, "personId", "token", dateTimeUpdated)).getStatus()); assertEquals(403, controller.updateDevice(9, "notMyPersonId", new Device(9, "notMyPersonId", "token9", deviceDao.getDeviceById(9).getDateTimeUpdated())) .getStatus()); assertEquals(404, controller .updateDevice(100, "personId", new Device(100, "personId", "token", dateTimeUpdated)).getStatus()); // Device updated successfully (personId null -> "personId") assertEquals(200, controller .updateDevice(1, "personId", new Device(1, "personId", "valid", dateTimeUpdated)).getStatus()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Device device = deviceDao.getDeviceById(1); // When personId is changed, dateRegistered is updated device.setPersonId(null); deviceDao.saveOrUpdateDevice(device, "notme"); device = deviceDao.getDeviceById(device.getId()); device.setPersonId("otherId1"); assertEquals(200, controller.updateDevice(1, "otherId1", device).getStatus()); device = deviceDao.getDeviceById(device.getId()); assertFalse(dateRegistered.equals(device.getDateRegistered())); assertTrue("otherId1".equals(device.getPersonId())); // When device is edited but personId is not changed, dateRegistered stays the same dateRegistered = device.getDateRegistered(); device.setToken("valid2"); device.setDeviceId("notDeviceId1"); assertEquals(200, controller.updateDevice(1, "otherId1", device).getStatus()); assertTrue(dateRegistered.equals(deviceDao.getDeviceById(device.getId()).getDateRegistered())); } }